博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GYM100633J. Ceizenpok’s formula 扩展lucas模板
阅读量:5149 次
发布时间:2019-06-13

本文共 3435 字,大约阅读时间需要 11 分钟。

J. Ceizenpok’s formula
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Dr. Ceizenp'ok from planet i1c5l became famous across the whole Universe thanks to his recent discovery — the Ceizenpok’s formula. This formula has only three arguments: n, k and m, and its value is a number of k-combinations of a set of n modulo m.

While the whole Universe is trying to guess what the formula is useful for, we need to automate its calculation.

Input

Single line contains three integers n, k, m, separated with spaces (1 ≤ n ≤ 1018, 0 ≤ k ≤ n, 2 ≤ m ≤ 1 000 000).

Output

Write the formula value for given arguments n, k, m.

Examples
Input
2 1 3
Output
2
Input
4 2 5
Output
1
 
 
 
【题解】
裸的扩展lucas + CRT,推荐博文http://blog.csdn.net/clove_unique/article/details/54571216
因为特殊情况k = 0的时候,我以为是0,加了个特判,结果应该是1,不用特判。。。。。。。。。。卡了我半个晚上
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #define min(a, b) ((a) < (b) ? (a) : (b))13 #define max(a, b) ((a) > (b) ? (a) : (b))14 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))15 template
16 inline void swap(T &a, T &b)17 {18 T tmp = a;a = b;b = tmp;19 }20 inline void read(long long &x)21 {22 x = 0;char ch = getchar(), c = ch;23 while(ch < '0' || ch > '9') c = ch, ch = getchar();24 while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();25 if(c == '-') x = -x;26 }27 const long long INF = 0x3f3f3f3f;28 29 long long pow(long long a, long long b, long long mod)30 {31 long long r = 1, base = a % mod;32 for(;b;b >>= 1)33 {34 if(b & 1) r *= base, r %= mod;35 base *= base, base %= mod;36 }37 return r; 38 }39 void exgcd(long long a, long long b, long long &x, long long &y)40 {41 if(!b)x = 1, y = 0;42 else exgcd(b, a%b, y, x), y -= (a / b) * x;43 }44 long long ni(long long x, long long mod)45 {46 long long inv, y; exgcd(x, mod, inv, y);47 inv = (inv % mod + mod) % mod;48 if(!inv) inv = mod;49 return inv; 50 }51 int tiaoshi;52 long long calc(long long n, long long p, long long pt)53 {54 if(n == 0) return 1;55 long long ans = 1;56 for(long long i = 1;i <= pt;++ i) if(i % p) ans *= i, ans %= pt;57 ans = pow(ans, n/pt, pt);58 for(long long i = 1;i <= n%pt;++ i) 59 if(i % p) 60 ans *= i, ans %= pt;61 return ans * calc(n/p, p, pt) % pt;62 }63 long long C(long long n, long long m, long long p, long long pt)64 {65 if(n < m || n < 0 || m < 0) return 0;66 long long cnt = 0;67 for(long long i = n;i;i /= p) cnt += i/p;68 for(long long i = m;i;i /= p) cnt -= i/p;69 for(long long i = n - m;i;i /= p) cnt -= i/p;70 return pow(p, cnt, pt) * calc(n, p, pt) % pt * ni(calc(m, p, pt), pt) % pt * ni(calc(n - m, p, pt), pt) % pt;71 } 72 long long exlucas(long long n, long long m, long long mod)73 {74 long long tmp2 = mod, ans = 0;75 for(long long i = 2;i <= mod;++ i)76 if(tmp2 % i == 0)77 {78 long long pt = 1;79 while(tmp2 % i == 0) tmp2 /= i, pt *= i;80 long long tmp3 = C(n, m, i, pt);81 ans += tmp3 * (mod/pt) % mod * ni(mod/pt, pt) % mod;82 ans %= mod;83 }84 return ans;85 }86 long long n,m,p;87 int main()88 {89 read(n), read(m), read(p);90 printf("%lld", exlucas(n, m, p)); 91 return 0;92 }
GYM100633J

 

转载于:https://www.cnblogs.com/huibixiaoxing/p/8406941.html

你可能感兴趣的文章
NOI2018垫底记
查看>>
快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
查看>>
Codeforces Round #277 (Div. 2)
查看>>
一步步学Mybatis-搭建最简单的开发环境-开篇(1)
查看>>
微信小程序图片上传
查看>>
【更新】智能手机批量添加联系人
查看>>
NYOJ-128前缀式计算
查看>>
centos6.7 配置外网端口映射
查看>>
淡定,啊。数据唯一性
查看>>
深入理解 JavaScript 事件循环(一)— event loop
查看>>
Hive(7)-基本查询语句
查看>>
Redis快速入门
查看>>
注意java的对象引用
查看>>
C++ 面向对象 类成员函数this指针
查看>>
inline函数的总结
查看>>
Python字符编码
查看>>
leetcode 49. 字母异位词分组(Group Anagrams)
查看>>
NSPredicate的使用,超级强大
查看>>
自动分割mp3等音频视频文件的脚本
查看>>
财务结算的目的和一般流程
查看>>