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 ...
随机推荐
- Qt——信号与槽
自定义槽函数,槽函数可以是任何位置定义的函数(成员函数,静态函数,全局函数),但是槽函数的函数类型要和信号的函数类型一致.
- Java+Selenium3自动化测试框架设计系列--href="javascript:void(0)"如何获得元素定位
经过前面两篇文章的铺 垫,我们这篇介绍,webdriver如何处理,一个浏览器上多个窗口之间切换的问题.我们先脑补这样一个测试场景,你在页面A点击一个连接,会在新的 tab窗口打开页面B,这个时候,你 ...
- go学习笔记-基础类型
基础类型 布尔值 布尔值的类型为bool,值是true或false,默认为false. //示例代码 var isActive bool // 全局变量声明 var enabled, disabled ...
- 简单整理React的Context API
之前做项目时经常会遇到某个组件需要传递方法或者数据到其内部的某个子组件,中间跨越了甚至三四层组件,必须层层传递,一不小心哪层组件忘记传递下去了就不行.然而我们的项目其实并没有那么复杂,所以也没有使用r ...
- IDEA Java Web(Spring)项目从创建到打包(war)
创建Maven管理的Java Web应用 创建新项目,"create new project",左侧类型选择"maven",右侧上方选择自己的SDK,点击&qu ...
- sudo mount -o loop pm.img /mnt/floppy
sudo mount -o loop pm.img /mnt/floppy 最近在学<一个操作系统的实现>,由于这本书比较老了,所以有一些对于软盘的操作指令现在用会出现一些错误,当我进行虚 ...
- django序列化时间
具体代码: import json,time,datetime lis ={'time':datetime.date.today(),"username":"zhilei ...
- javac一次性编译多个包下的.java文件
如题是我想要知道的,然后在网上搜了一下 下面是在某些帖子里看到别人说的只言片语 =========================================================== ...
- iOS中的数据库应用
iOS中的数据库应用 SLQLite简介 什么是SQLite SQLite是一款轻型的嵌入式数据库 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 它的处理速度比Mysql.Post ...
- 「暑期训练」「基础DP」 Piggy-Bank (HDU-1114)
题意与分析 完全背包问题. 算法背包九讲里面都有提到过,我自己再说下对完全背包的理解. 为什么01背包中遍历状态从VV到00?考虑一下基本方程$dp[i][j]=max(dp[i-1][j-w[i]] ...