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 ...
随机推荐
- python2.7练习小例子(十八)
19):题目:一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数. #!/usr/bin/python # -*- ...
- Android开发——告诉你Adapter应该写在Activity里面还是外面
0. 前言 本文转载自AItsuki的博客. 首先说明一下为什么要写这么一篇博客:最近看了一些其他人的项目,发现很多项目的做法是建立一个专门存放Adapter类的Package包,也有的项目干脆直接都 ...
- 详解jQuery中 .bind() vs .live() vs .delegate() vs .on() 的区别
转载自:http://zhuzhichao.com/2013/12/differences-between-jquery-bind-vs-live/ 我见过很多开发者很困惑关于jQuery中的.bin ...
- 实用脚本 1 -- 安装Ctags
Ctags是vim下方便代码阅读的工具,一般VIM中已经默认安装了Ctags,它可以帮助程序员很容易地浏览源代码. 1.如果系统中没有此工具用如下方法安装: 到ctags官网下载源码,解压后 ...
- linux-clone-ip处理办法
vim /etc/udev/rules.d/70-persistent-net.rules 步骤1:#将eth0相关的文件给删除 步骤2:#vi /etc/sysconfig/network-scri ...
- 指纹识别人脸识别 iOS
//1.判断iOS8及以后的版本 if([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){ //从iPhone5S开始,出现指 ...
- [转]Git,SVN的优缺点及适合的范围,开源项目?公司项目?
使用git不久,粗浅理解: 1)适用对象不同.Git适用于参与开源项目的开发者.他们由于水平高,更在乎的是效率而不是易用性.Svn则不同,它适合普通的公司开发团队.使用起来更加容易. 2)使用的场合不 ...
- jackson 处理空值
@JsonInclude(value=Include.NON_NULL) public class ResultBean 这样在返回数据的时候, { "code": "s ...
- Mysql性能优化三:主从配置,读写分离
大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢 ...
- Flask 学习笔记(一)
一.Web 服务器与 Web 框架 首先明确一下,要运行一个动态网页,我们需要 一个 Web 服务器来监听并响应请求,如果请求的是静态文件它就直接将其返回,如果是动态 url 它就将请求转交给 Web ...