Codeforces 300C Beautiful Numbers (数论)

原题传送门


题面:

Beautiful Numbers

                             time limit per test : 2 seconds
                           memory limit per test : 256 megabytes
                                   input : standard input
                                  output : standard output

Problem Description

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, number 111 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 (1e9 + 7).

A number’s length is the number of digits in its decimal representation without leading zeroes.

Input

The first line contains three integers: a, b, n (1 ≤ a < b ≤ 9, 1 ≤ n ≤ 1e6).

Output

Print a single integer — the answer to the problem modulo 1000000007 (1e9 + 7).

Sample Input (1)

1 3 3

Sample Output (1)

1

Sample Input (2)

2 3 10

Sample Output (2)

165

题面描述

Vitaly对a和b这两个数字很喜欢,所以他认为每个位都由a或b的数字是good的,而当这些位上的数字的和各位上的数是a或者b中的一个时,他就会认为这个数是excellent的,现在他想知道位数为n的数中有多少个数是excellent的.(例如原题面中 , a = 1, b = 3 , 111 是good的, 而1+1+1=3 == b,所以111是excellent)

题目分析

因为Vitaly认为每个位上仅是a或b的数是good的,并在good的基础上才会出现excellent.所以可以先用a和b构造出一个位数为n的数,那么这个数必定是good的,由于对于每个位置上,既可以是a,也可以是b.那么对于由i个a,n-i个b构成的数就会有C(i , n)个.(i = 0~n)因为n很大,而且i是从0到n的,不能用杨辉三角递推出C(i , n).那么就要用阶乘来求C(i , n)了,又因为要取模,对于除法取模就要计算逆元.这题的模数是一个素数,所以可以用费马小定理来求出逆元(而不需要用欧几里得拓展).

具体代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef long long int ll;
const int MOD = 1e9+7;
const int maxn = 1e6+5;
ll fact[maxn];
ll a, b, n;

ll qpow(ll x, ll n, ll mod)//快速幂
{
    x = x%mod;
    ll ans = 1;
    while(n)
    {
        if(n&1)
        {
            ans = ans*x%mod;
        }
        x = x*x%mod;
        n >>= 1;
    }
    return ans;
}

void pre_fact()//阶乘
{
    fact[0] = 1;
    for(ll i = 1; i <= maxn-5; i++)
    {
        fact[i] = fact[i-1]*i%MOD;
    }
}

bool check_sum(ll x)
{
    while(x)
    {
        if(x%10 != (ll)a && x%10 != (ll)b)//当某一位不是a或b时,check失败
        {
            return false;
        }
        x /= 10;
    }
    return true;
}

ll C(ll i, ll j)
{
    ll temp = fact[i]*fact[j-i]%MOD;
    return fact[j]*qpow(temp , MOD-2 , MOD)%MOD;//费马小定理求逆元
}

int main()
{
    pre_fact();//预处理出阶乘
    cin >> a >> b >> n ;
    ll ans = 0;
    for(ll i = 0; i <= n; i++)
    {
        if(check_sum(i*a+(n-i)*b))//
        {
            ans = (ans+C(i , n)%MOD)%MOD;
        }
    }
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43726593/article/details/88424234