Codeforces 300C Beautiful Numbers 【组合数学】

题目链接:Codeforces 300C Beautiful Numbers

题意:给定两个数 a b,美丽数是只含有 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|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[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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值