zoukankan      html  css  js  c++  java
  • GCD hdu1695容斥原理

    GCD

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


    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).

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


    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).
     
     
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <math.h>
     5 #include <algorithm>
     6 #include <vector>
     7 using namespace std;
     8 vector<int>q[110000];
     9 long long a[110000]={0};
    10 int bb;
    11 void init()
    12 {
    13     int i,j;
    14     for(i=0; i<110000; i++)a[i]=i,q[i].clear();
    15     for(i=2; i<110000; i+=2)
    16         a[i]/=2,q[i].push_back(2);
    17     for(i=3; i<110000; i+=2)
    18         if(a[i]==i)
    19             for(j=i; j<110000; j+=i)
    20                 a[j]=a[j]/i*(i-1),q[j].push_back(i);
    21     for(i=1; i<110000; i++)
    22         a[i]+=a[i-1];
    23 }
    24 int fun(int x,int y)
    25 {
    26     int i,cnt=0;
    27     int sum=1;
    28     for(i=0;i<q[y].size();i++)
    29     {
    30         if(x&(1<<i))
    31         {
    32             sum*=q[y][i];
    33             cnt++;
    34         }
    35     }
    36     if(cnt&1)
    37     return bb/sum;
    38     else return -(bb/sum);
    39 }
    40 long long work(int x)
    41 {
    42     int i;
    43     long long sum=0;
    44     for(i=1;i<(1<<q[x].size());i++)
    45     {
    46         sum+=fun(i,x);
    47     }
    48     return bb-sum;
    49 }
    50 int main()
    51 {
    52     init();
    53     int i,t,j,aa,c,d,k;
    54     long long ans;
    55     scanf("%d",&t);
    56     for(i=1; i<=t; i++)
    57     {
    58         scanf("%d%d%d%d%d",&aa,&bb,&c,&d,&k);
    59         if(bb>d)swap(bb,d);
    60         if(k)
    61         bb/=k,d/=k;
    62         else
    63         {
    64              printf("Case %d: %d
    ",i,0);
    65              continue;
    66         }
    67         ans=a[bb];
    68         for(j=bb+1; j<=d; j++)
    69         {
    70             ans+=work(j);
    71         }
    72         printf("Case %d: %I64d
    ",i,ans);
    73     }
    74 }
    View Code
  • 相关阅读:
    Android网络项目课程笔记-----系统设置_首选项框架&Holo风格的设置
    Android网络项目课程笔记-----加载数据
    Android网络项目课程笔记-----代码复用性
    Android网络项目课程笔记-----AdapterView嵌套
    Android网络项目课程笔记-----滑动Banner
    Android网络项目课程笔记-----滑动Tab&Banner
    Android网络项目课程笔记-----页面结构
    Android网络项目课程笔记-----欢迎页面新手引导
    C#调用SAP S4/HANA Gateway Service
    CefSharp试用
  • 原文地址:https://www.cnblogs.com/ERKE/p/3682068.html
Copyright © 2011-2022 走看看