CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+高速幂)
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和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;
}
CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+高速幂)的更多相关文章
- BZOJ_[HNOI2008]_Cards_(置换+Burnside引理+乘法逆元+费马小定理+快速幂)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1004 共n个卡片,染成r,b,g三种颜色,每种颜色的个数有规定.给出一些置换,可以由置换得到的 ...
- hdu1576-A/B-(同余定理+乘法逆元+费马小定理+快速幂)
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- codeforces div2_604 E. Beautiful Mirrors(期望+费马小定理)
题目链接:https://codeforces.com/contest/1265/problem/E 题意:有n面镜子,你现从第一面镜子开始询问,每次问镜子"今天我是否美丽",每天 ...
- 51nod A 魔法部落(逆元费马小定理)
A 魔法部落 小Biu所在的部落是一个魔法部落,部落中一共有n+1个人,小Biu是魔法部落中最菜的,所以他的魔力值为1,魔法部落中n个人的魔法值都不相同,第一个人的魔法值是小Biu的3倍,第二个人的魔 ...
- Codeforces.919E.Congruence Equation(同余 费马小定理)
题目链接 \(Description\) 给定a,b,x,p,求[1,x]中满足n*a^n ≡b (mod p) 的n的个数.\(1<=a,b<p\), \(p<=1e6+3\), ...
- HDU4549 M斐波那契数列 —— 斐波那契、费马小定理、矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4549 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...
- Codeforces Round #460 (Div. 2).E 费马小定理+中国剩余定理
E. Congruence Equation time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- UVA10200-Prime Time/HDU2161-Primes,例题讲解,牛逼的费马小定理和欧拉函数判素数。
10200 - Prime Time 此题极坑(本菜太弱),鉴定完毕,9遍过. 题意:很简单的求一个区间 ...
- Codeforces 300C Beautiful Numbers 【组合数】+【逆元】
<题目链接> 题目大意: 给出a和b,如果一个数每一位都是a或b,那么我们称这个数为good,在good的基础上,如果这个数的每一位之和也是good,那么这个数是excellent.求长度 ...
随机推荐
- C语言学习13
快速排序 //快速排序 #include <stdio.h> void quicksort(int a[], int left, int right); void main() { ] = ...
- js的闭包中关于执行环境和作用链的理解
首先讲一讲执行环境: 执行环境按照字面上来理解就是指目前代码执行所在的环境. 当JavaScript代码执行的时候,会进入不同的执行上下文,这些执行上下文会构成了一个执行上下文栈(Execution ...
- mybatis完整sql调试
问题描述 在使用mybatis进行开发的时候,由于可以动态拼接sql,这样大大方便了我们.但是也有一定的问题,当我们动态sql拼接的块很多的时候,我们要想从*mapper.xml中直接找出完整的sql ...
- 修改centos的yum源为国内的源
1.安装Centos后默认的Yum源如下 ll /etc/yum.repos.d/ [root@localhost ~]# ll /etc/yum.repos.d/ total 32 -rw-r- ...
- jQuery+ajax城市联动
分享一下自己最近写的城市联动.技术使用ajax+jQuery实现. 首先请看前台的javascript代码. 以下是连个实现异步加载的方法. <script type="text/ja ...
- CodeForces 20 A+B
A - BerOS file system 水题不解释了,压缩斜杆.要注意最后没有斜杠. char a[105]; ...
- POJ1159:Palindrome【dp】
题目大意:给出一个字符串,问至少添加多少个字符才能使它成为回文串? 思路:很明显的方程是:dp[i][j]=min{dp[i+1][j],dp[i][j-1],dp[i+1][j-1](str[i]= ...
- 洛谷P1757 通天之分组背包
题目背景 直达通天路·小A历险记第二篇 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品相互冲突,现在,他想知道最大 ...
- 【收藏】下载Chrome商店插件的方法,万恶的gwd
以下是下载离线插件包的方法: 第一步: 每个Google Chrome扩展都有一个固定的ID,例如https://chrome.google.com/webstore/detail/bfbmjmiod ...
- 洛谷P2814 家谱(gen)
题目背景 现代的人对于本家族血统越来越感兴趣. 题目描述 给出充足的父子关系,请你编写程序找到某个人的最早的祖先. 输入输出格式 输入格式: 输入由多行组成,首先是一系列有关父子关系的描述,其中每一组 ...