CodeForces #181.div2.problem C

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
 
考虑到excellent number的值<=9*10^6,枚举它的每一位digit是否为a或b只需最多循环7次(7位数),最大的位数为10^6,复杂度为O(n)。假如有a,b,n,则一开始先查看a*n,然后循环每次比上一次减少一个a,增加一个b(这里就是前面说的位数最大为10^6,即最大要循环那么多次)。假如aaabb是一个excellent number,那么abbaa,bbaaa等等也是,所以对于每次找到的符合要求的a和b的组合,要取C(n,i),i为a或b的个数均可。接下来就是如何快速求C(n,i),因为C(n,i) = (n!)/((n-i)!*(i!));对含除式的数求同余模,比如(a/b)%mod,结果为a*b的逆元%mod。记b的逆元为a[b],则C(n,i) = n! * a[(n-m)!] * a[m!] % mod。最后就是如何求某个数的逆元,对于b,p,p为质数,则b的逆元 = b^(p-2)%mod。
所以这道题的做法就是先把10^6以内的阶乘和阶乘对应的逆元打表,然后直接暴力枚举。
 
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
//#define ls l,mid,rt<<1
//#define rs mid+1,r,rt<<1|1
#define SIZE 1000005
//#define inf
#define mod 1000000007
using namespace std;

typedef __int64 Int;
typedef long long ll;
typedef unsigned long long ull;

const double PI = acos(-1.0);
const double eps = 1e-8;

int a,b,n;
int ans;
Int fac[SIZE];
Int antiFac[SIZE];

Int fp(Int n,Int m)
{
    Int ret = 1;
    while(m)
    {
        if(m & 1)
            ret = ret * n % mod;
        m >>= 1;
        n = n * n % mod;
    }
    return ret;
}

int main()
{
    fac[0] = antiFac[0] = 1;
    for(int i=1; i<SIZE; i++)
    {
        fac[i] = fac[i-1]*i%mod;
        antiFac[i] = fp(fac[i],mod-2);
    }
    while(cin >> a >> b >> n)
    {
        ans = 0;
        for(int i=0; i<=n; i++)
        {
            int temp = (n-i)*a + i*b;
            bool flag = false;
            while(temp)
            {
                int t = temp % 10;
                temp /= 10;
                if(t != a && t!= b)
                {
                    flag = true;
                    break;
                }
            }
            if(!flag)
                ans = (ans%mod + fac[n]*antiFac[n-i]%mod*antiFac[i]%mod)%mod;
        }
        cout << ans << endl;
    }
    return 0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值