Codeforces round 181 div2 C

枚举n个位置中有多少个为数字a(设为x),剩下的位置放b。则如果sum = a * x + b * (n - x)是good number,那么答案加上C(n, x),所以唯一的问题的如何计算C(n, x)。

C(n, x) % Mod = n! / (x! * (n - x) !) % Mod= n! * (x! * (n - x)!)^(-1) % Mod。(p)^(-1)表示p对Mod的乘法逆元。因为Mod是素数,所以p^(g(Mod)) % Mod = 1,g(Mod) = Mod - 1,于是p对Mod的乘法逆元就是p^(Mod - 2),于是这里用乘法逆元用欧拉函数和快速幂求。

代码如下:

/*
 * Author: stormdpzh
 * Created Time:  2013/6/21 16:38:34
 * File Name: c.cpp
 */
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <functional>

#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d\n", n)
#define mset(a, b) memset(a, b, sizeof(a))

using namespace std;

typedef long long lint;
const int INF = 1 << 30;
const int MaxN = 1000005;
const int Mod = 1000000007;

lint fac[MaxN];
int n;
int a, b;

void init()
{
    fac[0] = 1;
    repf(i, 1, MaxN - 1) {
        fac[i] = fac[i - 1] * i % Mod;
    }
}

bool check(int x)
{
    while(x) {
        int y = x % 10;
        x /= 10;
        if(y != a && y != b) return false;
    }
    return true;
}

int mypow(lint x, int n)
{
    lint ans = 1;
    while(n > 0) {
        if(n & 1) ans = ans * x % Mod;
        x = x * x % Mod;
        n >>= 1;
    }
    return ans;
}

int main()
{
    init();
    while(3 == scanf("%d%d%d", &a, &b, &n)) {
        lint ans = 0;
        repf(i, 0, n) {
            int x = i, y = n - i;
            int sum = x * a + y * b;
            if(check(sum)) {
                ans = (ans + (fac[n] * mypow(fac[x] * fac[y] % Mod, Mod - 2) % Mod)) % Mod;
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值