We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
frommathimportceildefgcd(a,b):ifa>b:returngcd(b,a)whilea!=0:a,b=b%a,areturnbdefproduct(lst):prod=1forninlst:prod=(prod*n)%(10**9+7)returnprod# Have a global variable denote the bounds# Have the function denote how much to divide the bounds bybounds=Nonecache=Nonedefcoprime_count(divisor):ifdivisor>bounds[-1]// 2:return1ifdivisorincache:returncache[divisor]# bounds are assumed to be sortedscaled_bounds=[n// divisor for n in bounds]total=product(scaled_bounds)cap=scaled_bounds[0]# total = sum d from 1 to lowest bound coprime_count(d) (inclusive)rest=sum(coprime_count(divisor*d)fordinrange(2,cap+1))cache[divisor]=(total-rest)%(10**9+7)returncache[divisor]t=int(input())for_inrange(t):k=int(input())bounds=list(map(int,input().split()))bounds.sort()cache=dict()total=0foriinrange(1,bounds[0]+1):total=(total+i*coprime_count(i))%(10**9+7)print(total)
For anyone interested in O(T*(Nmin + sqrt(N)*K)) + O(N) preprocessing approach :
//https://www.codechef.com/viewsolution/44723407#include<bits/stdc++.h>#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);typedeflonglongll;typedeflongdoubleld;#define pb push_back#define mp make_pair#define ff first#define ss second#define mod 1000000007#define pii pair<ll,ll>#define inf 1000000000000000000#define bpc(x) __builtin_popcountll(x)#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)#define rep(n) for(ll i = 0; i < n; i++)#define repi(i,n) for(ll i = 0; i < n; i++)#define hmap gp_hash_table<ll, ll>#include<ext/pb_ds/assoc_container.hpp>#include<ext/pb_ds/tree_policy.hpp>usingnamespace__gnu_pbds;#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>usingnamespacestd;mt19937_64mt(chrono::steady_clock::now().time_since_epoch().count());vector<ll>mu,phi,primes,f;voidcalc(lln){mu.resize(n+1);phi.resize(n+1);f.resize(n+1);primes.clear();vector<ll>nums(n+1);nums[1]=1;f[1]=1;boolisp[n+1];rep(n+1)isp[i]=true;isp[0]=isp[1]=false;mu[1]=1,phi[1]=1;for(lli=2;i<=n;i++){if(isp[i]){phi[i]=i-1;primes.pb(i);mu[i]=-1;nums[i]=i;f[i]=(i-1)%mod;////!!!!!HANDLE THIS CASE FOR A FUNCTION//When number is a prime}lltmp=n/i;for(llj=0;j<primes.size()&&primes[j]<=tmp;j++){llcurr=primes[j]*i;isp[curr]=false;if(i%primes[j]==0){nums[curr]=nums[i]*primes[j];phi[curr]=primes[j]*phi[i];mu[curr]=0;if(curr==nums[curr])f[curr]=(curr-i)%mod;//!!!!!HANDLE THIS CASE FOR A FUNCTION//when number is some power(>=2) of primes[j]elsef[curr]=(f[curr/nums[curr]]*f[nums[curr]])%mod;//when there are at least 2 primes and power of primes[j] is atleast 2 in currbreak;}nums[curr]=primes[j];mu[curr]=(mu[i]*mu[primes[j]]);phi[curr]=(phi[i]*phi[primes[j]]);f[curr]=(f[primes[j]]*f[i])%mod;//When number has only single power of this prime}}}llpowa(lla,llb,llc){a%=c;if(a<0)a+=c;llres=1;while(b>0){if(b&1)res*=a,res%=c;a*=a,a%=c;b>>=1;}returnres;}llnorm(lla){a%=mod;if(a<0)a+=mod;returna;}lladd(lla,llb){a=norm(a);b=norm(b);returnnorm(a+b);}llmul(lla,llb){a=norm(a);b=norm(b);returnnorm(a*b);}llsub(lla,llb){a=norm(a);b=norm(b);returnnorm(a-b);}lldivi(lla,llb){b=powa(b,mod-2,mod);returnmul(a,b);}#define N 100005intmain(){FAST/**/llinv[N];inv[0]=0;inv[1]=1;for(lli=2;i<N;i++)inv[i]=divi(1,i);llt;cin>>t;calc(N-1);while(t--){llk;cin>>k;llarr[k];llmx=N;rep(k)cin>>arr[i],mx=min(mx,arr[i]);llppr[N];rep(N)ppr[i]=1;for(lli=0;i<k;i++){llval=arr[i];llid=1;for(id=1;id*id<=val;id++){ppr[id]=mul(ppr[id],val/id);ppr[id+1]=mul(ppr[id+1],inv[val/id]);}//assert(id>0);for(llcid=val/id;cid>0;cid--){lll=(val+1+cid)/(cid+1),r=(val/cid);l=max({l,id});r=min({r,val});if(l>r)continue;ppr[l]=mul(ppr[l],cid);ppr[r+1]=mul(ppr[r+1],inv[cid]);}}for(lli=2;i<=mx;i++)ppr[i]=mul(ppr[i-1],ppr[i]);llans=0;for(lli=1;i<=mx;i++){lltmp=ppr[i];tmp=mul(tmp,f[i]);ans=add(ans,tmp);}cout<<ans<<"\n";}return0;}
Python3 :-)
For anyone interested in O(T*(Nmin + sqrt(N)*K)) + O(N) preprocessing approach :
Can be done in O(T*sqrt(N)*K)...
O(TNK) passes :| Judge data is weak.