2016夏季练习——数论
2016-07-14 11:13
295 查看
来源:HDU3037
Lucas定理模板题
中间的取逆元是费马小定理
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 100000+10;
LL n,m,p;
LL MOD;
LL fac[MAXN];
void ini(){
MOD=p;
fac[0]=1;
for(int i=1;i<=p;i++)
fac[i]=fac[i-1]*i%MOD;
}
LL quick_pow(LL a,LL n){
LL res=1;
LL temp=a%MOD;
while(n){
if(n&1) res=(res*temp)%MOD;
temp=temp*temp%MOD;
n>>=1;
}
return res;
}
int combination(LL n,LL k){
if(k>n) return 0;
return fac
*quick_pow(fac[k]*fac[n-k],p-2)%MOD;//Fermat ans p is a prime
}
LL lucas(LL n,LL k){
if(k==0) return 1;
else
return (combination(n%p,k%p)*lucas(n/p,k/p))%p;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&p);
ini();
cout<<lucas(m+n,n)<<endl;
}
return 0;
}
Lucas定理模板题
中间的取逆元是费马小定理
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 100000+10;
LL n,m,p;
LL MOD;
LL fac[MAXN];
void ini(){
MOD=p;
fac[0]=1;
for(int i=1;i<=p;i++)
fac[i]=fac[i-1]*i%MOD;
}
LL quick_pow(LL a,LL n){
LL res=1;
LL temp=a%MOD;
while(n){
if(n&1) res=(res*temp)%MOD;
temp=temp*temp%MOD;
n>>=1;
}
return res;
}
int combination(LL n,LL k){
if(k>n) return 0;
return fac
*quick_pow(fac[k]*fac[n-k],p-2)%MOD;//Fermat ans p is a prime
}
LL lucas(LL n,LL k){
if(k==0) return 1;
else
return (combination(n%p,k%p)*lucas(n/p,k/p))%p;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&p);
ini();
cout<<lucas(m+n,n)<<endl;
}
return 0;
}
相关文章推荐
- [LeetCode] Counting Bits 计数位
- Ubuntu 之Android 开发配置大全『更新』
- HMTL笔记——Iframe
- CentOS6.3 编译安装LAMP(2):编译安装 Apache2.4.6
- PHP输出XML文件函数
- pop()实现逐个删除数组最后一位并输出
- C# 利用发射动态创建泛型类型的对象,泛型类型支持带惨的构造函数
- CodeForces 688D-Remainders Game
- android 动态补丁技术
- Android提高第四篇之Activity+Intent
- Android的xml布局文件代码讲解(TextView控件)
- Mesos和Kubernetes比较
- Android 的蓝牙简介
- Serializable源码分析笔记
- Sql Server Md5使用
- navigationBar随着tableview滑动透明度渐变
- APP切图详细规范终极指南
- node 学习笔记 - fs 文件操作
- java集合分类大全
- iOS复习知识点