結果

問題 No.2653 [Cherry 6th Tune] Re: start! (Make it Zero!)
ユーザー shobonvipshobonvip
提出日時 2024-02-23 23:42:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 4,085 bytes
コンパイル時間 6,041 ms
コンパイル使用メモリ 263,756 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-23 23:43:09
合計ジャッジ時間 11,606 ms
ジャッジサーバーID
(参考情報)
judge16 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 29 ms
6,676 KB
testcase_02 AC 33 ms
6,676 KB
testcase_03 AC 33 ms
6,676 KB
testcase_04 AC 36 ms
6,676 KB
testcase_05 AC 35 ms
6,676 KB
testcase_06 AC 39 ms
6,676 KB
testcase_07 AC 41 ms
6,676 KB
testcase_08 AC 42 ms
6,676 KB
testcase_09 AC 40 ms
6,676 KB
testcase_10 AC 40 ms
6,676 KB
testcase_11 AC 40 ms
6,676 KB
testcase_12 AC 40 ms
6,676 KB
testcase_13 AC 26 ms
6,676 KB
testcase_14 AC 27 ms
6,676 KB
testcase_15 AC 27 ms
6,676 KB
testcase_16 AC 27 ms
6,676 KB
testcase_17 AC 27 ms
6,676 KB
testcase_18 AC 27 ms
6,676 KB
testcase_19 AC 26 ms
6,676 KB
testcase_20 AC 26 ms
6,676 KB
testcase_21 AC 27 ms
6,676 KB
testcase_22 AC 38 ms
6,676 KB
testcase_23 AC 28 ms
6,676 KB
testcase_24 AC 28 ms
6,676 KB
testcase_25 AC 29 ms
6,676 KB
testcase_26 AC 29 ms
6,676 KB
testcase_27 AC 29 ms
6,676 KB
testcase_28 AC 29 ms
6,676 KB
testcase_29 AC 29 ms
6,676 KB
testcase_30 AC 28 ms
6,676 KB
testcase_31 AC 29 ms
6,676 KB
testcase_32 AC 29 ms
6,676 KB
testcase_33 AC 30 ms
6,676 KB
testcase_34 AC 32 ms
6,676 KB
testcase_35 AC 32 ms
6,676 KB
testcase_36 AC 32 ms
6,676 KB
testcase_37 AC 33 ms
6,676 KB
testcase_38 AC 33 ms
6,676 KB
testcase_39 AC 31 ms
6,676 KB
testcase_40 AC 32 ms
6,676 KB
testcase_41 AC 32 ms
6,676 KB
testcase_42 AC 29 ms
6,676 KB
testcase_43 AC 36 ms
6,676 KB
testcase_44 AC 35 ms
6,676 KB
testcase_45 AC 35 ms
6,676 KB
testcase_46 AC 36 ms
6,676 KB
testcase_47 AC 36 ms
6,676 KB
testcase_48 AC 36 ms
6,676 KB
testcase_49 AC 36 ms
6,676 KB
testcase_50 AC 35 ms
6,676 KB
testcase_51 AC 36 ms
6,676 KB
testcase_52 AC 36 ms
6,676 KB
testcase_53 AC 31 ms
6,676 KB
testcase_54 AC 34 ms
6,676 KB
testcase_55 AC 41 ms
6,676 KB
testcase_56 AC 29 ms
6,676 KB
testcase_57 AC 33 ms
6,676 KB
testcase_58 AC 31 ms
6,676 KB
testcase_59 AC 33 ms
6,676 KB
testcase_60 AC 32 ms
6,676 KB
testcase_61 AC 36 ms
6,676 KB
testcase_62 AC 36 ms
6,676 KB
testcase_63 AC 28 ms
6,676 KB
testcase_64 AC 29 ms
6,676 KB
testcase_65 AC 33 ms
6,676 KB
testcase_66 AC 31 ms
6,676 KB
testcase_67 AC 26 ms
6,676 KB
testcase_68 AC 26 ms
6,676 KB
testcase_69 AC 26 ms
6,676 KB
testcase_70 AC 25 ms
6,676 KB
testcase_71 AC 26 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}


