zoukankan      html  css  js  c++  java
  • HDU1695 莫比乌斯反演

    GCD

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 15981    Accepted Submission(s): 6144


    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.
     
    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).
     
    Source
    题意:gcd=k的对数
    思路:倍数莫比乌斯反演。
    代码:
     1 #include<bits/stdc++.h>
     2 #define  ll long long
     3 using namespace std;
     4 const int N = 2e5 + 5;
     5 int t;
     6 //线性筛法求莫比乌斯函数
     7 bool vis[N + 10];
     8 int pri[N + 10];
     9 int mu[N + 10];
    10 int sum[N];
    11 int a,b;
    12 void mus() {
    13     memset(vis, 0, sizeof(vis));
    14     mu[1] = 1;
    15     int tot = 0;
    16     for (int i = 2; i < N; i++) {
    17         if (!vis[i]) {
    18             pri[tot++] = i;
    19             mu[i] = -1;
    20         }
    21         for (int j = 0; j < tot && i * pri[j] < N; j++) {
    22             vis[i * pri[j]] = 1;
    23             if (i % pri[j] == 0) {
    24                 mu[i * pri[j]] = 0;
    25                 break;
    26             }
    27             else  mu[i * pri[j]] = -mu[i];
    28         }
    29     }
    30     sum[1]=1;
    31     for(int i=2;i<N;i++) sum[i]=sum[i-1]+mu[i];
    32 }
    33 int n,m,k;
    34 ll cal(int x,int y){//求[1,x],[1,y]内互质的数对
    35     int ma=min(x,y);
    36     ll ans=0;
    37     for(int i=1,j;i<=ma;i=j+1){
    38         j=min(x/(x/i),y/(y/i));
    39         if(j>=ma) j=ma;
    40         ans+=1ll*(sum[j]-sum[i-1])*(x/i)*(y/i);
    41     }
    42     return ans;
    43 }
    44 int main() {
    45     mus();
    46     scanf("%d",&t);
    47     for(int i=1;i<=t;i++){
    48         scanf("%d%d%d%d%d",&a,&n,&b,&m,&k);
    49         ll ans,res;
    50         if(!k) ans=0;
    51         else
    52         {
    53             if(n<m) swap(n,m);
    54             res=cal(m/k,m/k);//这部分除1外,算了两次
    55             ans=cal(n/k,m/k);//这部分为总的个数
    56             ans=ans-res+(res+1)/2;//最后结果
    57         }
    58         printf("Case %d: %lld
    ",i,ans);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    如何通过代码设置WPF控件的字体,颜色
    WPF DataGrid 控件的运用
    WPF 动态创建 DataTemplate 及数据绑定
    WPF Grid 用 C# 代码后台设置
    C# a++ 与 ++a 的区别
    WPF 绑定以基础数据类型为集合的无字段名的数据源
    Visual Studio 快捷键
    WPF TabItem.Collapse 的问题
    C# XML 文件中的空格值问题
    C# XML文件操作
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/9572455.html
Copyright © 2011-2022 走看看