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 ...
随机推荐
- 学习RUNOOB.COM进度二
MongoDB 概念解析 SQL术语/概念 MongoDB术语/概念 解释/说明 database database 数据库 table collection 数据库表/集合 row document ...
- rhel6.4扩充swap分区
状况:Red hat 6.4 swap分区不足 解决:扩充swap ================================================================== ...
- LeetCode:27. Remove Element(Easy)
1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...
- 如何理解Java中参数传递只能传值?
以前学习C#的时候,是完全在工作岗位上学习,一些底层较为深入的道理都不是很清楚.如今学习了Java,对于Java参数传递只能传值,不能传引用(指针)感到很困惑,在C#中不是常常说把某个引用传递到函数中 ...
- Oracle数据库抽数神器toad
使用了toad,再也不怕抽数成各种 文件格式,以及添加分割的数据文件了.百度搜toad,
- CSS实现自适应下保持宽高比
在项目中,我们可能经常使得自己设计的网页能自适应.特别是网站中的图片,经常要求在网页放大(或缩小)时,宽高同时放大(或缩小),而且不变形(即保持正常的长宽比).为了不变形,常用的方法就是设置width ...
- react错误总结
react-native 错误总结 The development server returned response error code: 500 in react-native https://b ...
- nvm版本管理工具安装
windows 安装nvm步骤(shi'yongnvm-windows管理node版本): 瞎几把前言:mac上可以用n来管理node版本,私以为n很好用.家里的win7台式机一直没有安装过任何管理工 ...
- 计算机概念总结5-阿里云的了解-ecs
1.ecs 1.1ecs 云服务器Elastic Compute Service(ECS)是阿里云提供的一种基础云计算服务.使用云服务器ECS就像使用水.电.煤气等资源一样便捷.高效.您无需提前采购硬 ...
- C++STL——概述
一.相关介绍 STL 标准模板库 在编写代码的过程中有一些程序经常会被用到,而且需求特别稳定,所以C++中把这些常用的模板做了统一的规范,慢慢的就形成了STL 提供三种类型的组件: 容器.迭代器和算法 ...