[CodeForces 300C Beautiful Numbers]组合计数
题意:十进制的每一位仅由a和b组成的数是“X数”,求长度为n,各数位上的数的和是X数的X数的个数
思路:由于总的位数为n,每一位只能是a或b,令a有p个,则b有(n-p)个,如果 a*p+b*(n-p) 为X数,则这种情况的答案就是C(n,p),将所有情况累加起来即可。
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=;i<a.size();i++)a[i]=b[i];} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-8; /* -------------------------------------------------------------------------------- */ template<int mod>
struct ModInt {
const static int MD = mod;
int x;
ModInt(ll x = ): x(x % MD) {}
int get() { return x; } ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); }
ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); }
ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); }
ModInt operator / (const ModInt &that) const { return *this * that.inverse(); } ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; }
ModInt operator -= (const ModInt &that) { x -= that.x; if (x < ) x += MD; }
ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; }
ModInt operator /= (const ModInt &that) { *this = *this / that; } ModInt inverse() const {
int a = x, b = MD, u = , v = ;
while(b) {
int t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
if(u < ) u += MD;
return u;
} };
typedef ModInt<> mint; const int maxn = 1e6 + ;
bool yes[ * maxn];
int a, b, n;
mint fac[maxn], facinv[maxn]; void pre_init() {
fillchar(yes, );
for (int i = ; i < ( << ); i ++) {
int buf = ;
for (int j = ; j < ; j ++) {
if (( << j) & i) buf = buf * + b;
else buf = buf * + a;
yes[buf] = true;
}
yes[buf] = true;
}
fac[] = facinv[] = ;
for (int i = ; i <= n; i ++) {
fac[i] = fac[i - ] * i;
facinv[i] = facinv[i - ] / i;
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
while (cin >> a >> b >> n) {
pre_init();
mint ans = ;
for (int i = ; i <= n; i ++) {
if (yes[b * n + (a - b) * i]) {
ans += fac[n] * facinv[i] * facinv[n - i];
}
}
cout << ans.get() << endl;
}
return ;
}
[CodeForces 300C Beautiful Numbers]组合计数的更多相关文章
- CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+高速幂)
C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 300C Beautiful Numbers
枚举,组合数,逆元. 枚举$a$用了$i$个,那么$b$就用了$n-i$个,这个时候和$sum=a*i+b*(n-i)$,判断$sum$是否满足条件,如果满足,那么答案加上$C(n,i)$. #inc ...
- Codeforces 300C Beautiful Numbers 【组合数】+【逆元】
<题目链接> 题目大意: 给出a和b,如果一个数每一位都是a或b,那么我们称这个数为good,在good的基础上,如果这个数的每一位之和也是good,那么这个数是excellent.求长度 ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- CodeForces 55D Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
随机推荐
- curl book
curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多种Linux发行版中,并且有DOS和Win32.Win64下的移植版本. 官方地址 man page 下载页面, ...
- PostMan接口测试(很全面的接口测试教程)
一:理论部分 1. 前言 在前后端分离开发时,后端工作人员完成系统接口开发后,需要与前端人员对接,测试调试接口,验证接口的正确性可用性.而这要求前端开发进度和后端进度保持基本一致,任何一方的进度跟不上 ...
- 用scanf、printf输入输出string型字符串
c语言里是没有string型的,string在c++里面.有的时候在c++里要用scanf.printf输入输出string型字符串,这是可以实现的,不过要做一点处理. 具体操作看代码: #inclu ...
- tp5.1 模型 where多条件查询 like 查询
来源:https://blog.csdn.net/qq_41241684/article/details/87866416 所以我改成这样: $paperTypeModel = new PaperTy ...
- php函数封装
这是一些自定义封装的函数类,调用起来很方便,以后会有更多的封装函数更新! // 弹框跳转function alert($msg,$url=''){ echo "<script>& ...
- 第八次-非确定的自动机NFA确定化为DFA
提交作业 NFA 确定化为 DFA 子集法: f(q,a)={q1,q2,…,qn},状态集的子集 将{q1,q2,…,qn}看做一个状态A,去记录NFA读入输入符号之后可能达到的所有状态的集合. ...
- LightOJ 1287 Where to Run(期望)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1287 题意:给定一个n个点的无向图(0到n-1),你开始在0.你开始遍历这个图 ...
- 在CentOS 7中安装配置JDK8
为什么80%的码农都做不了架构师?>>> ###说明 参考博客:http://blog.csdn.net/czmchen/article/details/41047187 系统环 ...
- INTERVIEW #5
笔试 150min,3题,每题100分,自己果然还是个蒟蒻呢~ 最近状态好差,虽然做了一些题,但还是考得稀烂,大概有几点需要加强: 独立做题,不要一边看板子一边写代码,更不要一开始就看题解: 对之前研 ...
- python(格式化输出)
一.%格式化输出 1.整数的输出(参照ASCII) %o —— oct 八进制 %d —— dec 十进制(digit ) %x —— hex 十六进制 >>> print('%o' ...