既求gcd(b/k,d/k)==1的组合的个数,
设B=b/k D=d/k 且 B<D
考虑从D中取一个数
如果在1~B这部分可由欧拉phi函数求得,若在B+1~D这部分,可以用容斥原理求得.
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6596 Accepted Submission(s): 2410
Problem Description
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.
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.
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: 736427HintFor 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).
Source
/* ***********************************************
Author :CKboss
Created Time :2015年03月27日 星期五 09时27分50秒
File Name :HDOJ1696.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long int LL;
const LL maxn=100100;
LL a,b,c,d,k;
LL phi[maxn+10];
LL sum[maxn+10];
vector<LL> prime[maxn+10];
void phi_table(LL n)
{
phi[1]=1;
for(LL i=2;i<=n;i++)
{
if(!phi[i])
{
for(LL j=i;j<=n;j+=i)
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-1);
prime[j].push_back(i);
}
}
}
}
LL RET;
void dfs(int x,LL b,LL now,LL mark)
{
for(int i=x,sz=prime[now].size();i<sz;i++)
{
LL p=prime[now][i];
RET+=mark*(b/p);
if(x+1<sz) dfs(i+1,b/p,now,-mark);
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
phi_table(maxn);
for(LL i=1;i<=maxn;i++) sum[i]=sum[i-1]+phi[i];
LL T_T,cas=1;
cin>>T_T;
while(T_T--)
{
cin>>a>>b>>c>>d>>k;
if(k==0)
{
cout<<"Case "<<cas++<<": 0"<<endl;
continue;
}
b/=k; d/=k;
if(b>d) swap(b,d);
LL ans=sum[b];
for(LL i=b+1;i<=d;i++)
{
RET=b; dfs(0,b,i,-1);
ans+=RET;
}
//printf("Case %lld: %lld\n",cas++,ans);
cout<<"Case "<<cas++<<": "<<ans<<endl;
}
return 0;
}