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 ...
随机推荐
- 3、Java并发编程:Thread类的使用
Java并发编程:Thread类的使用 在前面2篇文章分别讲到了线程和进程的由来.以及如何在Java中怎么创建线程和进程.今天我们来学习一下Thread类,在学习Thread类之前,先介绍与线程相关知 ...
- MySQL数据库性能优化专题
摘录: 书:<MySQL性能调优与架构设计> 一个系列: (按顺序排一下) MySQL 数据库性能优化之缓存参数优化 http://isky000.com/database/mysql-p ...
- Linux上Makefile管理java项目
前面文章讲到了Linux上通过.spec文件与rpmbuild命令将java程序打包为RPM安装包, 现阶段遇到新的需求: 使用Makefile来操纵java的编译.打包 该需求以前面的内容为基础 可 ...
- jmeter动态获取jsessionid
思想是在一个线程组内添加一个cookie管理器,登录之后,用正则提取到sessionid,该线程组下的操作便可以共享这个session了. 1. 依次新建线程组.cookie管理器.http请求-登录 ...
- C++ STL容器——stack用法介绍
stack是一种容器适配器,专门设计用于在LIFO上下文中操作(后进先出),其中元素仅从容器的一端插入和删除. 容器适配器,而不是一种容器. 它是容器适配器是指,只要支持一系列方法的容器(empty, ...
- 21天学习caffe(二)
本文大致记录使用caffe的一次完整流程 Process 1 下载mnist数据集(数据量很小),解压放在data/mnist文件夹中:2 运行create_mnist.sh,生成lmdb格式的数据( ...
- Pro Git - 笔记3
Git Branching Branches in a Nutshell Branches in a Nutshell let’s assume that you have a directory c ...
- 关于Android Studio启动后自己的配置
根据Android Stduio自己设置的配置,我们在执行一些操作时可能不向教程那样,此时就要看教程上的Android Stduio的设置.
- linux下easy_install的安装与使用详解
Python中的easy_install工具用起来非常好用,它的作用类似于Php中的pear,或者Ruby中的gem,或者Perl中的cpan. 1.easy_install安装 如果想使用easy_ ...
- Java作业09-异常
6. 为如下代码加上异常处理 byte[] content = null; FileInputStream fis = new FileInputStream("testfis.txt&qu ...