当前位置:网站首页>洛谷P2150 寿司晚宴
洛谷P2150 寿司晚宴
2022-08-11 04:00:00 【CLH_W】
题目描述
为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴。小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴。
在晚宴上,主办方为大家提供了 n−1n−1 种不同的寿司,编号 1,2,3,\ldots,n-11,2,3,…,n−1,其中第 ii 种寿司的美味度为 i+1i+1。(即寿司的美味度为从 22 到 nn)
现在小 G 和小 W 希望每人选一些寿司种类来品尝,他们规定一种品尝方案为不和谐的当且仅当:小 G 品尝的寿司种类中存在一种美味度为 xx 的寿司,小 W 品尝的寿司中存在一种美味度为 yy 的寿司,而 xx 与 yy 不互质。
现在小 G 和小 W 希望统计一共有多少种和谐的品尝寿司的方案(对给定的正整数 pp 取模)。注意一个人可以不吃任何寿司。
输入格式
输入文件的第 11 行包含 22 个正整数 n, pn,p 中间用单个空格隔开,表示共有 nn 种寿司,最终和谐的方案数要对 pp 取模。
输出格式
输出一行包含 11 个整数,表示所求的方案模 pp 的结果。
输入输出样例
输入 #1复制
3 10000
输出 #1复制
9
输入 #2复制
4 10000
输出 #2复制
21
输入 #3复制
100 100000000
输出 #3复制
3107203
说明/提示
【数据范围】

勘误:0 < p \le 10^90<p≤10
9
上代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int n, mod, f[1 << 8][1 << 8], g[2][1 << 8][1 << 8];
pair<int, int> a[maxn];
inline int gi()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
const int maxp = 8;
const int p[8] = {
2, 3, 5, 7, 11, 13, 17, 19
};
inline void inc(int &a, int b)
{
a += b;
if (a >= mod) a -= mod;
}
int main()
{
n = gi(); mod = gi();
for (int i = 2; i <= n; ++i) {
int k = i;
for (int j = 0; j < maxp; ++j)
if (k % p[j] == 0) {
a[i].second ^= 1 << j;
while (k % p[j] == 0) k /= p[j];
}
a[i].first = k;
}
sort(a + 2, a + n + 1);
f[0][0] = 1;
for (int i = 2; i <= n; ++i) {
if (i == 2 || a[i].first == 1 || a[i].first != a[i - 1].first) {
memcpy(g[0], f, sizeof(g[0]));
memcpy(g[1], f, sizeof(g[1]));
}
for (int s1 = (1 << maxp) - 1; s1 >= 0; --s1)
for (int s2 = (1 << maxp) - 1; s2 >= 0; --s2) {
if ((s2 & a[i].second) == 0) inc(g[0][s1 | a[i].second][s2], g[0][s1][s2]);
if ((s1 & a[i].second) == 0) inc(g[1][s1][s2 | a[i].second], g[1][s1][s2]);
}
if (i == n || a[i].first == 1 || a[i].first != a[i + 1].first) {
for (int s1 = (1 << maxp) - 1; ~s1; --s1)
for (int s2 = (1 << maxp) - 1; ~s2; --s2) {
f[s1][s2] = g[0][s1][s2] + g[1][s1][s2] - f[s1][s2];
if (f[s1][s2] < 0) f[s1][s2] += mod;
if (f[s1][s2] >= mod) f[s1][s2] -= mod;
}
}
}
int ans = 0;
for (int s1 = (1 << maxp) - 1; ~s1; --s1)
for (int s2 = (1 << maxp) - 1; ~s2; --s2)
if ((s1 & s2) == 0) inc(ans, f[s1][s2]);
printf("%d\n", ans);
return 0;
}
边栏推荐
- 【FPGA】day19-二进制转换为十进制(BCD码)
- The custom of the C language types -- -- -- -- -- - structure
- leetcode刷题第13天二叉树系列之《98 BST及其验证》
- LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
- .NET service registration
- set_new_handler(0)是什么意思?有什么用?
- QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
- es-head插件插入查询以及条件查询(五)
- Alibaba Cloud releases 3 high-performance computing solutions
- DNS separation resolution and intelligent resolution
猜你喜欢

The last update time of the tables queried by the two nodes of the rac standby database is inconsistent

LeetCode刷题第17天之《3 无重复字符的最长子串》

【FPGA】day22-SPI protocol loopback

QueryDet:级联稀疏query加速高分辨率下的小目标检测

高校就业管理系统设计与实现

What should I do if the channel ServerID is incorrect when EasyCVR is connected to a Hikvision Dahua device and selects another cluster server?

北湖区燕泉街道开展“戴头盔·保安全”送头盔活动

LeetCode Brush Questions Day 11 String Series "58 Last Word Length"

LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"

【FPGA】abbreviation
随机推荐
Where can machine learning be applied?What is machine learning useful for?
The thirteenth day of learning programming
How can users overcome emotional issues in programmatic trading?
使用百度EasyDL实现智能垃圾箱
【FPGA】SDRAM
Callable实现多线程
电力机柜数据监测RTU
shell monitors gpu usage
LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
【FPGA】day20-I2C读写EEPROM
【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
【C语言】入门
Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
【ADI低功耗2k代码】基于ADuCM4050的ADXL363、TMP75的加速度、温度检测及串口打印、蜂鸣器播放音乐(孤勇者)
华南师范宋宇老师课堂对话论文翻译
I didn't expect MySQL to ask these...
蹭个热度-请勿打开
常见布局效果实现方案
Enter the starting position, the ending position intercepts the linked list
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)