codeforces_300C_组合数_快速幂
2 seconds
256 megabytes
standard input
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.
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
思路:遍历数中的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_组合数_快速幂的更多相关文章
- BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS
BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS 题意: 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p, ...
- Gym - 101775A Chat Group 组合数+逆元+快速幂
It is said that a dormitory with 6 persons has 7 chat groups ^_^. But the number can be even larger: ...
- [CSP-S模拟测试]:涂色游戏(DP+组合数+矩阵快速幂)
题目描述 小$A$和小$B$在做游戏.他们找到了一个$n$行$m$列呈网格状的画板.小$A$拿出了$p$支不同颜色的画笔,开始在上面涂色.看到小$A$涂好的画板,小$B$觉得颜色太单调了,于是把画板擦 ...
- 【2021 ICPC Asia Jinan 区域赛】 C Optimal Strategy推公式-组合数-逆元快速幂
题目链接 题目详情 (pintia.cn) 题目 题意 有n个物品在他们面前,编号从1自n.两人轮流移走物品.在移动中,玩家选择未被拿走的物品并将其拿走.当所有物品被拿走时,游戏就结束了.任何一个玩家 ...
- Luogu P1226 取余运算||快速幂_快速幂
超短代码 #include<iostream> #include<cstdio> using namespace std; long long b,p,k; long long ...
- 51nod 1835 - 完全图 - [dp][组合数公式][快速幂]
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1835 基准时间限制:1 秒 空间限制:131072 KB ...
- [poj3070]Fibonacci_矩乘_快速幂
Fibonacci poj-3070 题目大意:求Fibonacci第n项. 注释:模数为10000,$1\le n \le 10^9$. 想法:矩阵题,用例题6的想法,我们构造矩阵 $\begin{ ...
- 【Luogu】P3414组合数(快速幂)
题目链接 从n的元素中选零个,选一个,选两个,选三个...选n个的方案数和,其实就是n个元素中取任意多个元素的方案数,那对于每一个元素,都有取或不取两种情况,所以方案数最终为2^n个. #includ ...
- [bzoj2467][中山市选2010]生成树_快速幂
生成树 bzoj-2467 中山市选2010 题目大意:题目链接 注释:略. 想法:首先,考虑生成树的性质.每两个点之间有且只有一条路径.我们将每个五边形的5条边分为外面的4条边和内部的一条边,在此简 ...
随机推荐
- Java基础笔记(一)
本文主要是我在看<疯狂Java讲义>时的读书笔记,阅读的比较仓促,就用 markdown 写了个概要. 第一章 Java概述 Java SE:(Java Platform, Standar ...
- SSH-struts2的异常处理
在学习j2se的时候学习过利用java的exception类去处理异常.在struts2框架中也提供了对于异常的处理.简单说就是当Action发生异常时.能够在struts2.xml文件里进行配置,将 ...
- Write a program that gives count of common characters presented in an array of strings..(or array of
转自出处 Write a program that gives count of common characters presented in an array of strings..(or arr ...
- web 开发之js---ajax cgi异步调试
xmlhttp.onreadystatechange=function() { //alert(xmlhttp.readyState); //alert(xmlhttp.status); if (xm ...
- Android小技巧总结1
0.Android彻底退出引用程序的方法 1) Dalvik VM的本地方法 android.os.Process.killProcess(android.os.Process.myPid()); / ...
- [计算机故障]toshiba笔记本计算机无法正常启动,每次均需要按ESC
同事一台toshiba的笔记本计算机,无法正常启动,每次均需要按ESC才可以正常后续动作. 之后系统可以正常工作. 排查过程: 1.尝试恢复bios到默认配置:不行,而且不小心搞了个蓝屏,还好记得是“ ...
- linux select poll and epoll
这里以socket文件来阐述它们之间的区别,假设现在服务器端有100 000个连接,即已经创建了100 000个socket. 1 select和poll 在我们的线程中,我们会弄一个死循环,在循环里 ...
- input keyevent发送按键值【转】
本文转载自:http://blog.csdn.net/moyu123456789/article/details/71209893 1.adb shell进入android设备,执行命令input k ...
- 利用JFreeChart生成时序图(6) (转自 JSP开发技术大全)
利用JFreeChart生成时序图(6) (转自 JSP开发技术大全) 14.6 利用JFreeChart生成时序图 通过JFreeChart插件只能生成普通效果的时序图,利用工厂类ChartFact ...
- Python不兼容问题
今天遇到了一个Python2与3不兼容的坑. ride是基于robot框架的python自动化ui,但它只支持python2,而我电脑环境只有python3,想跑别人基于ride编写的测试用例,折腾了 ...