bzoj1951 组合数取模 中国剩余定理
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int a[4]={2,3,4679,35617};
int p[36000],b[4],n,g,ans,i,j,x,y,mod=999911658;
int power(int a,int b){//快速幂
int c=1;
for(;b;b>>=1){
if(b&1) c=(ll)c*a%mod;
a=(ll)a*a%mod;
}
return c;
}
void exgcd(int a,int b,int &x,int &y){
if(!b) {x=1,y=0; return;}
exgcd(b,a%b,x,y);
int z=x; x=y; y=z-y*(a/b);
}
int inv(int a,int p){//求乘法逆元
int x,y;
exgcd(a,p,x,y);
return (x%p+p)%p;
}
int calc(int x,int mod){//求C(n,x)%mod的值
int ans=1,y,a,b;
for(y=n;x;x/=mod,y/=mod){//lucas定理
a=x%mod,b=y%mod;
ans=(ll)ans*p[b]%mod*inv(p[a],mod)%mod*inv(b<a?0:p[b-a],mod)%mod;//p[n]是n的阶乘取模mod的结果。
}
return ans;
}
int main(){
cin>>n>>g;
g%=mod+1;
if(!g) {cout<<"0\n"; return 0;}
for(p[0]=i=1;i<=a[3];i++) p[i]=(ll)p[i-1]*i%mod;//预处理n的阶乘(处理到取模数就可以了)
for(i=1;i*i<=n;i++)
if(n%i==0){
for(j=0;j<4;j++) b[j]=(b[j]+calc(i,a[j]))%a[j];
if(i*i!=n)
for(j=0;j<4;j++) b[j]=(b[j]+calc(n/i,a[j]))%a[j];
}
for(i=0;i<4;i++){
exgcd(mod/a[i],a[i],x,y);
ans=(ans+(ll)x*(mod/a[i])%mod*b[i])%mod;
}
ans=(ans+mod)%mod,mod++;
ans=power(g,ans);
cout<<ans<<endl;
return 0;
}
bzoj1951 组合数取模 中国剩余定理的更多相关文章
- 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数
typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...
- 组合数取模(lucas定理+CRT合并)(AC)
#include<bits/stdc++.h> #define re register #define int long long using namespace std; ; inlin ...
- 组合数取模及Lucas定理
引入: 组合数C(m,n)表示在m个不同的元素中取出n个元素(不要求有序),产生的方案数.定义式:C(m,n)=m!/(n!*(m-n)!)(并不会使用LaTex QAQ). 根据题目中对组合数的需要 ...
- 组合数取模介绍----Lucas定理介绍
转载https://www.cnblogs.com/fzl194/p/9095177.html 组合数取模方法总结(Lucas定理介绍) 1.当n,m都很小的时候可以利用杨辉三角直接求. C(n,m) ...
- 组合数取模&&Lucas定理题集
题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020 输出组合数C(n, m) mod p (1 ...
- 组合数取模Lucas定理及快速幂取模
组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1) , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- lucas定理解决大组合数取模
LL MyPow(LL a, LL b) { LL ret = ; while (b) { ) ret = ret * a % MOD; a = a * a % MOD; b >>= ; ...
- 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 ...
随机推荐
- Hello Swift
第一个Swift作品,HelloSwift,呵呵.你会把原来的项目用swift改写吗?
- crontab 总结
crontab -e --------- linux定时任务提示-bash: crontab: command not found 执行 crontab 命令如果报 command not found ...
- python MySQLdb连接mysql失败(转载)
最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...
- 关于一家大型互联网公司的.NET面试
上周去了一家大型的互联网公司去面试!四个面试官提的问题整理下!以后会注明答案! 1.关于垃圾回收的过程!GC的过程 其中包含:什么是根,Finalize与Dispose的区别,什么时候用到!IDisp ...
- 2018.7.24 Error Code
来不及解释了,写下再说 -------------------------------------------- SUCCESS = 0, RTC_SELFTEST_FAILED = 1, ...
- python_查找模块的方法
在python自带的Command Line中: 1. 查找一个模块拥有的方法 import 模块名 help(模块名) or dir(模块名) 2. 查找一个模块拥有的方法 import 模块名 h ...
- 一段tcl代码
#!/usr/bin/wish proc icanspeak {} { set name [.ent get] } { exec s $name } } label .lab -text " ...
- OpenCV教程【002 VideoCapture加载并播放视频】
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace c ...
- 【整理】2-SAT
2-satisfiability,我们一般将其缩写为 2-sat. 了解全名有助于我们对这个算法的理解. 百度翻译:‘satisfiability’---“可满足性,适定性”. “合取范式可满 ...
- SQL Server 学习系列之六
SQL Server 学习系列之六 SQL Server 学习系列之一(薪酬方案+基础) SQL Server 学习系列之二(日期格式问题) SQL Server 学习系列之三(SQL 关键字) SQ ...