Codeforces 300C Beautiful Numbers 【组合数学】

来源:互联网 发布:unity5.3.4f1破解mac 编辑:程序博客网 时间:2024/04/27 05:46

题目链接:Codeforces 300C Beautiful Numbers

题意:给定两个数ab,美丽数是只含有ab的整数。现在让你构造一个长度为n的整数满足自己是美丽数且每一个位之和也是美丽数。问方案数。

思路:我们枚举a的个数,判断当前整数是否合法,然后就是一个简单的组合数学了。由于较n大,我们用逆元求下就好了。

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#define PI acos(-1.0)#define CLR(a, b) memset(a, (b), sizeof(a))#define fi first#define se second#define ll o<<1#define rr o<<1|1using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MAXN = 1e6 + 1;const int pN = 1e6;// <= 10^7const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;void add(LL &x, LL y) { x += y; x %= MOD; }LL fac[MAXN];void getfac(){    fac[0] = 1 % MOD;    for(LL i = 1; i < MAXN; i++)        fac[i] = fac[i-1] * i % MOD;}LL Pow(LL a, LL n){    LL ans = 1;    while(n)    {        if(n & 1)            ans = ans * a % MOD;        a = a * a % MOD;        n >>= 1;    }    return ans;}int n, a, b;bool judge(int v) {    while(v) {        if(v % 10 != a && v % 10 != b) return false;        v /= 10;    }    return true;}LL C(LL n, LL m) {    return fac[n] * Pow(fac[m], MOD-2) % MOD * Pow(fac[n-m], MOD-2) % MOD;}int main(){    getfac();    cin >> a >> b >> n;    LL ans = 0;    for(int i = 0; i <= n; i++) {        int v = a * i + b * (n-i);        if(judge(v)) {            add(ans, C(n, i));        }    }    cout << ans << endl;    return 0;}
0 0
原创粉丝点击