求1<=i<=n&&1<=j<=m,gcd(i,j)=k的(i,j)的对数
最后的结果f(k)=Σ(1<=x<=n/k)mu[x]*(n/(x*k))*(m/(x*k))
遍历的复杂度是O(n/k),按理来说是会t的,但是这题过了,更好的办法是用分块降低到O(sqrt(n/k))
详细介绍请看:链接
这题要(i,j)和(j,i)算重复的,所以要减去
//#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pii pair<int,int> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-12; const int N=100000+10,maxn=400000+10,inf=0x3f3f3f3f; int mu[N],prime[N],sum[N]; bool mark[N]; void init() { mu[1]=1; int cnt=0; for(int i=2;i<N;i++) { if(!mark[i])prime[++cnt]=i,mu[i]=-1; for(int j=1;j<=cnt;j++) { int t=i*prime[j]; if(t>N)break; mark[t]=1; if(i%prime[j]==0){mu[t]=0;break;} else mu[t]=-mu[i]; } } for(int i=1;i<N;i++)sum[i]=sum[i-1]+mu[i]; } int main() { init(); int t,cnt=0; scanf("%d",&t); while(t--) { ll a,b,c,d,k; scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k); if(!k) { printf("Case %d: 0 ",++cnt); continue; } if(b>d)swap(b,d); b/=k,d/=k; ll ans=0,ans1=0; for(ll i=1,last=1;i<=b;i=last+1) { last=min(b/(b/i),d/(d/i)); ans+=(ll)(sum[last]-sum[i-1])*(b/i)*(d/i); } for(ll i=1,last=1;i<=b;i=last+1) { last=b/(b/i); ans1+=(ll)(sum[last]-sum[i-1])*(b/i)*(b/i); } printf("Case %d: %lld ",++cnt,ans-ans1/2); } return 0; } /******************** ********************/