Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
Output
For each test case, print the number of choices. Use the format in the example.
Sample Input
2 1 3 1 5 1 1 11014 1 14409 9
Sample Output
Case 1: 9 Case 2: 736427
Hint
For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
题意:在a~b,c~d这两个区间内找x,y,使得GCD(x,y)==k。
思路:这道题的话,题意让我们在a~b,c~d这两个区间内找x,y,使得GCD(x,y)==k。因为GCD(x,y)==k,所以GCD(x/k,y/k)==1
这样问题就转变为了求x/k和y/k互质的对数了,所以区间就可以缩小为1~b/k和1~d/k,所求的就是这两个区间里面互质数的对数了。又因为题里说类似(1,2)和(2,1)属于一种,所以我们就判断一下b,d的大小,将小的值赋给b,以y>x来保持唯一性。接下来分为两种情况:
1. y <= b , 那么互素对数就是 1~a的欧拉函数的累计和。
2. y >= b , 用容斥原理。
之后本题就可以解出了。
AC代码(普通版):
#include <bits/stdc++.h>
typedef long long ll;
const int maxx=100010;
const int inf=0x3f3f3f3f;
using namespace std;
ll phi[maxx],prime[maxx],p[maxx];
bool flag[maxx];
ll fac[maxx];
ll k;
ll euler(ll n)
{
memset(phi,0,sizeof(phi));
phi[1]=1;
for(ll i=2; i<maxx; i++)
if(!phi[i])
for(ll j=i; j<maxx; j+=i)
{
if(!phi[j])
phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
}
ll div(ll n)
{
ll cnt=0;
for(ll i=2; i*i<=n; i++)
{
if(n%i==0)
{
fac[cnt++]=i;
while(n%i==0)
n/=i;
}
}
if(n>1)
fac[cnt++]=n;
return cnt;
}
ll solve(ll cnt,ll n)
{
ll ans=0;
for(ll i=1; i<(1<<cnt) ; i++)
{
ll ones=0,mult=1;
for(ll j=0; j<cnt; j++)
{
if(i & (1<<j))
{
ones++;
mult*=fac[j];
}
}
if(ones&1)
ans+=n/mult;
else
ans-=n/mult;
}
return n-ans;
}
int main()
{
euler(maxx);
int t,cas=0;
scanf("%d",&t);
while(t--)
{
cas++;
ll a,b,c,d,k;
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
if(b>d)
swap(b,d);
if(k==0)
{
printf("Case %d: 0\n",cas);
continue;
}
b=b/k;
d=d/k;
ll ans=0;
for(int i=1; i<=b; i++)
{
ans+=phi[i];
}
for(ll i=b+1; i<=d; i++)
{
ll cnt=div(i);
ans+=solve(cnt,b);
}
printf("Case %d: %lld\n",cas,ans);
}
return 0;
}
除了普通版,我们还可以用素数筛法和合数分解来优化代码:模板:https://www.cnblogs.com/033000-/p/10041664.html
AC代码(素数筛法+合数分解):
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
using namespace std;
long long a,b,c,d,k;
/*素数筛选和合数分解*/
const int maxn=100100;
int prime[maxn+1];
void getprime()
{
memset(prime,0,sizeof(prime));
for(int i=2; i<maxn; i++)
{
if(!prime[i])
prime[++prime[0]]=i;
for(int j=1; j<=prime[0]&&prime[j]<=maxn/i; j++)
{
prime[prime[j]*i]=1;
if(i%prime[j]==0)
break;
}
}
}
/*合数分解*/
long long factor[105][2];
int fatcnt;
int getfactor(long long x)
{
if(x==0)
return 0;
fatcnt=0;
long long tmp=x;
for(int i=1; prime[i]<=tmp/prime[i]; i++)
{
factor[fatcnt][1]=0;
if(tmp%prime[i]==0)
{
factor[fatcnt][0]=prime[i];
while(tmp%prime[i]==0)
{
factor[fatcnt][1]++;
tmp/=prime[i];
}
fatcnt++;
}
}
if(tmp!=1)
{
factor[fatcnt][0]=tmp;
factor[fatcnt++][1]=1;
}
return fatcnt;
}
/*欧拉函数筛*/
int phi[100100];
void phitable_ZY(int n)
{
memset(phi,0,sizeof(phi));
phi[1]=1;
for(int i=2; i<=n; i++)
if(!phi[i])
for(int j=i; j<=n; j+=i)
{
if(!phi[j])
phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
}
long long cr(long long i)
{
getfactor(i);
long long ans=0;
for(int i=1; i<(1<<fatcnt); i++)
{
long long t=1;
int cut=0;
for(int j=0; j<fatcnt; j++)
{
if(i&(1<<j))
t*=factor[j][0],cut++;
}
if(cut&1)
ans+=b/t;
else
ans-=b/t;
}
return (long long)b-ans;
}
int main()
{
getprime();
phitable_ZY(100010);
int t,cas=0;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&k);
printf("Case %d: ",++cas);
if(k==0||k>b||k>d)
{
printf("0\n");
continue;
}
b/=k;
d/=k;
long long ans=0;
if(b>d)
swap(b,d);
for(int i=1; i<=b; i++)
ans+=phi[i];
for(int i=b+1; i<=d; i++)
{
ans+=cr(i);
}
printf("%I64d\n",ans);
}
return 0;
}