結果
問題 | No.2497 GCD of LCMs |
ユーザー | 蜜蜂 |
提出日時 | 2023-10-13 01:32:37 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 33 ms / 2,000 ms |
コード長 | 5,739 bytes |
コンパイル時間 | 2,767 ms |
コンパイル使用メモリ | 204,532 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-14 23:18:11 |
合計ジャッジ時間 | 3,779 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 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 | 2 ms
5,376 KB |
testcase_07 | AC | 5 ms
5,376 KB |
testcase_08 | AC | 14 ms
5,376 KB |
testcase_09 | AC | 16 ms
5,376 KB |
testcase_10 | AC | 20 ms
5,376 KB |
testcase_11 | AC | 10 ms
5,376 KB |
testcase_12 | AC | 26 ms
5,376 KB |
testcase_13 | AC | 32 ms
5,376 KB |
testcase_14 | AC | 6 ms
5,376 KB |
testcase_15 | AC | 6 ms
5,376 KB |
testcase_16 | AC | 33 ms
5,376 KB |
コンパイルメッセージ
main.cpp: In function 'std::vector<long long int> divisor(std::vector<std::pair<long long int, int> >&)': main.cpp:213:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 213 | auto [a,b]=fac[i]; | ^ main.cpp: In function 'int main()': main.cpp:249:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 249 | for(auto [p,c]:f)prime.emplace_back(p); | ^ main.cpp:276:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 276 | auto [u,v]=e[i]; | ^ main.cpp:284:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 284 | auto [u,v]=e[id]; | ^
ソースコード
// g++-13 1.cpp -std=c++17 -O2 -I . #include <bits/stdc++.h> using namespace std; #include <atcoder/dsu> #include <atcoder/modint> using namespace atcoder; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vld = vector<ld>; using vvld = vector<vld>; using vst = vector<string>; using vvst = vector<vst>; #define fi first #define se second #define pb push_back #define eb emplace_back #define pq_big(T) priority_queue<T,vector<T>,less<T>> #define pq_small(T) priority_queue<T,vector<T>,greater<T>> #define all(a) a.begin(),a.end() #define rep(i,start,end) for(ll i=start;i<(ll)(end);i++) #define per(i,start,end) for(ll i=start;i>=(ll)(end);i--) #define uniq(a) sort(all(a));a.erase(unique(all(a)),a.end()) //https://atcoder.jp/contests/arc122/submissions/24436348 //https://judge.yosupo.jp/submission/53811 //https://yukicoder.me/submissions/680896 //https://atcoder.jp/contests/abc180/submissions/24436784 #include <cassert> #include <cmath> #include <initializer_list> #include <random> #include <iostream> #include <algorithm> std::mt19937_64 mt{std::random_device{}()}; long long rnd(long long n) { return std::uniform_int_distribution<long long>(0, n-1)(mt); } bool is_prime(long long x){ using u128=__uint128_t; if(x==2||x==3||x==5||x==7)return true; if(x%2==0||x%3==0||x%5==0||x%7==0)return false; if(x<121){ return x>1; } long long d = (x-1) >> __builtin_ctzll(x-1); long long one=1,minus_one=x-1; auto pow = [](long long x,long long n,long long mod){ u128 res; x%=mod; if(n==0){ res=1;return res; } res=1; u128 now=x; for(;n;n>>=1,now=(now*now)%mod){ if(n&1){ res=res*now%mod; } } return res; }; auto ok = [&](long long a){ auto y=pow(a,d,x); long long t=d; while(y!=one&&y!=minus_one&&t!=x-1){ y=y*y%x; t<<=1; } if(y!=minus_one&&t%2==0)return false; return true; }; if(x<(1ull<<32)){ for(long long a:{2,7,61}){ if(!ok(a))return false; } } else{ for(long long a:{2,325,9375,28178,450775,9780504,1795265022}){ if(x<=a)return true; if(!ok(a))return false; } } return true; } long long GCD(long long a,long long b){ if(b==0){ return a; } else{ return GCD(b,(a%b)); } } long long rho(long long n,long long c){ using u128=__uint128_t; assert(n>1); long long cc=c; auto f = [&](long long x){ long long res; res=(((u128)x)*x+cc)%n; return res; }; long long x=1,y=2,z=1,q=1; long long g=1; long long m= 1LL<<((64-__builtin_ctzll(n))/5); for(long long r=1;g==1;r<<=1){ x=y; for(int i=0;i<r;i++){ y=f(y); } for(long long k=0;k<r&&g==1;k+=m){ z=y; for(long long i=0;i<std::min(m,r-k);i++){ y=f(y); long long w=(x-y)%n; if(w<0)w+=n; q=((u128)q*w)%n; } g=GCD(q,n); } } if(g==n){ do{ z=f(z); long long w=(x-z)%n; if(w<0)w+=n; g=GCD(w,n); }while(g==1); } return g; } long long prime_factor(long long n){ assert(n>1); if(is_prime(n)) return n; for(int i=0;i<100;i++){ long long m=rho(n,rnd(n)); if(is_prime(m))return m; n=m; } std::cerr<<"failed\n"; assert(false); return -1; } //nを素因数分解 {素因数,個数} で管理 (1は空配列) std::vector<std::pair<long long,int>> factor(long long n){ static std::vector<std::pair<long long,int>> res; res.clear(); for(long long i=2;i<=100&&i*i<=n;++i){ if(n%i==0){ int cnt=0; do{ cnt++; n/=i; }while(n%i==0); res.emplace_back(i,cnt); } } while(n>1){ long long p=prime_factor(n); int cnt=0; do{ cnt++; n/=p; }while(n%p==0); res.emplace_back(p,cnt); } std::sort(res.begin(),res.end()); return res; } //nの約数配列(昇順) 引数はfactor(n) std::vector<long long> divisor(std::vector<std::pair<long long,int>> &fac){ int sz=fac.size(); //n=1の時 if(sz==0){ return {1}; } std::vector<std::vector<long long>> x(sz); for(int i=0;i<sz;i++){ auto [a,b]=fac[i]; long long now=1; for(int j=0;j<=b;j++){ x[i].emplace_back(now); now*=a; } } std::vector<std::vector<long long>> res(sz); res[0]=x[0]; for(int i=1;i<sz;i++){ for(int j=0;j<res[i-1].size();j++){ for(int k=0;k<x[i].size();k++){ res[i].emplace_back(res[i-1][j]*x[i][k]); } } } std::sort(res[sz-1].begin(),res[sz-1].end()); return res[sz-1]; } using mint = modint998244353; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,m;cin>>n>>m; vi a(n); rep(i,0,n)cin>>a[i]; vi prime; rep(i,0,n){ auto f=factor(a[i]); for(auto [p,c]:f)prime.emplace_back(p); } uniq(prime); vector<pair<int,int>> e(m); rep(i,0,m){ int u,v;cin>>u>>v; u--;v--; e[i]={u,v}; } vector<mint> ans(n,(mint)1); for(auto p:prime){ vi cnt(n,0); int mx=0; rep(i,0,n){ int a2=a[i]; while(a2%p==0){ cnt[i]++; a2/=p; } mx=max(mx,cnt[i]); } vvi e_id(mx+1); rep(i,0,m){ auto [u,v]=e[i]; e_id[max(cnt[u],cnt[v])].emplace_back(i); } vi now(n,-1); dsu d(n); rep(i,0,mx+1){ for(auto id:e_id[i]){ auto [u,v]=e[id]; d.merge(u,v); } rep(j,0,n){ if(now[j]==-1&&d.same(0,j))now[j]=i; } } vector<mint> pd(mx+1,(mint)1); rep(i,1,mx+1)pd[i]=pd[i-1]*p; rep(i,0,n)ans[i]*=pd[now[i]]; } rep(i,0,n){ if(i==0)cout<<a[0]<<endl; else cout<<ans[i].val()<<endl; } }