結果

問題 No.2144 MM
ユーザー kotatsugamekotatsugame
提出日時 2022-12-02 22:16:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 572 ms / 2,000 ms
コード長 1,639 bytes
コンパイル時間 1,098 ms
コンパイル使用メモリ 86,532 KB
実行使用メモリ 38,784 KB
最終ジャッジ日時 2024-04-18 06:34:29
合計ジャッジ時間 8,405 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 30 ms
34,432 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 15 ms
18,304 KB
testcase_07 AC 68 ms
6,784 KB
testcase_08 AC 530 ms
38,784 KB
testcase_09 AC 68 ms
6,784 KB
testcase_10 AC 572 ms
38,272 KB
testcase_11 AC 543 ms
36,096 KB
testcase_12 AC 563 ms
37,632 KB
testcase_13 AC 394 ms
10,368 KB
testcase_14 AC 531 ms
35,072 KB
testcase_15 AC 407 ms
21,760 KB
testcase_16 AC 138 ms
32,768 KB
testcase_17 AC 236 ms
6,784 KB
testcase_18 AC 21 ms
5,376 KB
testcase_19 AC 326 ms
12,928 KB
testcase_20 AC 343 ms
21,248 KB
testcase_21 AC 293 ms
36,608 KB
testcase_22 AC 54 ms
10,880 KB
testcase_23 AC 398 ms
36,352 KB
testcase_24 AC 103 ms
18,816 KB
testcase_25 AC 30 ms
34,432 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 16 ms
19,072 KB
testcase_28 AC 5 ms
5,376 KB
testcase_29 AC 10 ms
11,264 KB
testcase_30 AC 31 ms
34,944 KB
testcase_31 AC 15 ms
17,920 KB
testcase_32 AC 17 ms
17,664 KB
testcase_33 AC 5 ms
7,296 KB
testcase_34 AC 27 ms
32,256 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:16:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   16 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<atcoder/modint>
#include<atcoder/lazysegtree>
using namespace std;
using mint=atcoder::modint998244353;
using dat=pair<mint,mint>;//(val, len)
dat op(dat a,dat b){return make_pair(a.first+b.first,a.second+b.second);}
dat e(){return make_pair(mint::raw(0),mint::raw(0));}
using F=pair<mint,mint>;//(mul, add)
dat mp(F f,dat x){return make_pair(x.first*f.first+x.second*f.second,x.second);}
F cmp(F f,F g){return make_pair(g.first*f.first,g.second*f.first+f.second);}
F id(){return make_pair(mint::raw(1),mint::raw(0));}
int N,M;
int A[2<<17],sign[2<<17];
long S[2<<17];
main()
{
	cin>>N>>M;
	for(int i=0;i<N;i++)
	{
		cin>>A[i];
		sign[i]=(N-i)%2==1?-1:1;
		S[i+1]=S[i]+A[i]*sign[i];
	}
	if(S[N]%M!=0)
	{
		cout<<-1<<endl;
		return 0;
	}
	atcoder::lazy_segtree<dat,op,e,F,mp,cmp,id>seg(vector<dat>(M,make_pair(mint::raw(0),mint::raw(1))));
	seg.set(0,make_pair(mint::raw(1),mint::raw(1)));
	int off=0;
	mint ans=1;
	for(int i=N-1;i>=0;i--)
	{
		if(sign[i]==1)
		{
			//0<=a<A[i], seg[S[i]+a-off]
			//S[i]-off<=*<S[i]+A[i]-off
			int L=((S[i]-off)%M+M)%M;
			int R=((S[i]+A[i]-off)%M+M)%M;
			if(L<=R)ans+=seg.prod(L,R).first;
			else ans+=seg.prod(L,M).first+seg.prod(0,R).first;
		}
		else
		{
			//0<=a<A[i], seg[S[i]-a-off]
			//S[i]-A[i]-off<*<=S[i]-off
			//S[i]-A[i]-off+1<=*<S[i]-off+1
			int L=((S[i]-A[i]-off+1)%M+M)%M;
			int R=((S[i]-off+1)%M+M)%M;
			if(L<=R)ans+=seg.prod(L,R).first;
			else ans+=seg.prod(L,M).first+seg.prod(0,R).first;
		}
		mint s=seg.all_prod().first;
		seg.apply(0,M,make_pair(mint(-1),s));
		if(sign[i]==-1)off=(off-1+M)%M;
		else off=(off+1)%M;
	}
	cout<<ans.val()<<endl;
}
0