結果

問題 No.2578 Jewelry Store
ユーザー pockynypockyny
提出日時 2023-12-06 03:37:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,774 bytes
コンパイル時間 2,698 ms
コンパイル使用メモリ 220,208 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-09-27 01:02:02
合計ジャッジ時間 17,734 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 22 ms
5,376 KB
testcase_03 AC 9 ms
5,376 KB
testcase_04 AC 84 ms
5,376 KB
testcase_05 AC 7 ms
5,376 KB
testcase_06 AC 23 ms
5,376 KB
testcase_07 AC 85 ms
5,376 KB
testcase_08 AC 10 ms
5,376 KB
testcase_09 AC 85 ms
5,376 KB
testcase_10 AC 177 ms
5,376 KB
testcase_11 AC 8 ms
5,376 KB
testcase_12 AC 9 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 7 ms
5,376 KB
testcase_15 AC 7 ms
5,376 KB
testcase_16 AC 171 ms
5,376 KB
testcase_17 AC 84 ms
5,376 KB
testcase_18 AC 7 ms
5,376 KB
testcase_19 AC 43 ms
5,376 KB
testcase_20 AC 83 ms
5,376 KB
testcase_21 AC 85 ms
5,376 KB
testcase_22 AC 19 ms
5,376 KB
testcase_23 AC 42 ms
5,376 KB
testcase_24 AC 14 ms
5,376 KB
testcase_25 AC 22 ms
5,376 KB
testcase_26 AC 16 ms
5,376 KB
testcase_27 AC 230 ms
5,376 KB
testcase_28 AC 446 ms
5,376 KB
testcase_29 AC 425 ms
5,376 KB
testcase_30 AC 635 ms
5,376 KB
testcase_31 AC 240 ms
5,376 KB
testcase_32 AC 151 ms
7,808 KB
testcase_33 AC 192 ms
5,760 KB
testcase_34 AC 242 ms
7,876 KB
testcase_35 AC 60 ms
5,376 KB
testcase_36 AC 211 ms
8,556 KB
testcase_37 AC 114 ms
5,376 KB
testcase_38 AC 140 ms
5,376 KB
testcase_39 AC 129 ms
5,376 KB
testcase_40 AC 139 ms
5,376 KB
testcase_41 AC 126 ms
5,376 KB
testcase_42 AC 115 ms
5,376 KB
testcase_43 AC 179 ms
5,376 KB
testcase_44 AC 100 ms
5,376 KB
testcase_45 AC 155 ms
5,376 KB
testcase_46 AC 124 ms
5,376 KB
testcase_47 AC 1,501 ms
5,376 KB
testcase_48 AC 317 ms
11,264 KB
testcase_49 TLE -
testcase_50 TLE -
testcase_51 AC 111 ms
5,376 KB
testcase_52 AC 597 ms
5,376 KB
testcase_53 AC 56 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T> int sz(T&& a) { return int(size(forward<T>(a))); }

template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;

using ll = int64_t;
using vi = vc<int>;
using pii = pair<int, int>;
using uint = uint32_t;
using ull = uint64_t;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

template <class F>
struct ycr {
	F f;
	template <class T> explicit ycr(T&& f_) : f(forward<T>(f_)) {}

	template <class... Args> decltype(auto) operator()(Args&&... args) {
		return f(ref(*this), forward<Args>(args)...);
	}
};
template <class F> decltype(auto) yc(F&& f) {
	return ycr<decay_t<F>>(forward<F>(f));
}

/**
 * Author: Hanfei Chen
 * Date: 2023-11-30
 * Description: Very fast factorization.
 * Source: https://judge.yosupo.jp/submission/110118
 * Status: Tested with https://judge.yosupo.jp/problem/factorize
 */
struct montgomery {
	using T = ull; /// start-hash
	using u128 = __uint128_t;
	T n, n2, r, t, e;
	montgomery(T n_) : n(n_) {
		assert(n < (T(1) << 62));
		assert(n % 2 == 1);
		n2 = n * 2;
		r = n & 3;
		for (int z = 0; z < 5; z++) r *= 2-n*r;
		r = -r;
		assert(r * n == T(-1));
		t = -T(n) % n;
		e = T(-u128(n) % n);
	} /// end-hash

	T reduce(u128 a) const { /// start-hash
		return T((a + u128(T(a) * r) * n) >> 64);
	}
	T mult(T a, T b) const {
		return reduce(u128(a) * b);
	} /// end-hash

	T encode(T a) const { /// start-hash
		return mult(a, e);
	}
	T decode(T a) const {
		a = reduce(a);
		return a < n ? a : a-n;
	} /// end-hash

	T pow(T a, ll b) const { /// start-hash
		assert(b >= 0);
		T v = t;
		while (b) {
			if (b & 1) v = mult(v, a);
			a = mult(a, a);
			b >>= 1;
		}
		return v;
	} /// end-hash

