用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的更多相关文章

  1. Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...

  2. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  3. Codeforces Round #181 (Div. 2) A. Array 构造

    A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...

  4. Codeforces Round #181 (Div. 2)

    A. Array 模拟. B. Coach 模拟. C. Beautiful Numbers good number的位和最大不超过\(10^7\),那么只要枚举a或b的个数,然后最多循环7次判断位和 ...

  5. 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 ...

  6. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  7. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  8. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  9. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

随机推荐

  1. 三.实例演示insert/update/delect更新数据库

    1.逻辑图 2.只是准备 3.代码展示 import pymysql conn=pymysql.connect( host='192.168.199.249', port=3306, user='ro ...

  2. requests 中response如何改变编码格式

    查看初始编码 首先查看拿到的response编码格式: (就不放代码了,因为此例需要用到cookie,可自行找个网站具体测试) 可见初始编码为:ISO-8859-1 修改编码 初始编码: 修改后编码: ...

  3. mysql分组取每组前几条记录(排名)

    1.创建表 create table tb( name varchar(10), val int, memo varchar(20) ); 2.插入数据 insert into tb values(' ...

  4. 我的Android进阶之旅------>解决错误:You need to use a Theme.AppCompat theme (or descendant) with this activity.

    #1.错误描述 今天,想实现Activity不显示标题栏的效果,在项目的AndroidManifest.xml文件,对相应的Activity添加属性 android:theme="@andr ...

  5. Keras之序贯(Sequential)模型

    序贯模型(Sequential) 序贯模型是多个网络层的线性堆叠. 可以通过向Sequential模型传递一个layer的list来构造该模型: from Keras.models import Se ...

  6. 用Kotlin开发Android的Hello Kotlin!!

    1 创建新项目 android studio 新建一个项目 只有一个空MainActivity 2 安装kotlin file-seeting-plugins-install jetbrains pl ...

  7. jsp创建cookie

    <jsp:include flush="true" page="header.jsp" /> <script type="text/ ...

  8. bootstrap基本使用

    bootstrap是封装了css和js代码实现酷炫的效果,所以使用的时候,比如说是列表效果,直接调用它本身定义的函数就ok了 静态文件 把href='..static/..'里面改为url_for静态 ...

  9. day8 一些字符转换和深浅拷贝 和枚举

    # li =[11,22,33,44,55,66,77,88]# del li[-2::-5]# print(li)# # dic = {'k1':'barry','k2':'alex','name' ...

  10. 杭电1024Max Sum Plus Plus

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题目: Problem Description Now I think you have got a ...