Beautiful Numbers 1800 ——组合数学

news/2024/6/16 8:51:57 标签: leetcode, 算法, 动态规划

problem cf 1800
由于每一位的和和顺序无关,所以枚举即可,
时间复杂度 O ( n l o g n ) O(nlogn) O(nlogn)

// Decline is inevitable,
// Romance will last forever.
#include <bits/stdc++.h>
using namespace std;
//#define mp make_pair
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define LL long long
#define ld long double
#define endl '\n'
#define RE0 return 0
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)

#define int long long
const int maxn = 1e6 + 10;
const int P = 1e9 + 7;    //998244353
const int INF = 0x7f7f7f7f;
int fac[maxn];
int a, b, n;
bool judge(int c) {
    while(c) {
        int z = c % 10;
        if(z != a && z != b) return false;
        c /=10;
    }
    return true;
}
ll power(ll a, ll b) {
    ll ans = 1 % P;
    for (; b; b >>= 1) {
        if (b & 1)  ans = ans * a % P;
        a = a * a % P;
    }
    return ans;
}
inline ll inv(ll x) {
    return power(x, (ll)(P-2));
}
void solve() {
    fac[0] = 1;
    cin >> a >> b >> n;
    for(int i =1 ; i <=n; i++) {
        fac[i] = fac[i-1] * i % P;
    }
    int cnt = 0;
    for(int i = 0; i <= n; i++) {
        int c = a * i + b * (n-i);
        if(judge(c)) {
            cnt = (cnt + fac[n]*inv(fac[i])%P*inv(fac[n-i])%P)%P;
        }
    }
    cout << cnt << endl;
}
signed main() {
     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//    int T; scanf("%d", &T); while(T--)
//    freopen("1channel00.txt","r",stdin);
//    freopen("2.txt","w",stdout);
//    int T; cin >> T; while(T--)
        solve();
    RE0;
}

http://www.niftyadmin.cn/n/1764925.html

相关文章

无关(relationship)——容斥原理+二进制枚举 *

problem nowcoder 真没想到这题用容斥原理来做&#xff0c;感觉很新颖 这种求区间[l,r]的问题若用[1,r]-[1,l-1]&#xff0c;记得把 l-1 // Decline is inevitable, // Romance will last forever. #include <bits/stdc.h> using namespace std; //#define mp make_pair…

回声消除技术解析——转

一、前言 因为工作的关系&#xff0c;笔者从2004年开始接触回声消除(Echo Cancellation)技术&#xff0c;而后一直在某大型通讯企业从事与回声消除技术相关的工作&#xff0c;对回声消除这个看似神秘、高端和难以理解的技术领域可谓知之甚详。 要了解回声消除技术的来龙去脉&am…

最后的晚餐 ——容斥原理,圆周排列 *

problem nowcoder 求任意一对夫妻都不挨着的圆排列种类。 注意预处理阶乘的逆元以便直接求组合数&#xff0c;以及不断更新2的幂次方。 反向考虑&#xff0c;f(i)f(i)f(i) 表示至少有 iii 对夫妻相邻的排列数&#xff0c;则由容斥原理&#xff1a; ans∑i0n(−1)if(i)ans \sum…

Non-interger Area 思维

problem nowcoder 思路 把点按照奇偶分为4类,一个三角形的面积是整数当且仅当构成它的三个点中至少2个是同一类的,用总共的三角形减去这类即可。 一开始并没有发现这个性质。 // Decline is inevitable, // Romance will last forever. #include <bits/stdc++.h> us…

CentOS6.5 (64bit) 光盘内部FTP源

一、启动系统&#xff0c;用ISO镜像挂载[rootyum ~]# mkdir -p /mnt/cdrom01[rootyum ~]# mkdir -p /mnt/cdrom02 [rootyum ~]# mount -a -t iso9660 -o loop /root/CentOS-6.5-x86_64-bin-DVD1.iso /mnt/cdrom01[rootyum ~]# mount -a -t iso9660 -o loop /root/CentOS-6.5-x8…

Division ——思维+模拟

problem nowcoder 思路 其实就是转化为 a 1 , . . a n a_1, ..a_n a

字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

题目传送门 1 /*2 题意&#xff1a;给出一系列名字变化&#xff0c;问最后初始的名字变成了什么3 字符串处理&#xff1a;每一次输入到之前的找相印的名字&#xff0c;若没有&#xff0c;则是初始的&#xff0c;pos[m] 数组记录初始位置4 在每一次更新…

P1967 [NOIP2013 提高组] 货车运输 —— LCA模板题 + 最大生成树

P1967 [NOIP2013 提高组] 货车运输 思路 显然只有2*(n-1)条边可能被用到,所以先求最大生成树,然后由于两点间的路径是唯一的,所以进行LCA,并用 w [ i ] [ j ] w[i][j] w[i][j] 记录从第i个节点到它的 <