2015ACM长春赛区网络赛 J题
来源:互联网 发布:手机 比价软件 编辑:程序博客网 时间:2024/05/17 07:07
Unknown Treasure
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2049 Accepted Submission(s): 752
Problem Description
On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a combination lock and some numbers on it. After quite a research, the mathematician found out that the correct combination to the lock would be obtained by calculating how many ways are there to pickm different apples among n of them and modulo it with M .M is the product of several different primes.
Input
On the first line there is an integer T(T≤20) representing the number of test cases.
Each test case starts with three integersn,m,k(1≤m≤n≤1018,1≤k≤10) on a line where k is the number of primes. Following on the next line are k different primes p1,...,pk . It is guaranteed that M=p1⋅p2⋅⋅⋅pk≤1018 and pi≤105 for every i∈{1,...,k} .
Each test case starts with three integers
Output
For each test case output the correct combination on a line.
Sample Input
19 5 23 5
Sample Output
6
利用卢卡斯定理和中国剩余定理
#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-6;const double pi = acos(-1.0);const int INF = 0x3f3f3f3f;const int MOD = 1000000007;#define ll long long#define CL(a) memset(a,0,sizeof(a))const int maxn = 1e5 + 5;ll fac[maxn] = { 0, 1 }, inv[maxn] = { 0, 1 };ll pow_mod(ll a, ll n, ll mod)//快速幂取模{ll ret = 1;while (n){if (n & 1)ret = ret*a%mod;a = a*a%mod;n >>= 1;}return ret;}ll Lucas(ll n, ll m, ll mod)//Lucas定理{ll ret = 1;ll mm = m, nn = n;while (mm && nn){ll mod_m = mm%mod, mod_n = nn%mod;if (mod_n >= mod_m)ret = ret*fac[mod_n] % mod*inv[mod_m] % mod*inv[mod_n - mod_m] % mod;else{ret = 0;break;}mm /= mod;nn /= mod;}return ret;}void exgcd(ll a, ll b, ll &d, ll &x, ll &y)//扩展欧几里德求逆元{if (b == 0)d = a, x = 1, y = 0;else{exgcd(b, a%b, d, y, x);y -= x * (a / b);}}ll china(ll n, ll m[], ll a[])//中国剩余定理求解{ll aa = a[0];ll mm = m[0];for (int i = 0; i<n; i++){ll sub = (a[i] - aa);ll d, x, y;exgcd(mm, m[i], d, x, y);if (sub % d) return -1;ll new_m = m[i] / d;new_m = (sub / d*x%new_m + new_m) % new_m;aa = mm*new_m + aa;mm = mm*m[i] / d;}aa = (aa + mm) % mm;return aa;}int main(){int cas;ll n, m, k, a[15], p[15];scanf("%d", &cas);while (cas--){scanf("%lld%lld%lld", &n, &m, &k);for (int i = 0; i<k; i++){scanf("%lld", &p[i]);//init(p[i]);for (int j = 2; j<p[i]; j++)fac[j] = fac[j - 1] * j%p[i];inv[p[i] - 1] = pow_mod(fac[p[i] - 1], p[i] - 2, p[i]);for (int j = p[i] - 1; j>0; j--)inv[j - 1] = inv[j] * j%p[i];a[i] = Lucas(n, m, p[i]);}printf("%lld\n", china(k, p, a));}return 0;}
0 0
- 2015ACM长春赛区网络赛 J题
- 2015年ACM长春赛区网络赛G题
- 2015ACM长春赛区网络赛C题
- 2015ACM长春赛区网络赛H题
- 2015ACM长春赛区网络赛 E题
- 2015ACM长春赛区网络赛 A题 没写完
- 2013icpc网络赛长春赛区J题(Flyer)
- HDU 5536 2015ACM-ICPC长春赛区现场赛J题
- 2012 ACM/ICPC 长春赛区网络赛
- 2013年ACM网络赛长春赛区
- hdu5446(2015长春网络赛J题)
- HDU4268 2012ACM长春赛区网络赛 Alice and Bob
- 2012 ACM/ICPC 长春赛区网络赛 1006(dfs)
- Hdu5441 2015长春赛区网络赛
- 2015ACM长春赛区网络赛 B题 没写完呢要打比赛了先存在这里的
- 2015 ACM ICPC 长春赛区 网络赛 HDU 5437 Alisha’s Party
- HDU 5437 Alisha’s Party(2015ACM长春赛区网络赛+优先队列)
- hdu 5920 Ugly Problem 2016ACM/CCPC长春赛区现场赛J
- js 阻止冒泡排序
- GitHub前100的安卓开源架包
- ajax 传json到后台servlet接收
- 1023 Problem W
- Codeforces 612C Replace To Make Regular Bracket Sequence
- 2015ACM长春赛区网络赛 J题
- C++中的虚函数和存虚函数
- Spring事务配置
- C#中的String,StringBuilder 常用方法总结
- Work Applications编程题:最大异或值
- JavaScript学习--Item12 undefined 与 null
- 蓝桥杯 密文搜索 O(nlogn)实现
- addEventListener绑定事件的对象方法。
- mysql存储过程----临时表 temporary