	bool is_prime() const {
		assert(n >= 3);
		assert(n & 1);

		T d = n-1; /// start-hash
		int s = __builtin_ctzll(d);
		d >>= s;
		auto check = [&](T a) -> int {
			a %= n;
			if (a == 0) return 1;
			T p = pow(encode(a), d);
			if (decode(p) == 1 || decode(p) == n-1) return 0;
			for (int z = 0; z < s; z++) {
				p = mult(p, p);
				if (decode(p) == n-1) return 0;
			}
			return -1;
		}; /// end-hash

		for (T a : {2,325,9375,28178,450775,9780504,1795265022}) { /// start-hash
			int w = check(a);
			if (w) return w == 1;
		}
		return true; /// end-hash
	}

	ull pollard() const {
		assert(n >= 3);
		assert(n & 1);

		if (is_prime()) return n; /// start-hash
		while (true) {
			T c = mt() % (n-1) + 1;
			T y = mt() % (n-1) + 1;
			auto f = [&](T a) -> T {
				return reduce(u128(a) * a + c);
			};
			for (T s = 1; ; s *= 2) {
				T x = n2-y;
				int m = int(min(T(1) << (__lg(n)/6), s));
				for (int i = 0; i < int(s/m); i++) {
					T w = t, z = y;
					for (int j = 0; j < m; j++) {
						y = f(y);
						w = mult(w, y+x);
					}
					T g = __gcd(decode(w), n);
					if (g != 1) {
						if (g < n) return g;
						for (int j = 0; j < m; j++) {
							z = f(z);
							if ((g = __gcd(decode(z+x), n)) != 1) {
								if (g < n) return g;
								goto fail;
							}
						}
						assert(false);
					}
				}
			}
fail:;
		} /// end-hash
	}

	// facs = {prime factors of (n-1)}
	bool is_root(T g, const vc<T>& facs) { /// start-hash
		assert(g >= 2);
		g = encode(g);
		for (auto f : facs) {
			if (decode(pow(g, (n-1) / f)) == 1) return false;
		}
		return true;
	} /// end-hash
};

bool is_prime(ull n) { /// start-hash
	if (n <= 1) return false;
	if (n == 2) return true;
	if (n % 2 == 0) return false;
	return montgomery(n).is_prime();
} /// end-hash

ull get_divisor(ull n) { /// start-hash
	assert(n > 1);
	if (n % 2 == 0) return 2;
	return montgomery(n).pollard();
} /// end-hash

vc<ull> factorize(ull n) { /// start-hash
	vc<ull> res;
	yc([&](auto self, ull v) -> void {
		if (v == 1) return;
		ull d = get_divisor(v);
		if (d == v) {
			res.push_back(d);
			return;
		}
		self(d);
		self(v/d);
	})(n);
	return res;
} /// end-hash

ll mod = 998244353;
ll dp[1<<15];
int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	cout << fixed << setprecision(20);
	int T; ll m;
	cin >> T >> m;
    ll x = m;
    auto res = factorize(x);
    map<ll,ll> mp;
    for(auto x:res) mp[x]++;
    vector<pair<ll,ll>> di;
    for(auto p:mp) di.push_back({p.first,p.second});
    while(T){
        T--;
        ll i,j,n,b,c,d;
        cin >> n >> b >> c >> d;
        vector<ll> a(n),w(n);
        for(i=0;i<n;i++) cin >> a[i];
        w[0] = b;
        for(i=1;i<n;i++) w[i] = (c*w[i - 1] + d)%mod;
        for(i=0;i<(1<<di.size());i++) dp[i] = 1;
        for(i=0;i<n;i++){
            if(m%a[i]==0 && w[i]){
                ll x = 0;
                int j = 0;
                for(auto p:di){
                    ll cnt = 0;
                    while(a[i]%p.first==0){
                        a[i] /= p.first; cnt++;
                    }
                    if(cnt<p.second) x |= 1<<j;
                    j++; 
                }
                (dp[x] *= (1 + w[i])) %= mod;
            }
        }
        //cout << dp[0] << " " << dp[1] << " " << dp[2] << " " << dp[3] << endl;
        for(j=0;j<di.size();j++){
            for(i=0;i<(1<<di.size());i++){
                if(!(i>>j&1)) (dp[i] *= dp[i^(1<<j)]) %= mod;
            }
        }
        ll ans = 0;
        for(i=0;i<(1<<di.size());i++){
            int c = 0;
            for(j=0;j<di.size();j++){
                if(i>>j&1) c = 1 - c;
            }
            if(!c) (ans += dp[i]) %= mod;
            else (ans += mod - dp[i]) %= mod;
        }
        if(m==1) (ans += mod - 1) %= mod;
        cout << ans << "\n";
    }
	// while (T--) {
	// 	ull n;
	// 	cin >> n;
	// 	auto res = factorize(n);
	// 	sort(res.begin(), res.end());
	// 	cout << res.size();
	// 	for (auto p : res) {
	// 		cout << ' ' << p;
	// 	}
	// 	cout << '\n';
	// }

	// return 0;
}
0