HDU -- 4869 Turn the pokers(组合数,费马小定理)
来源:互联网 发布:最近网络怎么了 编辑:程序博客网 时间:2024/05/01 02:41
有m张牌,要翻n次,每次翻xi个。问最终有多少种状态(牌的正反状态)。
http://acm.hdu.edu.cn/showproblem.php?pid=4869
如果一张牌翻过去在翻回来等于没翻,这样翻牌的次数总和就相当于减小了2。
如果设牌的起始状态都是0,可以发现最终翻成1的牌的总和的奇偶性是一定的。
所以我们要维护最少的1的个数和最多的1的个数。
∑C(k, m)就是答案(k是可能的1的个数)
这里要求组合数一定会用到除法。
要用到费马小定理:a^(p-1) = 1%p;
移过去一个a得,a^(p-2) = (1/a) % p;
这样就将除以一个整数a变成了求a的p-2次方(用快速幂实现)
维护最大最小值见代码:
//#pragma comment(linker,"/STACK:1024000000,1024000000")#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <cstdio>#include <string>#include <vector>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef double DB;typedef long long ll;typedef pair<int, int> PII;#define pb push_back#define MP make_pair#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1const DB pi = acos(-1.0);const DB eps = 1e-8;const int inf = 0x3f3f3f3f;const ll INF = 0x3f3f3f3f3f3f3f3f;const int mod = 1000000009;const int maxn = 200000 + 10;int n, m;ll C[maxn];ll Qpow(int x, int y){ ll tmp = x, ret = 1; while(y){ if(y & 1) ret = ret * tmp % mod; tmp = tmp * tmp % mod; y >>= 1; } return ret;}int main(){ while(scanf("%d%d", &n, &m) == 2){ int x, mi = 0, mx = 0;///最少有多少个1,最多有多少个1 for(int i=1; i<=n; i++){ scanf("%d", &x); int minn, maxx; if(x <= mi) minn = mi - x;///如果x小于最少能得到的1,那么将mi中的x个1变成0,就得到了新的最小的mi-x个1 else if(x <= mx) minn = ((mi % 2) == (x % 2)) ? 0 : 1;///如果mi<x<=mx,因为从mi到mx所有和它们奇偶性相同的数都能取到,所以只要判断x的奇偶性和mi的奇偶性是否相同,相同就是0,否则为1. else minn = x - mx;///如果x比当前能取到的最多的1的数量还多,那么最少的1就应该是现将mx个1翻成0,再将x-mx个0翻成1 if(x + mx <= m) maxx = mx + x;///如果x加上mx都不超过m,就将不为1的x个数翻成1,得到新的最多的mx+x个1 else if(x + mi <= m) maxx = ((m % 2) == ((mi+x) % 2)) ? m : m - 1;///如果m-mx<x<=m-mi,还是因为从mi到mx所有和它们奇偶性相同的数都能取到,所以当m的奇偶性和mi+x相同时,一定能将m个数全覆盖,否则一定能覆盖m-1个 else maxx = m - (x + mi - m);///否则,因为x+mi>m,当我把所有的0翻成1之后还剩x+mi-m次,所以1个个数就是m-(x+mi-m) mi = minn, mx = maxx; } C[0] = 1; for(int i=1; i<=mx; i++){ if(m - i < i) C[i] = C[m - i]; else{ C[i] = C[i - 1] * (m - i + 1) % mod * Qpow(i, mod - 2) % mod;///费马小定理 } } ll ans = 0; for(int i=mi; i<=mx; i+=2){ ans = (ans + C[i]) % mod; } printf("%I64d\n", ans); } return 0;}
0 0
- HDU -- 4869 Turn the pokers(组合数,费马小定理)
- 【HDU】4869 Turn the pokers 组合数
- hdoj 4869 Turn the pokers 【组合数 + 逆元】【费马小定理——>逆元】
- hdu 4869 Turn the pokers 策略(组合数)
- hdu 4704 Sum||hdu 4869 Turn the pokers 费马小定理
- 【数论】HDU 4869 Turn the pokers 费马小定理
- HDU 4869 Turn the pokers 组合数学
- HDU 4869 Turn the pokers【组合数学】
- <模板>Hdu4869 Turn the pokers 组合数求余 费马小定理
- 贪心+组合数 hdu4869 Turn the pokers
- HDU 4869 Turn the pokers
- hdu 4869 Turn the pokers
- HDU 4869 Turn the pokers
- HDU 4869 Turn the pokers
- HDU 4869 Turn the pokers
- HDU 4869Turn the pokers
- HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)
- hdu 4869 Turn the pokers 策略
- SimpleDateFormat的格式大全
- 金字塔
- 1
- Android 拍照并显示在ImageView中(进阶)
- 关于字符串组合等地址问题
- HDU -- 4869 Turn the pokers(组合数,费马小定理)
- jQuery.form Ajax无刷新上传报错 (jQuery.handleError is not a function) 解决办法
- 暂时备份(全方位分块仿射变换)
- qemu 修改qcow2 image文件
- POJ--3981:字符串替换
- xtu-1140 Anti-Goldbach's Conjecture
- Unity3D MonoBehaviour Lifecycle
- 常用excel函数实例
- have a look