# bzoj3309DZY Loves Math

## 3309: DZY Loves Math

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 1240  Solved: 777
[Submit][Status][Discuss]

4
7558588 9653114
6514903 4451211
7425644 1189442
6335198 4957

35793453939901
14225956593420
4332838845846
15400094813

## HINT

【数据规模】

T<=10000

1<=a,b<=10^7

 1 #include<bits/stdc++.h>
2 #define rint register int
3 #define N 10000001
4 using namespace std;
5 int cnt,sum[N],a[N],b[N],p[N>>1],vis[N];
6
7 int main(){
8     for(rint i=2;i<N;++i){
9         if(!vis[i]){p[++cnt]=i;a[i]=sum[i]=1;b[i]=i;}
10         for(rint j=1;j<=cnt&&p[j]*i<N;++j){
11             static int k,t;k=p[j]*i;vis[k]=1;
12             if(i%p[j]){a[k]=1;b[k]=p[j];sum[k]= a[i]==1?-sum[i]:0;}
13             else{
14                 a[k]=a[i]+1;b[k]=b[i]*p[j];t=i/b[i];
15                 if(t==1)sum[k]=1;else sum[k]= a[t]==a[k]?-sum[t]:0;
16                 break;
17             }
18         }
19     }
20     for(rint i=1;i<N;++i)sum[i]+=sum[i-1];
21     int T,n,m,pos;long long ans;scanf("%d",&T);
22     for(rint c=1;c<=T;++c){
23         scanf("%d%d",&n,&m);
24         if(n>m)swap(n,m);ans=0;
25         for(rint i=1;i<=n;i=pos+1){
26             pos=min(n/(n/i),m/(m/i));
27             ans+=1ll*(n/i)*(m/i)*(sum[pos]-sum[i-1]);
28         }
29         printf("%lld\n",ans);
30     }
31     return 0;
32 }

posted @ 2018-01-21 16:44  _wsy  阅读(131)  评论(0编辑  收藏  举报