// [ DUAL SEGMENT TREE ] BY SHOBON
// REFERENCE --- ATCODER LIBRARY
// VERSION 1.0 (2022/08/28)

int ceil_pow2(int n) {
	int x = 0;
	while ((1U << x) < (unsigned int)(n)) x++;
	return x;
}

template <class F, F (*composition)(F, F), F (*id)()> struct dual_segtree{
		public:
			dual_segtree() : dual_segtree(0) {}
			explicit dual_segtree(int n) : dual_segtree(std::vector<F>(n, id())) {}
			explicit dual_segtree(const std::vector<F>& v) : _n(int(v.size())) {
				log = ceil_pow2(_n);
				size = 1 << log;
				lz = std::vector<F>(2 * size, id());
				for (int i = 0; i < _n; i++) lz[size + i] = v[i];
			}
 
			void apply(int l, int r, F f){
				assert (0 <= l && l <= r && r <= _n);
				if (l == r) return;
 
				l += size;
				r += size;
 
				for (int i = log; i >= 1; i--){
					if (((l >> i) << i) != l) push(l >> i);
					if (((r >> i) << i) != r) push((r - 1) >> i);
				}
 
				while (l < r){
					if (l & 1) all_apply(l++, f);
					if (r & 1) all_apply(--r, f);
					l >>= 1;
					r >>= 1;
				}
			}
			
			F get(int p) {
				assert(0 <= p && p < _n);
				p += size;
				for (int i = log; i >= 1; i--) push(p >> i);
				return lz[p];
			}
		
		private:
			int _n, size, log;
			std::vector<F> lz;
 
			void all_apply(int k, F f){
				lz[k] = composition(f, lz[k]);
			}
 
			void push(int k){
				all_apply(2 * k, lz[k]);
				all_apply(2 * k + 1, lz[k]);
				lz[k] = id();
			}
 
};

// ---- END : DUAL SEGMENT TREE ----

struct F{
	mint b;
	mint c;
};

F composition(F g, F f){
	return {f.b * g.b, f.c * g.b + g.c};
}

F id(){
	return {1, 0};
}

// defcomp
template <typename T>
vector<T> compress(vector<T> &X) {
	vector<T> vals = X;
	sort(vals.begin(), vals.end());
	vals.erase(unique(vals.begin(), vals.end()), vals.end());
	return vals;
}
// -----

// importbisect
template <typename T>
int bisect_left(vector<T> &X, T v){
	return lower_bound(X.begin(), X.end(), v) - X.begin();
}

template <typename T>
int bisect_right(vector<T> &X, T v){
	return upper_bound(X.begin(), X.end(), v) - X.begin();
}
// -----


void solve(){
	int n, m; cin >> n >> m;
	vector<int> x(n);
	vector<ll> battle(n + 1);
	
	ll tmp = 0;
	rep(i,0,n){
		cin >> x[i];
		if (x[i] >= 0){
			tmp += x[i];
			tmp %= m;
		}
		battle[i + 1] = tmp;
	}

	int cnt = 0;
	rep(i,0,n){
		if (x[i] == -1) cnt++;
	}

	if (cnt == 0){
		ll tmp = 0;
		ll ans = 0;
		rep(i,0,n){
			tmp += x[i];
			tmp %= m;
			if (tmp != 0){
				ans++;
			}
		}
		if (tmp != 0){
			ans = 0;
		}
		cout << ans << '\n';
		return;
	}

	int now = (x[0] == -1);
	mint ans = 0;
	rep(i,0,n-1){
		// i - i+1
		ll tar = 0;
		bool mode = 1;
		
		if (now == 0){
			if (battle[i + 1] != 0) mode = 0;
		}else{
			tar += now - 1;
		}

		if (cnt - now == 0){
			if (battle[n] - battle[i + 1] != 0) mode = 0;
		}else{
			tar += cnt - now - 1;
		}
		
		if (mode){
			ans -= mint(m).pow(tar);
		}
		
		now += (x[i+1] == -1);

	}

	ans = mint(m).pow(cnt - 1) * (n - 1) + ans;
	cout << ans.val() << '\n';

}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t; cin >> t;
	while(t--) solve();
}

0