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 ...
随机推荐
- Java基础 - 面向对象 - 类方法传参
调用方法时可以给该方法传递一个或多个值,传给方法的值叫实参,在方法内部,接收实参的变量叫做形参,形参的声明语法与变量的声明语法一样.形参只在方法内部有效. Java中方法的参数主要有3种,分别为值参数 ...
- rest_framework 响应器
一 作用 根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件.用户请求URL: http://127.0.0.1:8000/test/?format=json http:/ ...
- Linux运维工程师:30道面试题整理
1.linux 如何挂在 windows 下的共享目录 mount.cifs //192.168.1.3/server /mnt/server -o user=administrator,pass=1 ...
- ThreadLocal 示例
ThreadLocal, 从字面意思上看是本地线程. 但实际上它是一个线程本地变量.它的功能就是为每一个使用该变量的线程都提供一个变量值的副本, 从而使得不会与其他线程的副本冲突. 与使用synchr ...
- STL sort “invalid operator <”
跟踪了下,是比较函数(下面的_Pred)的问题: template<class _Pr, class _Ty1, class _Ty2> inline bool _Debug_lt_pre ...
- 搭建自己的npm仓库
第一步:安装Erlang环境 首先,安装必要的库 yum install build-essential yum install libncurses5-dev yum install libssl- ...
- windows下线程库的使用
下载PTHREAD的WINDOWS开发包 pthreads-w32-2-4-0-release.exe(任何一个版本均可) http://sourceware.org/pthreads-win32 ...
- js实现删除弹框确认
JSP页面代码如下: <%@ page language="java" contentType="text/html; charset=UTF-8"%&g ...
- 软件测试工程师的Linux之路(持续更新修正)
软件测试工程师成长必经之路—Linux学习. 测试工程师不同于运维工程师,所以在对不熟悉Linux的测试人员来说,先了解一些Linux系统的基本操作,能顺利开展测试工作即可,在强迫自己使用,熟悉命令行 ...
- Spring 之 @ComponentScan以及mock Spring MVC
[ @ComponentScan] 纠正:可以成功 Autowired 的原因是我在另外一个 config 文件中扫描了根包,这会顺带扫描所有该包的子包 还有,,上面的写法容易出错,建议这样写, @C ...