CodeForces 300C C. Beautiful Numbers (数论 + 逆元 + 详解)
2015-08-13 13:07
651 查看
C. Beautiful Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vitaly is a very weird man. He's got two favorite digits a and b.
Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits a and b.
Vitaly calls a good number excellent, 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, number111 is excellent and number 11 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 by 1000000007 (109 + 7).
A number's length is the number of digits in its decimal representation without leading zeroes.
Input
The first line contains three integers: a, b, n (1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).
Output
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
Sample test(s)
input
output
input
output
[/code]
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vitaly is a very weird man. He's got two favorite digits a and b.
Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits a and b.
Vitaly calls a good number excellent, 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, number111 is excellent and number 11 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 by 1000000007 (109 + 7).
A number's length is the number of digits in its decimal representation without leading zeroes.
Input
The first line contains three integers: a, b, n (1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).
Output
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
Sample test(s)
input
1 3 3
output
1
input
2 3 10
output
165
方法一,通过学习数论乘法逆元可以快速的解决问题,
方法二,在deal(num[1] * i + (n - i) * num[2])将所有满足条件的所需要的a,b的个数先预处理用数组存储起来,然后,通过DFS()不断遍历满足条件的个数,用ans记录数据,
这里讲解一下乘法逆元的相关知识:
乘法逆元的主要运用是将除法变为乘法:
F / A mod C = D
存在一个数K 使得 A * K == 1(mod C)
那么两种乘起来就是F * K mod C = D
如此我们就将除法变为了乘法,现在只需要求解K的值
然后说明 a/x mod p=a*x^(p-2) mod p其中p为质数
的原因,这里要运用到费马小定理,也许读者可能不知道,我无法给出证明,但是大家可以先记住费马小定理就可以了:x^(p - 1) mod p == 1其中p是质数,
所以之前说的a / x mod p = a * x^(p - 2) mod p
通过前面的乘法逆元可以知道,x ^ (p - 1) mod p = 1 - > a / x mod p = a / x * x ^ (p - 1) mod p
-> a * x ^ (p - 2) mod p
接下来的解答,大家可以通过看代码了解
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; typedef long long LL; const LL mod = 1e9 + 7; const int MAXN = 1e6 + 5; LL n; LL num[3]; LL ans,f[MAXN]; LL mod_pow(LL x,LL n,LL mod) { LL res = 1; while(n > 0) { if(n & 1)res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } bool deal(LL x) { while(x > 0) { LL tmp = x % 10L; if(tmp != (LL)num[1] && tmp != (LL)num[2]) return false; x /= 10; } return true; } int main() { scanf("%I64d%I64d%I64d", &num[1], &num[2], &n); LL ans = 0; f[0] = 1; for(int i = 1; i <= n; i ++)f[i] = f[i - 1] * i % mod; for(int i = 0; i <= n; i ++) { if(deal(num[1] * i + (n - i) * num[2])) { ans = (ans + mod_pow(f[i] * f[n - i] % mod,mod - 2, mod) % mod) % mod; } } printf("%I64d\n",f * ans % mod); return 0; }
[/code]
相关文章推荐
- Java里的指针
- BootstrapQ 包Bootstrap tree,dialog等待
- nginx重启 服务
- 【VR】Leap Motion 官网文档 LeapUnityExtensions (LeapUnity扩展)
- 关于:Centoslinux 远程连接服务器时客户端一直显示:连接已复位,重新连接到(ip)
- 九度oj 1022
- hdu1213(并查集复习)
- Ubuntu下使用dex2jar
- 使用WITH AS提高性能简化嵌套SQL(转)
- Android Studio 使用 Gradle 打包 Jar
- 希尔排序1.0
- Android Studio 使用 Gradle 打包 Jar
- poj-2406-Power Strings (kmp)
- CSUOJ 赌神的战斗
- git - 简易指南
- windows下动态链接库(讲解)
- Git SSH Key 生成步骤
- hdu1394 Minimum Inversion Number(最小逆序数)
- MySQL子查询慢现象的解决
- 数据库备份与恢复:数据量大的库怎么考虑设计备份策略?