1. 白玉楼前(youmu.cpp/c/pas)
【题目背景】
“一觉醒来怎么半灵又不见了?一定是幽幽子吃了。”
“幽幽子你给我吐出来!”
“我这边有个游戏玩不过去,你帮我玩过去我就吐出来。”
【题目描述】
妖梦现在要玩幽幽子的游戏,她才能拿回自己的半灵。
游戏规则是这样的:
幽幽子有n 个点,现在她让妖梦对每个点随机一条出边(随机到每个点的概率都相等),
然后得到一张图。(注意:可以自环)
如果这张图任意一个点沿着边走两步(显然这样的走法唯一)都能到达自身,则幽幽子
可以通关。
现在幽幽子想问妖梦,她通关的概率是多少?
两个图不同,当且仅当存在一条边出现在图A 中且不出现在图B 中。图中的点有编号,
边无编号。
答案mod 998244353。
【输入格式】
第一行一个数T 表示数据组数:
下面T 行,每行中只有一个数,表示n。
【输出格式】
输出T 行,每行一个数表示答案。
【样例输入】
1
1
【样例输出】
1
【样例解释】
只有1->1 这种情况,且符合题意。
【更多样例输入输出】
更多样例输入输出请见选手目录下/youmu 文件夹。
算法:dp+打表
题解:
经过打表以及很长时间猜测,冷静思考,推断等非正常方法,可以发现
满足条件的方案数可以动态规划,
设为当一共个点时,满足条件的方案数,
满足dp[i]=((dp[i-1]%p+((i-1)*dp[i-2]%p)%p)%p+p)%p;(p为模数)
然后,我们只需要求出在mod p 意义下的逆元即可(费马小定理)!
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define N 500005
using namespace std;
const ll p=998244353;
ll dp[N];
ll powermod(ll x,ll y)
{
ll ret=1;
while(y)
{
if(y%2) ret=ret*x%p;
y=y/2;
x=x*x%p;
}
return (ret%p+p)%p;
}
int main()
{
freopen("youmu.in","r",stdin);
freopen("youmu.out","w",stdout);
dp[1]=1,dp[2]=2;
for(int i = 3;i <= 500000;i++)
{
dp[i]=((dp[i-1]%p+((i-1)*dp[i-2]%p)%p)%p+p)%p;
}
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
ll jc=powermod(n,n);
printf("%I64d\n",(dp[n]*(powermod(jc,p-2)%p)+p)%p);
}
return 0 ;
}