列表

详情


NC22398. Mysterious … Host

描述

Finally, Rikka got an excellent design which not only creates efficient traffic but also costs little money. But Rikka didn't care whether the trite officers accepted it --- LCR had been behind her for long, with a chaste smile, a garland, and a white dress. Gradually calmed down by the smile, the girls reached out their hands and began their journey. They ended up at a self-study room in the Middle School Attached to Northwestern Polytechnical University after the romantic meeting.

The formulas on the whiteboards and papers have reminded Rikka (about her study, of course), so she is asking LCR to play a game with her now (in order to review combinatorics).

LCR has an n-order permutation and Rikka tries to guess it. At first, Rikka should choose a set of n-order permutations. LCR will make some queries then. Each time, LCR gives a segment and for each chosen permutation Rikka answers if the segment is consecutive (defined in the following paragraphs) in it. In the end, Rikka will win the game if and only if there exists a chosen permutation which has the same answer to each query as LCR's original one.

Rikka wonders how many permutations she has to choose at least to ensure winning the game. For future games, she needs the answer for each positive integer n not greater than N. She only needs the answer modulo a certain prime P because its exact value may be too large.

The segment [L, R] is consecutive in an n-order permutation p if and only if there doesn't exist three integers x, y, z such that , , , , where is an integer in [1, n], which means the i-th element in the permutation p.

输入描述

The only line contains two integers , the maximal order of the permutations and the divisor, separated by a space. It is guaranteed that P is prime.

输出描述

Output N lines; for , the n-th line contains one integer, the minimum number of permutations which Rikka needs to choose for order n, modulo P.

示例1

输入:

10 65537

输出:

1
1
3
12
52
240
1160
5795
29681
23951

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 727ms, 内存消耗: 628K, 提交时间: 2020-03-17 11:19:20

#include<iostream>
#include<algorithm>
using namespace std;
int mod;
void add(int &a,int b)
{
	a+=b;
	if(a>=mod) a-=mod;
}
#define MAXNUM 5555
#define rep(i,s,t) for(int i=s;i<t;i++)
int sum[MAXNUM][5],dp[MAXNUM];
int main()
{
	int n;
	scanf("%d%d",&n,&mod);
	dp[1]=sum[1][1]=1;
	rep(i,2,n+1)
	{
		rep(j,1,i)
		rep(k,1,5)
		{
			add(sum[i][k],sum[i-j][k-1]*1LL*dp[j]%mod);
			if(k==4) add(sum[i][4],sum[i-j][4]*1LL*dp[j]%mod);
		}
		add(dp[i],sum[i][4]);
		rep(k,2,5)
		add(dp[i],sum[i][k]);
		sum[i][1]=dp[i];
	}
	rep(i,1,n+1)
	printf("%d\n",dp[i]);
}

上一题