Codeforces Round #181 (Div. 2)C
用lucas定理, p必须是素数
对于单独的C(n, m) mod p,已知C(n, m) mod p = n!/(m!(n - m)!) mod p。显然除法取模,这里要用到m!(n-m)!的逆元。
根据费马小定理:
已知(a, p) = 1,则 ap-1 ≡ 1 (mod p), 所以 a*ap-2 ≡ 1 (mod p)。
也就是 (m!(n-m)!)的逆元为 (m!(n-m)!)p-2 ;
所以C(n, m) mod p = n! * (m! * (n - m)! )^(p-2)%mod
中间用快速幂取余
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; ll fac[N],a,b;
bool ok(ll x)
{
while(x){
if(x%!=a&&x%!=b)return ;
x/=;
}
return ;
}
ll quick(ll a,ll b)
{
ll ans=;
while(b){
if(b&)ans=ans*a%mod;
a=a*a%mod;
b/=;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
fac[]=fac[]=;
for(ll i=;i<N;i++)
fac[i]=fac[i-]*i%mod;
ll n,ans=;
cin>>a>>b>>n;
for(ll i=;i<=n;i++)
if(ok(a*i+(n-i)*b))
ans=(ans+fac[n]*quick(fac[i]*fac[n-i]%mod,mod-)%mod)%mod;
cout<<ans<<endl;
return ;
}
/******************** ********************/
Codeforces Round #181 (Div. 2)C的更多相关文章
- Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力
C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Codeforces Round #181 (Div. 2) A. Array 构造
A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...
- Codeforces Round #181 (Div. 2)
A. Array 模拟. B. Coach 模拟. C. Beautiful Numbers good number的位和最大不超过\(10^7\),那么只要枚举a或b的个数,然后最多循环7次判断位和 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- docker的安装以及jdk和tomcat的环境配置
准备工作:需要Linux kernel 3.8支持查看linux内核的版本:root@ubuntu-dev:~# cat /proc/version查看linux版本:root@ubuntu-dev: ...
- BioinfomaticsPPT-1-Introduction
1.人类基因基础 2.基因组规模 3.基因分化表达 4.遗传信息流 5.基因基础知识相关 6.遗传编码 7.序列比对的意义 8.
- ngs中reads mapping-pku的生信课程
4.NGS中的reads mapping 顾名思义,就是将测序的得到的DNA定位在基因组上. 因为二代测序的得到的序列是较短的,reads mapping很好地解决了这个问题. 本质上reads ma ...
- PAT 1081 Rational Sum[分子求和][比较]
1081 Rational Sum (20 分) Given N rational numbers in the form numerator/denominator, you are suppose ...
- day3-python-文件操作(1)
本文内容涉及python打开/创建文件对象,文件的读写.文件指针位置的移动.获取命令行参数. 1. open()open函数以指定模式返回一个file对象,如: file_object = open( ...
- Python(文件处理)
二.基本操作 #r''------------------>> r:原生字符串,不判断符号的含义#文件处理 f=open(r’c:\a.txt’,’r’,encoding=’utf-8’) ...
- 解释一下python中的//,%和**运算符
//运算符执行地板除法(向下取整除),它会返回整除结果的整数部分 print(7//2) 这里整除后会返回3.5 同样的,执行取幂运算,ab会返回a的b次方 print(2**10) 最后,%执行取模 ...
- __name__ = '__main__'有什么用
很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...
- PHP 留言板练习
登录页面同session一样 login页面 <form action="loginchuli.php" method="post"> <di ...
- Java 基础总结(一)
本文参见:http://www.cnblogs.com/dolphin0520/category/361055.html 1. String,StringBuffer,StringBuilder 1) ...