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条边和内部的一条边,在此简 ...
随机推荐
- C - The C Answer (2nd Edition) - Exercise 1-5
/* Modify the temperature conversion program to print the table in reverse order, that is, from 300 ...
- 关于微软网站维护培训时遇到的issues:What is WEDCS?
WEDCS (pronounced “wed-ex”), the Web Events Data Collection Service, is a system for collecting, com ...
- UVA10056 - What is the Probability ?(概率)
UVA10056 - What is the Probability ? (概率) 题目链接 题目大意:有n个人玩游戏,一直到一个人胜出之后游戏就能够结束,要不然就一直从第1个到第n个循环进行,没人一 ...
- 2016/1/18 Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- 00020970-0000-0000-C000-000000000046
00020970-0000-0000-C000-000000000046 System.InvalidCastException: 无法将类型为“Microsoft.Office.Interop.Wo ...
- YTU 2642: 填空题:类模板---求数组的最大值
2642: 填空题:类模板---求数组的最大值 时间限制: 1 Sec 内存限制: 128 MB 提交: 646 解决: 446 题目描述 类模板---求数组的最大值 找出一个数组中的元 ...
- luogu 4429 染色
bjoi 2018 染色 推了个错误结论得了60分? 题目大意: 一个无重边和自环的无向图,并且对每个点分别给了一个大小为2的颜色集合,只能从这个集合中选一种颜色给这个点染色 求一个染色方案使得没有两 ...
- Flask的flask-sqlalchemy
flask-sqlalchemy是Flask和SQLAlchemy的管理者 - db = SQLAlchemy() - 包含配置 - 包含ORM基类 - 包含create_all - engine - ...
- SPOJ OTOCI 动态树 LCT
SPOJ OTOCI 裸的动态树问题. 回顾一下我们对树的认识. 最初,它是一个连通的无向的无环的图,然后我们发现由一个根出发进行BFS 会出现层次分明的树状图形. 然后根据树的递归和层次性质,我们得 ...
- Rails5 radio_button
容易错,集中记下来 首先是radio button的三种形式 函数名 参数意义 radio_button_tag(prop, value [, opts]) prop: radio的属性 v ...