Bzoj4197: [Noi2015]寿司晚宴
来源:互联网 发布:11选5前三破解算法 编辑:程序博客网 时间:2024/03/29 19:03
原题网址:http://www.lydsy.com/JudgeOnline/problem.php?id=4197
这和今年THUSC的一题有点像,都是将根号以内的质数作状态。
首先每个质数都只能在一边出现一次,考虑状压,把包含质数的二进制压成状态,两边质数不能有交集,每次一个数放进来,枚举放左边,放右边和不放。但
已经加了枚举子集的小小优化,但跑下来时间还是没达到bzoj平均水平,不过是1A,开心。(再也不会忘记+P再取模啦)
#include<bits/stdc++.h>typedef long long ll;const int N = 505;const int all = 1 << 8;const int p[8] = {2,3,5,7,11,13,17,19};struct rec{ int s,b;}a[N];int n,P;ll f[all][all],g[2][all][all],ans;bool cmp(const rec &a, const rec &b){ return a.b > b.b;}int main(){ scanf("%d%d",&n,&P); for (int i=1; i<n; i++){ int temp = i + 1; for (int j=0; j<8; j++){ a[i].s = a[i].s * 2 + (temp % p[j] == 0); while (temp % p[j] == 0) temp /= p[j]; } a[i].b = temp; } n--; std::sort(a+1,a+n+1,cmp); g[0][0][0] = 1; g[1][0][0] = 1; f[0][0] = 1; for (int i=1; i<=n; i++){ if (i == 1 || a[i].b != a[i-1].b || a[i].b == 1) for (int S=0; S<all; S++) for (int _S=0; _S<all; _S++) f[S][_S] = (g[0][S][_S] + g[1][S][_S] - f[S][_S]) % P, g[0][S][_S] = g[1][S][_S] = f[S][_S]; int _C = (all - 1) ^ a[i].s; for (int _S = _C; _S>=0; _S = (_S - 1) & _C){ int C = (all - 1) ^ _S; for (int S = C; S>=0; S = (S - 1) & C){ g[0][S | a[i].s][_S] = (g[0][S | a[i].s][_S] + g[0][S][_S]) % P; g[1][_S][S | a[i].s] = (g[1][_S][S | a[i].s] + g[1][_S][S]) % P; if (!S) break; } if (!_S) break; } } for (int S=0; S<all; S++) for (int _S=0; _S<all; _S++) ans += g[0][S][_S] + g[1][S][_S] - f[S][_S]; printf("%lld\n",(ans % P + P) % P); return 0;}
阅读全文
0 0
- BZOJ4197 [Noi2015]寿司晚宴
- bzoj4197 [Noi2015]寿司晚宴
- 【bzoj4197】[Noi2015]寿司晚宴
- Bzoj4197: [Noi2015]寿司晚宴
- 【bzoj4197】【NOI2015】寿司晚宴
- 【bzoj4197】[Noi2015]寿司晚宴 dp
- 【bzoj4197】[Noi2015]寿司晚宴 状压DP
- 【BZOJ4197】[Noi2015]寿司晚宴【状压DP】【背包】
- [NOI2015][BZOJ4197][状压DP]寿司晚宴
- bzoj4197[Noi2015]寿司晚宴 [状压DP]
- bzoj4197 [NOI2015] [状压dp] 寿司晚宴
- BZOJ4197: [Noi2015]寿司晚宴 状压DP
- 【NOI2015】【bzoj4197】【状压DP】【滚动数组】寿司晚宴
- 4197: [Noi2015]寿司晚宴
- 4197: [Noi2015]寿司晚宴
- 【NOI2015】【寿司晚宴】【状压DP】
- bzoj 4197: [Noi2015]寿司晚宴
- BZOJ 4197 Noi2015 寿司晚宴
- Spring Security
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem A : Corn's new language
- hdu1045+bfs Fire Net
- linux下安装jdk和tomcat的方法
- 2017.5.21测试 题二 朋友
- Bzoj4197: [Noi2015]寿司晚宴
- Oracle/MySQL/SQL Server创建/删除视图
- 2017年国内开源镜像站点
- phpstorm/webstorm/idea 出现一只刷新缓存Updating Index
- 关于dubbo分布式项目管理(生产者/消费者)简单Demo
- adf4360的寄存器写入顺序
- 百度统计安装和使用
- js中解析字符串-eval()和JSON()
- TCP网络编程框架函数