結果

問題 No.1731 Product of Subsequence
ユーザー shobonvip
提出日時 2023-05-26 21:13:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,060 bytes
コンパイル時間 3,867 ms
コンパイル使用メモリ 261,680 KB
最終ジャッジ日時 2025-02-13 05:25:19
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15 TLE * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef modint1000000007 mint;
typedef long long ll;

vector<pair<int,int>> pfact(int n){
	vector<pair<int,int>> ret;
	for (int i=2; i*i<=n; i++){
		if (n%i == 0){
			int cnt = 0;
			while (n%i == 0){
				n /= i;
				cnt++;
			}
			ret.push_back(pair(i, cnt));
		}
	}
	if (n > 1) ret.push_back(pair(n, 1));
	return ret;
}

int main(){
	int n, k; cin >> n >> k;
	vector<pair<int,int>> pf = pfact(k);
	vector<int> pl;
	for (auto [p,c]: pf){
		pl.push_back(p);
	}
	vector<ll> a(n);
	for (int i=0; i<n; i++){
		cin >> a[i];
		ll nw = 1;
		for (int j: pl){
			while (a[i] % j == 0){
				nw *= j;
				a[i] /= j;
			}
		}
		a[i] = nw % k;
	}
	map<int,mint> s;
	s[1] = 1;
	for (int i=0; i<n; i++){
		map<int,mint> ns = s;
		for (auto j=s.begin(); j!=s.end(); j++){
			int tmp = (ll)j->first * a[i] % k;
			if (ns.find(tmp) == ns.end()){
				ns[tmp] = 0;
			}
			ns[tmp] += j->second;
		}
		s = ns;
	}
	if (s.find(0) == s.end()){
		s[0] = 0;
	}
	cout << s[0].val() << endl;
}
0