codeforces Beautiful Numbers

枚举 sum = ai + b(n - i)

C[n][i] = fact[n]inv(fact[n - i]fact[i]).inv(a) is multiplicative inverse element(moduloMOD). MOD is a prime number, soinv(a) = aMOD - 2.

乘法逆元解决 除法中模的方法

代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <iomanip>
using namespace std;
const int mod =1000000007;
long long f[1000005];
long long quickpow(long long a,long long n)
{
    long long b=1;
    while(n>0)
    {
        if(n&1)
        {
            b=b*a;
            b%=mod;
        }
        n>>=1;
        a*=a;
        a%=mod;
    }
    return b%mod;
}
int check(int sum,int a,int b)
{
    while(sum>0)
    {
        int num=sum%10;
        if(num!=a&&num!=b)
        return 0;
        sum/=10;
    }
    return 1;
}
int main()
{
    int a,b,n;
    scanf("%d%d%d",&a,&b,&n);
    f[1]=1;
    f[0]=1;
    for(int i=2;i<=n;i++)
    {
        f[i]=f[i-1]*i;
        f[i]%=mod;
    }
    long long ans=0;
    for(int i=0;i<=n;i++)
    {
        int sum=a*i+b*(n-i);
        if(check(sum,a,b))
        {
            int down,up;
            down=n;
            up=i;
            long long k=f[up]*f[down-up];
            k%=mod;
            ans+=(f[down]*quickpow(k,mod-2))%mod;
            ans%=mod;
        }
    }
   // printf("%I64d\n",quickpow(2,10));
    printf("%I64d\n",ans%mod);
}




你可能感兴趣的:(codeforces Beautiful Numbers)