Codeforces Round #705 (Div. 2)D. GCD of an Array (multiset)

https://codeforces.com/contest/1493/problem/D

  1 #define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  2 #define bug(x) cout<<#x<<" is "<<x<<endl
  3 #include <bits/stdc++.h>
  4 #define iter ::iterator
  5 using namespace  std;
  6 typedef long long ll;
  7 typedef pair<int,ll>P;
  8 #define pb push_back
  9 #define mk make_pair
 10 #define se second
 11 #define fi first
 12 #define rs o*2+1
 13 #define ls o*2
 14 
 15 inline ll read(){ll x=0,f=1;char ch=getchar();  while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
 16 return x*f;}
 17 
 18 const ll mod=1e9+7;
 19 
 20 const int N=2e5+5;
 21 
 22 int T,n,cnt;
 23 
 24 ll ans=1;
 25 
 26 multiset<int>s[N];
 27 
 28 map<int,int>mp[N];
 29 
 30 ll qp(ll x,ll y){
 31     ll res=1;
 32     while(y){
 33         if(y&1)res=res*x%mod;
 34         x=x*x%mod;
 35         y>>=1;
 36     }
 37     return res;
 38 }
 39 
 40 void gao1(int id,int x,int y){
 41     if(!mp[id][x]){
 42         mp[id][x]=y;
 43         s[x].insert(y);
 44         if(s[x].size()==n){
 45             ans=ans*qp(x,*s[x].begin())%mod;
 46         }
 47         return;
 48     }
 49     if(s[x].size()==n){
 50         ll res=qp(x,*s[x].begin());
 51         ans=ans*qp(res,mod-2)%mod;
 52     }
 53     s[x].erase(s[x].find(mp[id][x]));
 54     mp[id][x]+=y;
 55     s[x].insert(mp[id][x]);
 56     if(s[x].size()==n){
 57         ans=ans*qp(x,*s[x].begin())%mod;
 58     }
 59 }
 60 
 61 void gao(int id,int x){
 62     int h=sqrt(x);
 63     for(int i=2;i<=h;i++){
 64         if(x%i)continue;
 65         ll tot=0;
 66         while(x%i==0){
 67             tot++;
 68             x/=i;
 69         }
 70         gao1(id,i,tot);
 71     }
 72     if(x>1)gao1(id,x,1);
 73 }
 74 
 75 int main(){ 
 76     n=read();T=read();
 77 
 78     for(int i=1;i<=n;i++){
 79         int x;
 80         x=read();
 81         gao(i,x);
 82     }
 83     while(T--){
 84         int id,x;
 85         id=read();
 86         x=read();
 87         gao(id,x);
 88         printf("%lld\n",ans);
 89     }
 90 
 91 
 92 }
 93 /*
 94 
 95 8 24
 96 1 5 6 2 3 30 10 15
 97 8 2
 98 6 1
 99 4 3
100 5 2
101 2 6
102 1 6
103 
104 
105 
106 */

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章