当前位置:网站首页>洛谷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;
}
边栏推荐
- 【力扣】22.括号生成
- STC8H development (15): GPIO drive Ci24R1 wireless module
- What is ensemble learning in machine learning?
- QueryDet:级联稀疏query加速高分辨率下的小目标检测
- 获取Qt的安装信息:包括安装目录及各种宏地址
- Introduction to c # a week of high-level programming c # - LINQ Day Four
- Provincial level of Echart maps, as well as all prefecture-level download and use
- leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
- What has programmatic trading changed?
- A brief analysis of whether programmatic futures trading or manual order is better?
猜你喜欢

Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
![[Likou] 22. Bracket generation](/img/f6/435fe9e0b4c1545514d1bf195ffd44.png)
[Likou] 22. Bracket generation

2022-08-10 The sixth group Hiding spring study notes

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

Read the article, high-performance and predictable data center network

【愚公系列】2022年08月 Go教学课程 036-类型断言
![[FPGA] day19- binary to decimal (BCD code)](/img/d8/6d223e5e81786335a143f135385b08.png)
[FPGA] day19- binary to decimal (BCD code)

【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口

【深度学习】基于卷积神经网络的天气识别训练

QueryDet:级联稀疏query加速高分辨率下的小目标检测
随机推荐
LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
uni-app - city selection index list / city list sorted by A-Z (uview component library IndexList index list)
Will oracle cardinality affect query speed?
Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
(转)JVM中那些区域会发生OOM?
Multi-serial port RS485 industrial gateway BL110
Read the article, high-performance and predictable data center network
Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
LeetCode刷题第16天之《239滑动窗口最大值》
每日一题-滑动窗口
云平台下ESB产品开发步骤说明
uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)
EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
华南师范宋宇老师课堂对话论文翻译
Get the length of the linked list
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
What should I do if the channel ServerID is incorrect when EasyCVR is connected to a Hikvision Dahua device and selects another cluster server?
使用百度EasyDL实现森林火灾预警识别
C语言 recv()函数、recvfrom()函数、recvmsg()函数
Introduction to c # a week of high-level programming c # - LINQ Day Four