CodeForces 300C Beautiful Numbers 组合数取余

题目链接 : http://codeforces.com/problemset/problem/300/C

明显的组合数问题,题意就不赘述了。

公式 C(n,m) = fact[n]*inv(fact[n-m]*fact[m])%MOD; fact[i] = i!;  留着备忘

知道上面这个东西 这个题就已经A掉了,话说数论真的好坑哇......知道公式的秒出,不知道的做到死也出不来了......

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <string>

#define LL long long
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))

using namespace std;

LL num[100000];

const LL INF = 1000000007;

LL fact[1000010];

bool Is_Good(LL a,LL b,LL sum)
{
    LL temp;
    while(sum)
    {
        temp = sum%10;
        if(temp != a && temp != b)
            return false;
        sum /= 10;
    }
    return true;
}

LL cal(LL a,LL b)
{
    if(b == 0)
        return 1;
    if(b == 1)
        return a;
    LL temp = cal(a,b>>1)%INF;
    if(b&1)
    {
        return (temp*temp%INF)*a%INF;
    }
    else
    {
        return temp*temp%INF;
    }
}

int main()
{
    LL i,a,b,n;
    LL sum,ans;

    for(fact[0] = 1,i = 1;i <= 1000000; ++i)
    {
        fact[i] = fact[i-1]*i;
        if(fact[i] >= INF)
        {
            fact[i] %= INF;
        }
    }

    while(cin>>a>>b>>n)
    {
        for(i = 0,ans = 0; i <= n; ++i)
        {
            sum = a*i + b*(n-i);

            if(Is_Good(a,b,sum))
            {
                ans += fact[n]*cal(fact[n-i],INF-2)%INF*cal(fact[i],INF-2)%INF;
                if(ans >= INF)
                    ans %= INF;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

你可能感兴趣的:(CodeForces 300C Beautiful Numbers 组合数取余)