题意:每组数据给出5个整数a,b,c,d,k,并且保证a=c=1。。。然后求有多少对数对(x,y)&&gcd(x,y)=k&&a<=x<=b&&c<=y<=d,并且(x,y)与(y,x)算一组。
分析:首先我们将b/=k,d/=k。然后求1~b和1~d中有多少组数对互质即可。直接遍历1~b中每个数然后在1~d中查询有多少个互质的数即可,设有get(i,j)表示1~j中有多少与i互质的数的个数,为了避免重复计算我们计算是应该用get(i,d)-get(i,i-1)。那么求get(i,d)我们只要对i进行因式分解然后容斥即可。因为有get(i,d)-get(i,i-1)所以要保证b<=d,这里应该要注意一下。
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=200010;
const int MAX=151;
const int MOD=1000000007;
const int MOD1=100000007;
const int MOD2=100000009;
const int INF=1000000000;
const double EPS=0.00000001;
typedef long long ll;
typedef unsigned long long uI64;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int a[N],q[N],p[N][10];
void deal() {
int i,j,k=0,w;
memset(q,0,sizeof(q));
for (i=2;i<=100000;i++) {
if (!q[i]) a[++k]=i;
for (j=1;j<=k;j++) {
if (a[j]*i>100000) break ;
q[a[j]*i]=1;
if (i%a[j]==0) break ;
}
}
memset(p,0,sizeof(p));
for (i=1;i<=k;i++) {
w=a[i];
while (w<=100000) {
p[w][0]++;p[w][p[w][0]]=a[i];w+=a[i];
}
}
}
int get(int a,int b) {
if (!b) return 0;
int i,j,g,w,ret=b;
for (i=1;i<1<<p[a][0];i++) {
g=0;w=1;
for (j=0;j<p[a][0];j++)
if (i&(1<<j)) {
g++;w*=p[a][j+1];
}
if (g&1) ret-=b/w;
else ret+=b/w;
}
return ret;
}
int main()
{
int a,b,c,d,k,i,t,ca;
ll ans;
deal();
scanf("%d", &t);
for (ca=1;ca<=t;ca++) {
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
if (k==0) { printf("Case %d: 0\n", ca);continue ; }
b/=k;d/=k;ans=0;
if (b>d) { b^=d;d^=b;b^=d; }
for (i=1;i<=b;i++) ans+=(ll)get(i,d)-get(i,i-1);
printf("Case %d: %lld\n", ca, ans);
}
return 0;
}
/*
5
1 3 1 5 1
1 5 1 3 1
1 11014 1 14409 9
1 100000 1 100000 1
*/