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).

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+; bool excellent(int x)
{
while(x>=)
{
if(x%!=a&&x%!=b)
break;
x/=;
}
if(x>)
return ;
else
return ;
} long long powerMod(long long a,long long b,long long c) ///(a^b)%c
{
long long ans=;
a=a%c;
while(b>)
{
if(b%==)
ans=(ans*a)%c;
b=b/;
a=(a*a)%c;
}
return ans ;
} LL mult[];
void multi()
{
LL res=;
mult[]=;
for(int i=;i<=;i++)
mult[i]=(mult[i-]*i)%M;
} long long combine(int i,int n)
{
LL n1=mult[n];
LL n2=mult[i];
n2=(n2*mult[n-i])%M;
n2=powerMod(n2,M-,M);
n2=(n2*n1)%M;
return n2;
} int main()
{
long long ans=;
multi();
scanf("%d%d%d",&a,&b,&n); ///n为数字长度
for(int i=;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 ;
}

codeforces_300C_组合数_快速幂的更多相关文章

  1. BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS

    BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS 题意: 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p, ...

  2. Gym - 101775A Chat Group 组合数+逆元+快速幂

    It is said that a dormitory with 6 persons has 7 chat groups ^_^. But the number can be even larger: ...

  3. [CSP-S模拟测试]:涂色游戏(DP+组合数+矩阵快速幂)

    题目描述 小$A$和小$B$在做游戏.他们找到了一个$n$行$m$列呈网格状的画板.小$A$拿出了$p$支不同颜色的画笔,开始在上面涂色.看到小$A$涂好的画板,小$B$觉得颜色太单调了,于是把画板擦 ...

  4. 【2021 ICPC Asia Jinan 区域赛】 C Optimal Strategy推公式-组合数-逆元快速幂

    题目链接 题目详情 (pintia.cn) 题目 题意 有n个物品在他们面前,编号从1自n.两人轮流移走物品.在移动中,玩家选择未被拿走的物品并将其拿走.当所有物品被拿走时,游戏就结束了.任何一个玩家 ...

  5. Luogu P1226 取余运算||快速幂_快速幂

    超短代码 #include<iostream> #include<cstdio> using namespace std; long long b,p,k; long long ...

  6. 51nod 1835 - 完全图 - [dp][组合数公式][快速幂]

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1835 基准时间限制:1 秒 空间限制:131072 KB   ...

  7. [poj3070]Fibonacci_矩乘_快速幂

    Fibonacci poj-3070 题目大意:求Fibonacci第n项. 注释:模数为10000,$1\le n \le 10^9$. 想法:矩阵题,用例题6的想法,我们构造矩阵 $\begin{ ...

  8. 【Luogu】P3414组合数(快速幂)

    题目链接 从n的元素中选零个,选一个,选两个,选三个...选n个的方案数和,其实就是n个元素中取任意多个元素的方案数,那对于每一个元素,都有取或不取两种情况,所以方案数最终为2^n个. #includ ...

  9. [bzoj2467][中山市选2010]生成树_快速幂

    生成树 bzoj-2467 中山市选2010 题目大意:题目链接 注释:略. 想法:首先,考虑生成树的性质.每两个点之间有且只有一条路径.我们将每个五边形的5条边分为外面的4条边和内部的一条边,在此简 ...

随机推荐

  1. Android Toast小解

    简单介绍:Toast英文含义是吐司,在Android中.它就像烘烤机里做好的吐司弹出来,并持续一小段时间后慢慢消失. Toast也是一个容器,能够包括各种View,并承载着它们显示. Android中 ...

  2. android不是内部或外部命令,也不是可执行的程序或批处理文件

    问题: 原因:没有配置好android sdk环境变量 解决方法: (1)切换到android sdk下的tools文件夹,再执行android命令就可以启动Android SDK管理器,我的andr ...

  3. 数据结构 - 希尔排序(Shell&#39;s Sort) 具体解释 及 代码(C++)

    数据结构 - 希尔排序(Shell's Sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2 ...

  4. BZOJ1202 [HNOI2005]狡猾的商人 并查集维护前缀和

    1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1935  Solved: 936[Submit][Stat ...

  5. java8新特性-方法引用

    方法引用:若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用 (可以将方法引用理解为 Lambda 表达式的另外一种表现形式) 1. 对象的引用 :: 实例方法名2. 类名 :: 静 ...

  6. fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt

    project->xx Properties->Manifest->Input and Output->Embed Manifest将yes修改为no

  7. POJ 3100:Root of the Problem

    Root of the Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12060   Accepted: 6 ...

  8. oracle 统计/分析函数

    Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,而聚合函数对于每个组只返回一行. 语法: Sql代码 <analytic ...

  9. 观光公交 2011年NOIP全国联赛提高组(贪心,递推)

    观光公交 2011年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold       题目描述 Description 风景迷人的小城 Y 市 ...

  10. 公司5:JrVue表格

    组件名称:jr-dynamic-query-table 组件布局 table组件名称:  jr-dynamic-query-table 分页组件名称: el-pagination <div re ...