hdu 3944 lucas
2015-10-24 13:03
274 查看
需要分情况讨论。
然后求组合数 套上lucas。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, k, p;
ll powmod(ll a, ll b){ ll res = 1; a %= p; while(b){ if(b&1) res = res*a%p; b>>=1; a = a*a%p; } return res; }
ll fac[1310][10100];
bool is[100010];
int cnt;
int pr[10010];
int id[10010];
void init(){
cnt = 0;
for(int i=2; i<=10000; i++){
if(!is[i]){
pr[++cnt] = i;
id[i] = cnt;
for(int j=i+i; j<=10000; j+=i)
is[j] = true;
}
}
for(int i=1; i<=cnt; i++){
fac[i][0] = 1;
for(int j=1; j<=10000; j++){
fac[i][j] = fac[i][j-1] * j % pr[i];
}
}
}
ll getc(int n, int m, ll p, int tmp){
if(m > n) return 0;
ll res = fac[tmp]
*powmod(fac[tmp][n-m]*fac[tmp][m]%p, p-2)%p;
return res;
}
ll lucas(int n, int m, ll p, int tmp){
if(m == 0) return 1;
return lucas(n/p, m/p, p, tmp)*getc(n%p, m%p, p, tmp)%p;
}
int main(){
int icase = 0;
init();
while(~scanf("%lld %lld %lld", &n, &k, &p)){
int tmp = id[p];
if(k > n/2) k = n- k;
ll res = lucas(n+1, k, p, tmp);
printf("Case #%d: %lld\n", ++icase, (res + n - k)%p);
}
return 0;
}
然后求组合数 套上lucas。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, k, p;
ll powmod(ll a, ll b){ ll res = 1; a %= p; while(b){ if(b&1) res = res*a%p; b>>=1; a = a*a%p; } return res; }
ll fac[1310][10100];
bool is[100010];
int cnt;
int pr[10010];
int id[10010];
void init(){
cnt = 0;
for(int i=2; i<=10000; i++){
if(!is[i]){
pr[++cnt] = i;
id[i] = cnt;
for(int j=i+i; j<=10000; j+=i)
is[j] = true;
}
}
for(int i=1; i<=cnt; i++){
fac[i][0] = 1;
for(int j=1; j<=10000; j++){
fac[i][j] = fac[i][j-1] * j % pr[i];
}
}
}
ll getc(int n, int m, ll p, int tmp){
if(m > n) return 0;
ll res = fac[tmp]
*powmod(fac[tmp][n-m]*fac[tmp][m]%p, p-2)%p;
return res;
}
ll lucas(int n, int m, ll p, int tmp){
if(m == 0) return 1;
return lucas(n/p, m/p, p, tmp)*getc(n%p, m%p, p, tmp)%p;
}
int main(){
int icase = 0;
init();
while(~scanf("%lld %lld %lld", &n, &k, &p)){
int tmp = id[p];
if(k > n/2) k = n- k;
ll res = lucas(n+1, k, p, tmp);
printf("Case #%d: %lld\n", ++icase, (res + n - k)%p);
}
return 0;
}
相关文章推荐
- html中input文本框,初始里边有文字提示,当点击时,文字消失
- html中input文本框,初始里边有文字提示,当点击时,文字消失
- 【C语言】【笔试题】编写函数: unsigned int reverse_bit(unsigned int value); 翻转二进制序列
- centos rpm 安装mysql5.7
- HDU 3622 Bomb Game
- Python if elif else语句
- oracle性能优化之表设计
- 【C语言】【笔试题】两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同
- 九度OJ 1150:Counterfeit Dollar(假美元) (分析、检验)
- codevs1376帕秋莉•诺蕾姬 东方幻想乡系列模拟赛Stage 2[三星]
- Hbase结构简单、作法
- 九度OJ 1150:Counterfeit Dollar(假美元) (分析、检验)
- 地图定位
- Android属性动画完全解析(二)
- uoj #139. 【UER #4】被删除的黑白树 dfs序 贪心
- 对于sizeof的含义、应用及易混点的解析
- hibernate HQL new xxx() 查询 join连接多个表,并把连接后的表保存到一个新类中
- android----Timer和TimerTask的使用
- Apache Mesos【一】Apache Mesos的整体架构
- android-----用代码安装apk文件