Codeforces Round #181 (Div. 2)---C. Beautiful Numbers
来源:互联网 发布:动漫创作软件 编辑:程序博客网 时间:2024/05/07 19:03
Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly calls a positive integergood, if the decimal representation of this integer only contains digitsa and b. Vitaly calls a good numberexcellent, if the sum of its digits is a good number.
For example, let's say that Vitaly's favourite digits are 1 and 3, then number 12 isn't good and numbers 13 or 311 are. Also, number 111 is excellent and number11 isn't.
Now Vitaly is wondering, how many excellent numbers of length exactly n are there. As this number can be rather large, he asks you to count the remainder after dividing it by1000000007 (109 + 7).
A number's length is the number of digits in its decimal representation without leading zeroes.
The first line contains three integers: a,b, n(1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
1 3 3
1
2 3 10
165
此题首先要列出方程 sum = x * a + (n - x) * b;
表示一个n位的数里有x个a, 1 - x 个 b,如此以来只要一遍for就行了,然后判断sum是否是good数,如果是, 剩下的就是一个组合数取模的问题了,这个可以用扩展欧几里得求逆元搞定
/************************************************************************* > File Name: tmp.cpp > Author: ALex > Mail: 405045132@qq.com > Created Time: 2014年12月26日 星期五 19时22分14秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define ll long long using namespace std;const int mod = 1000000007;ll extend_gcd(ll a, ll b, ll &x, ll &y){if (!b){x = 1;y = 0;return a;}ll gcd = extend_gcd(b, a % b, x, y);ll t = x;x = y;y = t - (a / b) * x;}ll get_inverse(ll num){ll x, y;extend_gcd(num, mod, x, y);return (x % mod + mod) % mod;}ll combine(ll n, ll m){ll t1 = 1, t2 = 1;for (ll i = n; i > m; --i){t1 = (t1 * i) % mod;t2 = (t2 * (i - m)) % mod;}return t1 * get_inverse(t2) % mod;}int main(){int a, b, n;while (~scanf("%d%d%d", &a, &b, &n)){int ans = 0;for (int i = 0; i <= n; ++i){ll sum = i * a + (n - i) * b;bool flag = true;while (sum != 0){if (sum % 10 == a || sum % 10 == b){sum /= 10;continue;}flag = false;break;}if (!flag){continue;}if (i == 0){ans++;ans %= mod;continue;}ans += combine(n, i); ans %= mod;}printf("%d\n", ans);}return 0;}
- Codeforces Round #181 (Div. 2)---C. Beautiful Numbers
- Codeforces Round #236 (Div. 1) D. Beautiful Pairs of Numbers
- Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers
- Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
- Codeforces Round #240 (Div. 2) C. Mashmokh and Numbers
- Codeforces Beta Round #34 (Div. 2) C. Page Numbers
- Codeforces Round #226 (Div. 2)--C. Bear and Prime Numbers
- Codeforces Round #451 (Div. 2) C. Phone Numbers
- Codeforces Round #164 (Div. 2) C. Beautiful Sets of Points【思维题】
- Codeforces Beta Round #51 D. Beautiful numbers
- Codeforces Beta Round #51 D. Beautiful numbers
- codeforces Round #451 (Div. 2) Phone Numbers
- Codeforces Round #206 (Div. 2) E. Vasya and Beautiful Arrays
- Codeforces Round #231 (Div. 2)B. Very Beautiful Number
- Codeforces Round #231 (Div. 2) B. Very Beautiful Number
- Codeforces Round #231 (Div. 2) B Very Beautiful Number
- Codeforces Round #345 (Div. 2) B. Beautiful Paintings
- Codeforces Round #345 (Div. 2)-B. Beautiful Paintings(暴力)
- 2014_12_26
- 单例模式
- NSDictionary NSMutableDictionary的相关API总结
- 【CF189E】Weak Memory
- Linux man命令的使用方法
- Codeforces Round #181 (Div. 2)---C. Beautiful Numbers
- 推荐几个hadoop搭建的几个好的博文,亲测成功!
- poj1741 Tree
- C++ primer第4章
- USACO 1.2 Problem 2
- 取10个随机数, 每个数的取值范围是20-25, 找出其中重复的数, 重复次数
- VBP/VFP/HBP/HFP
- 如何将你的设计理念付诸实践
- 转义字符和格式字符