QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#767532 | #9748. 最大公因数的平方和 | wtc | RE | 0ms | 0kb | C++14 | 1.3kb | 2024-11-20 21:12:13 | 2024-11-20 21:12:13 |
answer
#include<bits/stdc++.h>
#define fo(i,l,r) for(int i=(l);i<=(r);++i)
#define fd(i,l,r) for(int i=(l);i>=(r);--i)
#define fu(i,l,r) for(int i=(l);i<(r);++i)
#define ll long long
#define int ll
using namespace std;
const int N=100007,B=1000;
int n,Q,a[N],b[N],c[N],d[N],sum[N],bz[N];
ll f[N],g[N],ans[N];
vector<int>p[N];
struct Qry{
int l,r,bh,c;
};
vector<Qry>q[N];
void upd(int x,ll y){while(x<=n)g[x]+=y,x+=x&-x;}
ll qry(int x){ll s=0;while(x)s+=g[x],x-=x&-x;return s;}
void init(int n)
{
fo(i,1,n) f[i]=1ll*i*i;
fo(i,2,n)
if(!bz[i])
{
for(int j=n/i*i;j>=i;j-=i) f[j]-=f[j/i],bz[j]=1;
}
}
signed main()
{
scanf("%d",&n);
init(n);
fo(i,1,n) scanf("%d",&a[i]),c[a[i]]=i;
fo(i,1,n) scanf("%d",&b[i]),d[b[i]]=i;
fo(i,B+1,n)
{
for(int j=i;j<=n;j+=i)
p[c[j]].push_back(i);
}
scanf("%d",&Q);
fo(i,1,Q)
{
int l,r,u,v;
scanf("%d%d%d%d",&l,&r,&u,&v);
q[r].push_back((Qry){u,v,i,1});
q[l-1].push_back((Qry){u,v,i,-1});
}
fo(j,1,B)
{
fo(i,1,n) sum[i]=sum[i-1]+(b[i]%j==0);
int w=0;
fo(i,1,n)
{
w+=(a[i]%j==0);
for(Qry x:q[i]) ans[x.bh]+=1ll*(sum[x.r]-sum[x.l-1])*w*f[j]*x.c;
}
}
fo(i,1,n)
{
for(int x:p[i])
{
for(int y=x;y<=n;y+=x) upd(d[y],f[x]);
}
for(Qry x:q[i]) ans[x.bh]+=(qry(x.r)-qry(x.l-1))*x.c;
}
fo(i,1,Q) printf("%lld\n",ans[i]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
5 4 1 5 3 2 1 2 3 4 5 5 3 3 2 3 3 4 2 4 3 4 3 4 5 5 1 1 1 1 2 2