zoj 3435 莫比乌斯基础

来源:互联网 发布:java编写四级查分软件 编辑:程序博客网 时间:2024/05/23 12:51

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) 内 求过(1,1,1)的直线

全部-1 就变成(a-1,b-1,c-1) 和 0 0 0 就和上一题一样了,那么就是 求 一个点为0 两个点为0的情况
因为莫比乌斯求的是 大于等于1的情况,所以把两个为0的情况(只有3种加上就好)单独考虑上

有一个为0 那么,那么利用剩下不为0的两个进行莫比乌斯,加上三个不为0的莫比乌斯情况就好,因为gcd还是=1,所以所以枚举数量还是n/1(即全部)
数据量较大 达到了1e6 常数太大所以必须加速,

#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn=1e6+100;  bool vis[maxn];  int prime[maxn],mu[maxn];  int cnt,sum[maxn];  void Init(){      int N=maxn;      memset(prime,0,sizeof(prime));      memset(mu,0,sizeof(mu));      memset(vis,0,sizeof(vis));      mu[1] = 1;      cnt = 0;      for(int i=2; i<N; i++){          if(!vis[i]){              prime[cnt++] = i;              mu[i] = -1;          }          for(int j=0; j<cnt&&i*prime[j]<N; j++){              vis[i*prime[j]] = 1;              if(i%prime[j]) mu[i*prime[j]] = -mu[i];              else{                  mu[i*prime[j]] = 0;                  break;              }          }      }    for(int i=1;i<N;i++)        sum[i]=sum[i-1]+mu[i];  }ll cal(ll x1,ll x2){    ll gg=min(x1,x2);    ll total=0;    for(int i=1,next=0;i<=gg;i=next+1)    {        next=min(x1/(x1/i),x2/(x2/i));        total+=(sum[next]-sum[i-1])*((x1/i)*(x2/i));    }    return total;}ll cal1(ll x1,ll x2,ll x3){    ll gg=min(min(x1,x2),x3);    ll total=0;    for(int i=1,next=0;i<=gg;i=next+1)    {        next=min(x1/(x1/i),min(x2/(x2/i),x3/(x3/i)));        total+=(sum[next]-sum[i-1])*((x1/i)*(x2/i)*(x3/i));    }    return total;}int main(){    ll x,y,z;    Init();    while(scanf("%lld%lld%lld",&x,&y,&z)!=EOF)    {        x--,y--,z--;        ll total=3;        total+=cal(x,y);        total+=cal(y,z);        total+=cal(x,z);        total+=cal1(x,y,z);        cout<<total<<endl;    }}