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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值