結果

問題 No.1392 Don't be together
ユーザー kotatsugamekotatsugame
提出日時 2021-02-12 22:21:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 2,000 ms
コード長 3,129 bytes
コンパイル時間 895 ms
コンパイル使用メモリ 71,948 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 04:50:24
合計ジャッジ時間 5,598 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 147 ms
4,376 KB
testcase_07 AC 213 ms
4,376 KB
testcase_08 AC 186 ms
4,380 KB
testcase_09 AC 170 ms
4,376 KB
testcase_10 AC 213 ms
4,376 KB
testcase_11 AC 214 ms
4,376 KB
testcase_12 AC 214 ms
4,380 KB
testcase_13 AC 214 ms
4,376 KB
testcase_14 AC 214 ms
4,376 KB
testcase_15 AC 214 ms
4,384 KB
testcase_16 AC 213 ms
4,384 KB
testcase_17 AC 213 ms
4,380 KB
testcase_18 AC 214 ms
4,380 KB
testcase_19 AC 214 ms
4,376 KB
testcase_20 AC 68 ms
4,380 KB
testcase_21 AC 24 ms
4,376 KB
testcase_22 AC 145 ms
4,380 KB
testcase_23 AC 88 ms
4,376 KB
testcase_24 AC 80 ms
4,380 KB
testcase_25 AC 98 ms
4,376 KB
testcase_26 AC 26 ms
4,380 KB
testcase_27 AC 63 ms
4,380 KB
testcase_28 AC 84 ms
4,380 KB
testcase_29 AC 44 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:10:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]

ソースコード

diff #

#line 1 "a.cpp"
#include<iostream>
#include<vector>
using namespace std;
#line 3 "/home/kotatsugame/library/math/modint.cpp"
#include<utility>
template<int m>
struct modint{
	unsigned int x;
	constexpr modint()noexcept:x(){}
	template<typename T>
	constexpr modint(T x_)noexcept:x((x_%=m)<0?x_+m:x_){}
	constexpr unsigned int val()const noexcept{return x;}
	constexpr modint&operator++()noexcept{if(++x==m)x=0;return*this;}
	constexpr modint&operator--()noexcept{if(x==0)x=m;--x;return*this;}
	constexpr modint operator++(int)noexcept{modint res=*this;++*this;return res;}
	constexpr modint operator--(int)noexcept{modint res=*this;--*this;return res;}
	constexpr modint&operator+=(const modint&a)noexcept{x+=a.x;if(x>=m)x-=m;return*this;}
	constexpr modint&operator-=(const modint&a)noexcept{if(x<a.x)x+=m;x-=a.x;return*this;}
	constexpr modint&operator*=(const modint&a)noexcept{x=(unsigned long long)x*a.x%m;return*this;}
	constexpr modint&operator/=(const modint&a)noexcept{return*this*=a.inv();}
	constexpr modint operator+()const noexcept{return*this;}
	constexpr modint operator-()const noexcept{return modint()-*this;}
	constexpr modint pow(long long n)const noexcept
	{
		if(n<0)return pow(-n).inv();
		modint x=*this,r=1;
		for(;n;x*=x,n>>=1)if(n&1)r*=x;
		return r;
	}
	constexpr modint inv()const noexcept
	{
		int s=x,t=m,x=1,u=0;
		while(t)
		{
			int k=s/t;
			s-=k*t;
			swap(s,t);
			x-=k*u;
			swap(x,u);
		}
		return modint(x);
	}
	friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;}
	friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;}
	friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;}
	friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;}
	friend constexpr bool operator==(const modint&a,const modint&b){return a.x==b.x;}
	friend constexpr bool operator!=(const modint&a,const modint&b){return a.x!=b.x;}
	friend ostream&operator<<(ostream&os,const modint&a){return os<<a.x;}
	friend istream&operator>>(istream&is,modint&a){long long v;is>>v;a=modint(v);return is;}
};
#line 5 "a.cpp"
using mint=modint<998244353>;
int N,M;
int P[5050];
bool vis[5050];
mint dp[5050],ep[2][2][5050];
main()
{
	cin>>N>>M;
	for(int i=0;i<N;i++)
	{
		cin>>P[i];
		P[i]--;
	}
	dp[0]=1;
	for(int i=0;i<N;i++)if(!vis[i])
	{
		vector<int>now;
		now.push_back(i);
		do{
			int u=now.back();
			now.push_back(P[u]);
			vis[P[u]]=true;
		}while(now.back()!=i);
		now.pop_back();
		int a=0;
		for(int j=0;j<2;j++)for(int k=0;k<=N;k++)ep[a][j][k]=0;
		for(int j=0;j<=N;j++)
		{
			ep[a][1][j]+=dp[j]*j;
			ep[a][1][j+1]+=dp[j];
		}
		for(int id=1;id<now.size();id++)
		{
			int b=1-a;
			for(int j=0;j<2;j++)for(int k=0;k<=N;k++)ep[b][j][k]=0;
			for(int j=0;j<2;j++)for(int k=0;k<=N;k++)
			{
				if(j==0)
				{
					ep[b][1][k]+=ep[a][j][k];
					ep[b][0][k]+=ep[a][j][k]*(k-2);
					ep[b][0][k+1]+=ep[a][j][k];
				}
				else
				{
					ep[b][0][k]+=ep[a][j][k]*(k-1);
					ep[b][0][k+1]+=ep[a][j][k];
				}
			}
			a=b;
		}
		for(int k=0;k<=N;k++)dp[k]=ep[a][0][k];
	}
	cout<<dp[M]<<endl;
}
0