有一张N×m的数表,其第i行第j列(1 < =i < =n,1 < =j < =m)的数值为能同时整除i和j的所有自然数之和。给定a,计算数表中不大于a的数之和模2^31的值模2^31的值。
1<=n.m<=10 5 ,Q<=2×10 4 组询问
记k的约数和f(k)
求∑ n i=1 ∑ m j=1 gcd(i,j)[f(gcd(i,j))≤a]
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case %d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<‘ ‘; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<‘ ‘; cout<<a[i][m]<<endl; }
typedef long long ll;
typedef unsigned long long ull;
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch==‘-‘) f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-‘0‘; ch=getchar();}
return x*f;
}
#define MAXN (100000+10)
int p[MAXN],tot;
bool b[MAXN]={0};
int mul[MAXN]={0};
pi F[MAXN]={};
void make_prime(int n)
{
tot=0; mul[1]=1;
Fork(i,2,n)
{
if (!b[i]) p[++tot]=i,mul[i]=-1;
For(j,tot)
{
if (i*p[j]>n) break;
b[i*p[j]]=1;
mul[i*p[j]]=-mul[i];
if (i%p[j]==0) {
mul[i*p[j]]=0;
break;
}
}
}
For(i,n)
for(int j=i;j<=n;j+=i) F[j].fi+=i;
For(i,n) F[i].se=i;
sort(F+1,F+1+n);
}
struct comm{
int n,m,a,id;
friend bool operator<(comm a,comm b){return a.a<b.a;}
}ask[MAXN];
int Q;
int ans[MAXN];
const int N = (int)1e5+1;
int t[MAXN]={0};
void add(int x,int y){
for(int i=x;i<=N;i+=i&(-i)) t[i]+=y;
}
int query(int x) {
int tmp=0;
for(int i=x;i;i-=i&(-i)) tmp+=t[i];
return tmp;
}
int solve(int x) {
int tmp=0;
int n=ask[x].n,m=ask[x].m;
for(int i=1;i<=n;) {
int j=min(n/(n/i),m/(m/i));
tmp+=(n/i)*(m/i)*(query(j)-query(i-1));
i=j+1;
}
return tmp;
}
int main()
{
// freopen("bzoj3529.in","r",stdin);
// freopen(".out","w",stdout);
make_prime(N);
Q=read();
For(i,Q) {
ask[i]=(comm){read(),read(),read(),i};
if (ask[i].n>ask[i].m) swap(ask[i].n,ask[i].m);
}
sort(ask+1,ask+1+Q);
int j=0;
For(i,Q) {
while(j<N && F[j+1].fi<=ask[i].a) {
++j;
for(int d=F[j].se;d<=N;d+=F[j].se) add(d,F[j].fi*mul[d/F[j].se]);
}
ans[ask[i].id]=solve(i);
}
For(i,Q) printf("%d\n",ans[i]&0x7fffffff);
return 0;
}
时间: 2024-10-12 07:59:54