GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17385 Accepted Submission(s): 6699
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.
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.
1 3 1 5 1
1 11014 1 14409 9
Case 2: 736427
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).
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int ans;
LL tot[maxn + ];
int prime[maxn+], phi[maxn+];
bool vis[maxn+];
void getphi()
{
ans = ;
phi[] = ;
for(int i=; i<=maxn; i++)
{
if(!vis[i])
{
prime[++ans] = i;
phi[i] = i - ;
}
for(int j=; j<=ans; j++)
{
if(i * prime[j] > maxn) break;
vis[i * prime[j]] = ;
if(i % prime[j] == )
{ phi[i * prime[j]] = phi[i] * prime[j]; break;
}
else
phi[i * prime[j]] = phi[i] * (prime[j] - );
}
}
} int get_cnt(int n, int m)
{
int ans = ;
for(int i = ; i * i <= n; i++)
{
if(n % i) continue;
while(n % i == ) n /= i;
prime[ans++] = i;
}
if(n != ) prime[ans++] = n;
int res = ;
for(int i = ; i < ( << ans); i++)
{
int tmp = , cnt2 = ;
for(int j = ; j < ans; j++)
{
if(((i >> j) & ) == ) continue;
tmp *= prime[j];
cnt2++;
}
if(cnt2 & ) res += m / tmp;
else res -= m / tmp;
}
return m - res;
} int main()
{
getphi();
int a, b, c, d, k;
for(int i = ; i < maxn; i++)
{
tot[i] = tot[i - ] + phi[i]; }
int T, kase = ;
cin >> T;
while(T--)
{
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
if(k == )
{
printf("Case %d: 0\n", ++kase);
continue;
}
int n = b / k, m = d / k;
LL sum = tot[n > m ? m : n];
// cout << sum << endl;
if(m > n) swap(n, m);
for(int i =m + ; i <= n; i++)
{
sum += get_cnt(i, m);
}
printf("Case %d: %lld\n", ++kase, sum);
} return ;
}
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17385 Accepted Submission(s): 6699
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.
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.
1 3 1 5 1
1 11014 1 14409 9
Case 2: 736427
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).