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: abn (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

题目大意:

给出a和b,假设一个数每一位都是a或b,那么我们称这个数为good,在good的基础上,假设这个数的每一位之和也是good,那么这个数是excellent。

求长度为n的excellent数的个数mod(1e9+7)。ps:1e9+7是一个质数。



解题思路:

因为题目中给出了n,所以我们能够枚举a的个数m,那么剩下的(n-m)位就是b。再推断a*m+b*(n-m)是不是good数,假设是。那么我们在答案中加上C(m,n)就可以,枚举完成即终于答案。

可是n最大为1e6,计算组合数时(C(m,n)=n!/(m!*(n-m)!))要计算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<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const int MAXN=1e6+50;
typedef __int64 LL; LL f[MAXN],a,b,n; bool is_excellent(int x)
{
while(x)
{
if(x%10!=a&&x%10!=b)
return false;
x/=10;
}
return true;
} LL fastmod(LL b,LL c,LL mod)//b^c%mod
{
LL re=1,base=b;
while(c)
{
if(c&1)
re=((re%mod)*(base%mod))%mod;
base=((base%mod)*(base%mod))%mod;
c>>=1;
}
return re%mod;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
f[0]=1;
f[1]=1;
for(int i=2;i<=1e6;i++)
f[i]=(f[i-1]*i)%MOD;
while(scanf("%I64d%I64d%I64d",&a,&b,&n)!=EOF)
{
LL ans=0;
for(int i=0;i<=n;i++)
{
int num=a*i+b*(n-i);
if(is_excellent(num))
{
//DEBUG;
LL t=f[n];
t=(t*fastmod(f[i],MOD-2,MOD))%MOD;
t=(t*fastmod(f[n-i],MOD-2,MOD))%MOD;
ans=(ans+t)%MOD;
}
}
printf("%I64d\n",ans%MOD);
}
return 0;
}

最新文章

  1. Javascript之confirm的用法
  2. angularjs之$timeout指令
  3. android 电量分析工具
  4. JqGrid TreeView使用
  5. 自制单片机之一------AT89S51最小系统制做
  6. Laravel路由和控制器的绑定
  7. 【Win 10 应用开发】MIDI 音乐合成——乐理篇
  8. Linux指令--ps
  9. 用jquery实现日期控件
  10. vue 开发和生产的跨域问题
  11. OO第一单元单元总结
  12. 攻击图生成工具mulval的安装和配置
  13. day1-6 字符串、列表、元组、字典、类型转换
  14. etf基金和lof基金区别
  15. linux下mysql5.7以上my.cnf配置文件配置
  16. Reverse Words in a String leetcode java
  17. 如何提交代码到CEPH Repo。 顺便庆祝下,提交了第一个ceph pull request。实现了从0到1的突破
  18. day-17 L1和L2正则化的tensorflow示例
  19. 大聊Python----Select解析
  20. Compare, sort, and delete duplicate lines in Notepad ++

热门文章

  1. 常用的网络通信命令--write.wall.mesg.mail
  2. Python自动化测试框架——生成测试报告
  3. redis搭建配置
  4. JS提前声明和定义方式
  5. LeetCode(169)Majority Element
  6. hdu3376 Matrix Again
  7. PHPTaint-检测xss/sqli/shell注入的php扩展模块[转]
  8. 安装weblogic时,运行configure.cmd报错、闪退、无法创建域
  9. python蛋疼的编码decode、encode、unicode、str、byte的问题都在这了
  10. .net异步编程async和await的讨论收获