您的位置:首页 > 其它

Codeforces 300C Beautiful Numbers 【组合数学】

2016-03-23 00:24 399 查看
题目链接:Codeforces 300C Beautiful Numbers

题意:给定两个数a和b,美丽数是只含有a、b的整数。现在让你构造一个长度为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|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e6 + 1;
const int pN = 1e6;// <= 10^7
const 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
* 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: