codeforces_300C_组合数_快速幂
2016-04-17 10:51
405 查看
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).
Examples
input
output
input
output
思路:遍历数中的a的个数i,b的个数为n-i,若(i*a)+(b*(n-i))为excellent,ans+=C(i,n);
求C(i,n)是关键,由费马小定理和乘法逆元可以推得组合数公式。
1)费马小定理
费马小定理(Fermat Theory)是数论中的一个重要定理,其内容为: 假如p是质数,且Gcd(a,p)=1,那么 a(p-1)(mod p)≡1。即:假如a是整数,p是质数,且a,p互质(即两者只有一个公约数1),那么a的(p-1)次方除以p的余数恒等于1。
简而言之就是如果a,p互质,同时p是质数,那么a^(p-1) mod p=1。证明略。
(2)乘法逆元
若对于a,p存在x,使得a*x mod p=1,那么我们称x为a对p的乘法逆元。证明略。
那么乘法逆元存在的意义是什么呢?
假如我们要求(a/b) mod p且无法直接求得a/b的值时,我们可以求出b对p的乘法逆元inv,那么(a/b) mod p=(a*inv) mod p。证明略。。。
证明如下:
假如inv是b对于p的乘法逆元,即b*inv=p*t+1(t为整数),移项得inv=(p*t+1)/b
(a*inv) mod p
=(a*((p*t+1)/b)) mod p
=(a*(p*t/b+1/b)) mod p
=(a/b) mod p+(a*(p*t+1)) mod p
=(a/b) mod p+(a*p*t/b) mod p
∵ (a*p*t/b) mod p=0
∴ 原式=(a/b) mod p
即(a*inv) mod p=(a/b) mod p
有了这2个概念我们就可以快速地算出组合数了。
我们可以知道x与x^p-2互为逆元(p是质数)。
证明:x与x^(p-2)互为逆元(p是质数)
由费马小定理:x^(p-1) mod p=1
x*(x^(p-2)) mod p=1
得x与x^(p-2)互为乘法逆元,证毕。
由上述结论可知,要计算C(i,n),即计算n!/(i!*(n-i)!) mod p,那么我们只需要计算n!*(i!*(n-i))^(p-2) mod p。(计算是使用快速幂)
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).
Examples
input
1 3 3
output
1
input
2 3 10
output
165
思路:遍历数中的a的个数i,b的个数为n-i,若(i*a)+(b*(n-i))为excellent,ans+=C(i,n);
求C(i,n)是关键,由费马小定理和乘法逆元可以推得组合数公式。
1)费马小定理
费马小定理(Fermat Theory)是数论中的一个重要定理,其内容为: 假如p是质数,且Gcd(a,p)=1,那么 a(p-1)(mod p)≡1。即:假如a是整数,p是质数,且a,p互质(即两者只有一个公约数1),那么a的(p-1)次方除以p的余数恒等于1。
简而言之就是如果a,p互质,同时p是质数,那么a^(p-1) mod p=1。证明略。
(2)乘法逆元
若对于a,p存在x,使得a*x mod p=1,那么我们称x为a对p的乘法逆元。证明略。
那么乘法逆元存在的意义是什么呢?
假如我们要求(a/b) mod p且无法直接求得a/b的值时,我们可以求出b对p的乘法逆元inv,那么(a/b) mod p=(a*inv) mod p。证明略。。。
证明如下:
假如inv是b对于p的乘法逆元,即b*inv=p*t+1(t为整数),移项得inv=(p*t+1)/b
(a*inv) mod p
=(a*((p*t+1)/b)) mod p
=(a*(p*t/b+1/b)) mod p
=(a/b) mod p+(a*(p*t+1)) mod p
=(a/b) mod p+(a*p*t/b) mod p
∵ (a*p*t/b) mod p=0
∴ 原式=(a/b) mod p
即(a*inv) mod p=(a/b) mod p
有了这2个概念我们就可以快速地算出组合数了。
我们可以知道x与x^p-2互为逆元(p是质数)。
证明:x与x^(p-2)互为逆元(p是质数)
由费马小定理:x^(p-1) mod p=1
x*(x^(p-2)) mod p=1
得x与x^(p-2)互为乘法逆元,证毕。
由上述结论可知,要计算C(i,n),即计算n!/(i!*(n-i)!) mod p,那么我们只需要计算n!*(i!*(n-i))^(p-2) mod p。(计算是使用快速幂)
#include<iostream> #include<cstdio> using namespace std; #define UL unsighed long long #define LL long long int a,b,n; int M=1e9+7; bool excellent(int x) { while(x>=0) { if(x%10!=a&&x%10!=b) break; x/=10; } if(x>0) return 0; else return 1; } long long powerMod(long long a,long long b,long long c) ///(a^b)%c { long long ans=1; a=a%c; while(b>0) { if(b%2==1) ans=(ans*a)%c; b=b/2; a=(a*a)%c; } return ans ; } LL mult[1000005]; void multi() { LL res=1; mult[0]=1; for(int i=1;i<=1000000;i++) mult[i]=(mult[i-1]*i)%M; } long long combine(int i,int n) { LL n1=mult ; LL n2=mult[i]; n2=(n2*mult[n-i])%M; n2=powerMod(n2,M-2,M); n2=(n2*n1)%M; return n2; } int main() { long long ans=0; multi(); scanf("%d%d%d",&a,&b,&n); ///n为数字长度 for(int i=0;i<=n;i++) { int mul=i*a+(n-i)*b; if(excellent(mul)) ans=(ans+combine(i,n)%M)%M; } printf("%I64d\n",ans%M); return 0; }
相关文章推荐
- 直接插入排序
- C/C++可变参数省略号"..."的应用
- Android adb shell学习心得(一)
- 计算机虚拟机运行原理(结构组成)
- Xcode7 中创建静态库:.a 和 .framework(一)
- diy作品——视觉追踪小车介绍
- 完美解决IE8不支持margin auto问题
- 关于内网穿透,内网映射的一些想法
- Android 开发规范
- kosaraju算法求强连通分量
- rem单位
- TortoiseSVN使用教程
- [HDOJ2512]一卡通大冒险(DP)
- 深入浅出Java并发包—读写锁ReentrantReadWriteLock原理分析(二)
- MongoDB数据表基本操作
- 大表设计思路
- 初识云计算的三种服务模式 (IaaS SaaS PaaS)
- 犬儒主义
- Window 7 自动登录
- iOS蓝牙4.0(BLE)-后台接收数据