当前位置:网站首页>洛谷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码)
- Get Qt installation information: including installation directory and various macro addresses
- uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
- rub the heat - do not open
- Is there any way for kingbaseES to not read the system view under sys_catalog by default?
- "110 Balanced Binary Tree Judgment" in leetCode's 14-day binary tree series
- set_new_handler(0)是什么意思?有什么用?
- LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
- What has programmatic trading changed?
- EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
猜你喜欢
![[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface](/img/cb/41e5f553b0b776dccf0e39f9bf377f.png)
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
![[Likou] 22. Bracket generation](/img/f6/435fe9e0b4c1545514d1bf195ffd44.png)
[Likou] 22. Bracket generation

【FPGA】day20-I2C读写EEPROM

Echart地图的省级,以及所有地市级下载与使用

【FPGA】abbreviation

es-head plugin insert query and conditional query (5)

【C语言】入门

【FPGA】SDRAM

这些云自动化测试工具值得拥有
![[C Language] Getting Started](/img/5e/484e3d426a6f1cc0d792a9ba330695.png)
[C Language] Getting Started
随机推荐
拼多多店铺营业执照相关问题
MySQL数据库存储引擎以及数据库的创建、修改与删除
What has programmatic trading changed?
Detailed explanation of VIT source code
多串口RS485工业网关BL110
【C语言】入门
How to delete statements audit log?
QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
机器学习是什么?详解机器学习概念
如何进行AI业务诊断,快速识别降本提效增长点?
uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)
Design and Realization of Employment Management System in Colleges and Universities
[ADI low-power 2k code] Based on ADuCM4050, ADXL363, TMP75 acceleration, temperature detection and serial port printing, buzzer playing music (lone warrior)
The development of the massage chair control panel makes the massage chair simple and intelligent
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
AI + medical: for medical image recognition using neural network analysis
Day20 FPGA 】 【 - block the I2C read and write EEPROM
js 将字符串作为js执行代码使用
我的 archinstall 使用手册
Power Cabinet Data Monitoring RTU