您的位置:首页 > 其它

BZOJ 4197: [Noi2015]寿司晚宴( dp )

2016-01-18 13:22 435 查看


N^0.5以内的质数只有8个, dp(i, j, k)表示用了前i个大质数(>N^0.5), 2人选的质数(<=N^0.5)集合分别为j, k时的方案数. 转移时考虑当前的大质数p是给哪个人即可. 时间复杂度O(N*2^16)

-----------------------------------------------------------------

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;#define b(x) (1 << (x))const int maxn = 509;const int maxp = 8;const int p[8] = {2, 3, 5, 7, 11, 13, 17, 19};int N, P;int b[maxn], bp[maxn], r[maxn];int dp[b(maxp)][b(maxp)], A[b(maxp)][b(maxp)], B[b(maxp)][b(maxp)];inline void upd(int &x, int t) { if((x += t) >= P) x -= P;}inline bool Cmp(const int &l, const int &r) { return bp[l] < bp[r];}int main() { bool F = false; scanf("%d%d", &N, &P); for(int i = 2; i <= N; i++) { b[i] = bp[i] = 0; int v = r[i] = i; for(int j = 0; j < 8; j++) if(v % p[j] == 0) { b[i] |= b(j); while(v % p[j] == 0) v /= p[j]; if(v == 1) break; } if(v != 1) bp[i] = v, F = true; } sort(r + 2, r + N + 1, Cmp); memset(dp, 0, sizeof dp); dp[0][0] = 1; for(int i = 2; i <= N; i++) if(!bp[r[i]]) { for(int x = b(maxp); x--; ) for(int y = b(maxp); y--; ) { upd(dp[x | b[r[i]]][y], dp[x][y]); upd(dp[x][y | b[r[i]]], dp[x][y]); } } if(!F) { int ans = 0; for(int i = b(maxp); i--; ) for(int j = b(maxp); j--; ) if(!(i & j)) upd(ans, dp[i][j]); printf("%d\n", ans); return 0; } for(int i = 2, v = 0; i <= N; i++) if(bp[r[i]]) { if(bp[r[i]] != v) { if(v) { for(int x = b(maxp); x--; ) for(int y = b(maxp); y--; ) dp[x][y] = ((A[x][y] + B[x][y] - dp[x][y]) % P + P) % P; } memcpy(A, dp, sizeof A); memcpy(B, dp, sizeof B); v = bp[r[i]]; } for(int x = b(maxp); x--; ) for(int y = b(maxp); y--; ) { upd(A[x | b[r[i]]][y], A[x][y]); upd(B[x][y | b[r[i]]], B[x][y]); } } int ans = 0; for(int i = b(maxp); i--; ) for(int j = b(maxp); j--; ) if(!(i & j)) upd(ans, ((A[i][j] + B[i][j] - dp[i][j]) % P + P) % P); printf("%d\n", ans); return 0;}-----------------------------------------------------------------

4197: [Noi2015]寿司晚宴

Time Limit: 10 Sec Memory Limit: 512 MB
Submit: 164 Solved: 119
[Submit][Status][Discuss]

Description

为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴。小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴。

在晚宴上,主办方为大家提供了 n−1 种不同的寿司,编号 1,2,3,…,n−1,其中第 i 种寿司的美味度为 i+1 (即寿司的美味度为从 2 到 n)。现在小 G 和小 W 希望每人选一些寿司种类来品尝,他们规定一种品尝方案为不和谐的当且仅当:小 G 品尝的寿司种类中存在一种美味度为 x 的寿司,小 W 品尝的寿司中存在一种美味度为 y 的寿司,而 x 与 y 不互质。现在小 G 和小 W 希望统计一共有多少种和谐的品尝寿司的方案(对给定的正整数 p 取模)。注意一个人可以不吃任何寿司。

Input

输入文件的第 1 行包含 2 个正整数 n,p,中间用单个空格隔开,表示共有 n 种寿司,最终和谐的方案数要对 p 取模。

Output

输出一行包含 1 个整数,表示所求的方案模 p 的结果。

Sample Input

3 10000

Sample Output

9

HINT

2≤n≤500

0<p≤1000000000

Source

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: