Monday 16 November 2015

C. Beautiful Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits a and b. Vitaly calls a good number excellent, if the sum of its digits is a good number.
For example, let's say that Vitaly's favourite digits are 1 and 3, then number 12 isn't good and numbers 13 or 311 are. Also, number111 is excellent and number 11 isn't.
Now Vitaly is wondering, how many excellent numbers of length exactly n are there. As this number can be rather large, he asks you to count the remainder after dividing it by 1000000007 (109 + 7).
A number's length is the number of digits in its decimal representation without leading zeroes.
Input
The first line contains three integers: abn (1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).
Output
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
Sample test(s)
input
1 3 3
output
1
input
2 3 10
output
165





First we need to make few observations ,as the number of digits is maximum 10^6 so the maximum value of the sum can go upto 9*10^6.  So looping once through all through all possible sum values can be plausible. So for each sum we first check if that is constructed only with a and bs . If it is then let the number have i's number of a and (n-i)'s number of bs. Hence the sum can be represented as ai+(n-i)b=sum.
THerefore for all values satisfying the equaiton we can add n!/i!(n-i)! to the answer.
The factorials and inverse factorials can be precomputed.

solution


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
 
#define mod 1000000007

typedef long long int ll;
ll f[1000010];
ll inv[1000010];
ll powmod(ll a,ll n,ll MOD)
{
ll p=1;
for(;n;)
{
if(n%2) p=(p)*a%MOD;
if(n/=2) a=(a)*a%MOD;
}
return p;
}
void pre()
{
 f[0]=1;
  for(int i=1;i<=1000001;i++)
  {
    f[i]=(f[i-1]*i)%mod;
  }
  inv[0]=1;
  for(int i=1;i<=1000001;i++)
  {
         inv[i]=powmod(f[i],mod-2,mod);
  }
}
int a,b;
bool check(int val)
{
   while(val!=0)
   {
    int rem=val%10;
    val=val/10;
    if(rem==a || rem== b )
    continue;
    else 
    return 0;
}
return 1;
}
int main()
{
pre();
int n;
//cout<<f[6]<<" "<<endl;
// int a,b;
cin>>a>>b;
if(b>a)
swap(a,b);
cin>>n;
ll ans=0;
int mx=9*n;
for(int i=1;i<=mx;i++)
{
   if(check(i))
   {
//     cout<<"for i "<<i<<endl;
       int checkval=i-b*n;
       if(checkval<0)
       continue;
           int temp=a-b;
           if(checkval%temp==0)
           {
//             cout<<"here "<<endl;
              int numbera=checkval/temp;
              int numberb=n-numbera;
//               cout<<"numer a "<<numbera<<" number b "<<numberb<<endl;
              if(numbera>=0 && numberb>=0)
              {
//               cout<<"inverses "<<inv[numbera]<<" "<<inv[numberb]<<endl;
     ll res=(((f[n]*inv[numbera])%mod)*inv[numberb])%mod;
                 ans=(ans+res)%mod;
             }
}
          
}
}
cout<<ans<<endl;
return 0;
}

No comments:

Post a Comment