题意:十进制的每一位仅由a和b组成的数是“X数”,求长度为n,各数位上的数的和是X数的X数的个数

思路:由于总的位数为n,每一位只能是a或b,令a有p个,则b有(n-p)个,如果 a*p+b*(n-p) 为X数,则这种情况的答案就是C(n,p),将所有情况累加起来即可。

  1. #include <map>
  2. #include <set>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <deque>
  6. #include <queue>
  7. #include <stack>
  8. #include <vector>
  9. #include <cstdio>
  10. #include <string>
  11. #include <cstdlib>
  12. #include <cstring>
  13. #include <iostream>
  14. #include <algorithm>
  15.  
  16. using namespace std;
  17.  
  18. #define X first
  19. #define Y second
  20. #define pb push_back
  21. #define mp make_pair
  22. #define all(a) (a).begin(), (a).end()
  23. #define fillchar(a, x) memset(a, x, sizeof(a))
  24. #define copy(a, b) memcpy(a, b, sizeof(a))
  25.  
  26. typedef long long ll;
  27. typedef pair<int, int> pii;
  28. typedef unsigned long long ull;
  29.  
  30. //#ifndef ONLINE_JUDGE
  31. void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
  32. void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
  33. void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
  34. while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
  35. void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
  36. void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
  37. void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
  38. //#endif
  39. template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
  40. template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
  41. template<typename T>
  42. void V2A(T a[],const vector<T>&b){for(int i=;i<b.size();i++)a[i]=b[i];}
  43. template<typename T>
  44. void A2V(vector<T>&a,const T b[]){for(int i=;i<a.size();i++)a[i]=b[i];}
  45.  
  46. const double PI = acos(-1.0);
  47. const int INF = 1e9 + ;
  48. const double EPS = 1e-8;
  49.  
  50. /* -------------------------------------------------------------------------------- */
  51.  
  52. template<int mod>
  53. struct ModInt {
  54. const static int MD = mod;
  55. int x;
  56. ModInt(ll x = ): x(x % MD) {}
  57. int get() { return x; }
  58.  
  59. ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); }
  60. ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); }
  61. ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); }
  62. ModInt operator / (const ModInt &that) const { return *this * that.inverse(); }
  63.  
  64. ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; }
  65. ModInt operator -= (const ModInt &that) { x -= that.x; if (x < ) x += MD; }
  66. ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; }
  67. ModInt operator /= (const ModInt &that) { *this = *this / that; }
  68.  
  69. ModInt inverse() const {
  70. int a = x, b = MD, u = , v = ;
  71. while(b) {
  72. int t = a / b;
  73. a -= t * b; std::swap(a, b);
  74. u -= t * v; std::swap(u, v);
  75. }
  76. if(u < ) u += MD;
  77. return u;
  78. }
  79.  
  80. };
  81. typedef ModInt<> mint;
  82.  
  83. const int maxn = 1e6 + ;
  84. bool yes[ * maxn];
  85. int a, b, n;
  86. mint fac[maxn], facinv[maxn];
  87.  
  88. void pre_init() {
  89. fillchar(yes, );
  90. for (int i = ; i < ( << ); i ++) {
  91. int buf = ;
  92. for (int j = ; j < ; j ++) {
  93. if (( << j) & i) buf = buf * + b;
  94. else buf = buf * + a;
  95. yes[buf] = true;
  96. }
  97. yes[buf] = true;
  98. }
  99. fac[] = facinv[] = ;
  100. for (int i = ; i <= n; i ++) {
  101. fac[i] = fac[i - ] * i;
  102. facinv[i] = facinv[i - ] / i;
  103. }
  104. }
  105.  
  106. int main() {
  107. #ifndef ONLINE_JUDGE
  108. freopen("in.txt", "r", stdin);
  109. //freopen("out.txt", "w", stdout);
  110. #endif // ONLINE_JUDGE
  111. while (cin >> a >> b >> n) {
  112. pre_init();
  113. mint ans = ;
  114. for (int i = ; i <= n; i ++) {
  115. if (yes[b * n + (a - b) * i]) {
  116. ans += fac[n] * facinv[i] * facinv[n - i];
  117. }
  118. }
  119. cout << ans.get() << endl;
  120. }
  121. return ;
  122. }

[CodeForces 300C Beautiful Numbers]组合计数的更多相关文章

  1. CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+高速幂)

    C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. CodeForces 300C Beautiful Numbers

    枚举,组合数,逆元. 枚举$a$用了$i$个,那么$b$就用了$n-i$个,这个时候和$sum=a*i+b*(n-i)$,判断$sum$是否满足条件,如果满足,那么答案加上$C(n,i)$. #inc ...

  3. Codeforces 300C Beautiful Numbers 【组合数】+【逆元】

    <题目链接> 题目大意: 给出a和b,如果一个数每一位都是a或b,那么我们称这个数为good,在good的基础上,如果这个数的每一位之和也是good,那么这个数是excellent.求长度 ...

  4. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  5. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  6. CodeForces 55D Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  7. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  8. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

  9. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

随机推荐

  1. Nightmare BFS

    Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The la ...

  2. 15分钟从零开始搭建支持10w+用户的生产环境(二)

    上一篇文章,把这个架构的起因,和操作系统的选择进行了详细说明. 原文地址:15分钟从零开始搭建支持10w+用户的生产环境(一)   二.数据库的选择 对于一个10W+用户的系统,数据库选择很重要. 一 ...

  3. Jmeter系列(2)- Jmeter工具介绍、Jmeter安装目录介绍、Jmeter面板介绍

    如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html Jmeter支持哪些测试场景? Jme ...

  4. 聊一聊JSONP和图像Ping的区别

    JSONP 在讲 JSONP 之前需要再来回顾一下在页面上使用 script 引入外部的 js 文件时到底引入了什么? 先建立一个 index.js 文件. console.log(123) 再建立一 ...

  5. keras数据集读取

    from tensorflow.python import keras (x_train,y_train),(x_test,y_test) = keras.datasets.cifar100.load ...

  6. Python中实现按顺序遍历字典

    第一种方法: import collections d = collections.OrderedDict([('a',1),('b',2),('c',3)]) ''' 或者把上面的那一行改成: d ...

  7. PHP 构造方法 __construct()

    PHP 构造方法 __construct() PHP 构造方法 __construct() 允许在实例化一个类之前先执行构造方法. 构造方法 构造方法是类中的一个特殊方法.当使用 new 操作符创建一 ...

  8. beetl 模板语法

    如何定义临时变量 @var tmp = false; 如何给临时变量赋值 @tmp = true; 如何在判断中使用临时变量 @if(tmp){ ... @} 如何使用条件语句 if else @if ...

  9. ip-端口-协议等基本概念

    互联网上的计算机,都会有一个唯一的32位的地址——ip地址.我们访问服务器,就必须通过这个ip地址.   局域网里也有预留的ip地址:192/10/172开头.局域网里的ip地址也是唯一的.   NA ...

  10. Spark 源码系列(六)Shuffle 的过程解析

    Spark 大会上,所有的演讲嘉宾都认为 shuffle 是最影响性能的地方,但是又无可奈何.之前去百度面试 hadoop 的时候,也被问到了这个问题,直接回答了不知道. 这篇文章主要是沿着下面几个问 ...