codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula 扩展Lucas定理 扩展CRT
默默敲了一个下午,终于过了,
扩展Lucas是什么,就是对于模数p,p不是质数,但是不大,如果是1e9这种大数,可能没办法,
对于1000000之内的数是可以轻松解决的。
代码完全手写,直接写了扩展的中国剩余定理(普通的不会写)
题意:给你n,m,p 求C(n,m)%p
#include<cstring>
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm> #define ll long long
#define N 27
using namespace std;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} ll n,m,p,ans,Modulo;
ll prime[N],num[N],mod[N];
int tot; void get_factor(ll p)
{
int up=(int)sqrt(p);
for (int i=;i<=up;i++)
{
if (p%i==)
{
prime[++tot]=i,mod[tot]=;
while(p%i==)
{
p/=i;
num[tot]++;
mod[tot]*=i;
}
}
}
if (p>) num[++tot]=,prime[tot]=mod[tot]=p;
}
ll fast_pow(ll a,ll b,ll mod)
{
ll ans=;
while(b)
{
if (b&) (ans*=a)%=mod;
(a*=a)%=mod;
b>>=;
}
return ans;
}
ll Recursion(ll n,ll x)
{
if (!n) return ;
ll dw=;
for (ll i=;i<=mod[x];i++)
if (i%prime[x]!=) (dw*=i)%=mod[x];
ll res=fast_pow(dw,n/mod[x],mod[x]);
for (ll i=n/mod[x]*mod[x]+;i<=n;i++)
if (i%prime[x]!=) (res*=i%mod[x])%=mod[x];
return (res*Recursion(n/prime[x],x))%mod[x];
}
void Ex_gcd(ll a,ll b,ll &x,ll &y)
{
if (!b)
{
x=,y=;
return;
}
else
{
Ex_gcd(b,a%b,x,y);
ll t=x;x=y;y=t-a/b*y;
}
}
ll Inv(ll a,ll b)
{
ll x,y;
Ex_gcd(a,b,x,y);
if (x<) x+=b;
return x;
}
ll get_combination(ll x)
{
ll ans=Recursion(n,x),k=;
for (ll i=n;i;i/=prime[x]) k+=i/prime[x];
for (ll i=m;i;i/=prime[x]) k-=i/prime[x];
for (ll i=n-m;i;i/=prime[x]) k-=i/prime[x];
ans*=fast_pow(prime[x],k,mod[x]);
ans%=mod[x];
ll res1=Recursion(m,x),res2=Recursion(n-m,x);
ans*=Inv(res1,mod[x]),ans%=mod[x];
ans*=Inv(res2,mod[x]),ans%=mod[x];
return ans;
}
void combine(ll &a,ll &b,ll c,ll d)
{
ll inv=Inv(b,d)*(c-a)%d;
a=inv*b+a,b=b*d,a%=b;
}
int main()
{
freopen("fzy.in","r",stdin);
freopen("fzy.out","w",stdout); n=read(),m=read(),p=read();
get_factor(p);
ans=get_combination(),Modulo=mod[];
for (int i=;i<=tot;i++)
{
ll res=get_combination(i),new_mod=mod[i];
combine(ans,Modulo,res,new_mod);
}
printf("%lld\n",(ans%Modulo+Modulo)%Modulo);
}
codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula 扩展Lucas定理 扩展CRT的更多相关文章
- codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula【扩展lucas】
传送门 [题意]: 求C(n,k)%m,n<=108,k<=n,m<=106 [思路]: 扩展lucas定理+中国剩余定理 #include<cstdio> usi ...
- CF 2015 ICL, Finals, Div. 1 J. Ceizenpok’s formula [Lucas定理]
http://codeforces.com/gym/100633/problem/J Lucas定理P不是质数裸题 #include <iostream> #include <cst ...
- 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)
J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Ceizenpok’s formula Gym - 100633J 扩展Lucas定理 + 中国剩余定理
http://codeforces.com/gym/100633/problem/J 其实这个解法不难学的,不需要太多的数学.但是证明的话,我可能给不了严格的证明.可以看看这篇文章 http://ww ...
- GYM100633J. Ceizenpok’s formula 扩展lucas模板
J. Ceizenpok’s formula time limit per test 2.0 s memory limit per test 256 MB input standard input o ...
- 2015 ICL, Finals, Div. 2【ABFGJK】
[题外话:我......不补了......] 2015 ICL, Finals, Div. 2:http://codeforces.com/gym/100637 G. #TheDress[水] (st ...
- Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理
Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...
- [Codeforces 100633J]Ceizenpok’s formula
Description 题库链接 求 \[C_n^m \mod p\] \(1\leq m\leq n\leq 10^{18},2\leq p\leq 1000000\) Solution 一般的 \ ...
- codeforces Gym - 100633J Ceizenpok’s formula
拓展Lucas #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring ...
随机推荐
- P1078 文化之旅
P1078 文化之旅 题目描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一 种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不 同的国家 ...
- Linux下Expect 完成自动输入密码
今天要开发一个定时任务,然后加入cron列表中.但是有个问题摆在眼前,脚本的执行中需要输入数据库密码(貌似5.1版本以上不允许在-p后直接加密码,会报错) mysql -u root -p <& ...
- vue2.0 watch
类型:string | Function | Object vue官网解释: 一个对象,键是需要观察的表达式,值是对应回调函数.值也可以是方法名,或者包含选项的对象.Vue 实例将会在实例化时调用 $ ...
- Qt 使用irrlicht(鬼火)3D引擎
项目中需要加载简单的3D场景.资深老前辈推荐使用开源小巧的引擎irrlicht. 关于irrlicht,来之百度百科 Irrlicht引擎是一个用C++书写的高性能实时的3D引擎,可以应用于C++程序 ...
- 杀死 tomcat 进程的脚本
新建一个.sh 文件 把下面的内容复制进去.然后 把这个文件放到tomcat 的bin目录下在关闭tomcat 执行这个脚本. 可以解决 在关闭tomcat的时候 总是遗留一些tomcat进程没有结束 ...
- session、token、cookie的区别
token就是令牌,比如你授权(登录)一个程序时,他就是个依据,判断你是否已经授权该软件cookie就是写在客户端的一个txt文件,里面包括你登录信息之类的,这样你下次在登录某个网站,就会自动调用co ...
- HDU 3269 P2P File Sharing System(模拟)(2009 Asia Ningbo Regional Contest)
Problem Description Peer-to-peer(P2P) computing technology has been widely used on the Internet to e ...
- STL应用——hdu1702(队列+堆栈)
水题 练习一下堆栈和队列的使用 #include <iostream> #include <cstdio> #include <algorithm> #includ ...
- 程序运行bug查看
1.左击计算机进入管理,点击windows日志,查看程序信息. 可以方便看到报错信息.
- beta版本冲刺六
目录 组员情况 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最新成果 团队签入记 ...