zoukankan      html  css  js  c++  java
  • 莫比乌斯算法

    //hdu    1695

    GCD

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


    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
     
     

    如果 : 

    那么 :

    如果    :

    那么   :

     d|n   ==    n%d=0

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<vector>
     7 #define N 100009
     8 using namespace std;
     9 #define ll long long 
    10 #define mem(a,b)  memset(a,b,sizeof(a))
    11 int a,b,c,d,k,t;
    12 int u[N],pre[N];
    13 bool vis[N];
    14 /*
    15 其中μ()函数是莫比乌斯函数,定义是: 
    16 如果d=1 , μ(d)=1 
    17 如果d为互异质数p1,p2…pk的乘积,则μ(d)=(-1)^k
    18 否则,μ(d)=0 
    19 */
    20 void init(){
    21     u[1]=1;
    22     int i,j,pree=0;//局部变量要初始化
    23     for(i=2;i<=N;i++){
    24         if(!vis[i]){
    25             pre[++pree]=i;
    26             u[i]=-1;
    27         }
    28         for(j=1;j<=pree&&i*pre[j]<=N;j++){
    29             vis[i*pre[j]]=1;
    30             if(i%pre[j]==0){
    31                 u[i*pre[j]]=0;//不是由多个质数组成
    32                 break;
    33             }
    34             else{
    35                 u[i*pre[j]]=-u[i];
    36             }
    37         }
    38     }
    39 }
    40 int  main()
    41 {
    42    init();
    43    scanf("%d",&t);
    44     for(int i=1;i<=t;i++){
    45        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
    46        if(!k)  {
    47            printf("Case %d: 0
    ",i);//注意输出格式
    48            continue;
    49        }
    50        b/=k,d/=k;//gcd(kx,ky)==k  互相推出   gcd(x,y)==1
    51        if(b>d) swap(b,d);//b:小的
    52        ll  ans1=0,ans2=0;
    53        for(int i=1;i<=b;i++) ans1+=(ll)u[i]*(b/i)*(d/i);//三个因子相乘的结果可能超int 
    54        for(int i=1;i<=b;i++) ans2+=(ll)u[i]*(b/i)*(b/i);
    55        ans1-=ans2/2;//去重
    56        printf("Case %d: %lld
    ",i,ans1);
    57    }   
    58    return  0;
    59 }

    51 NOD  

    题目来源: CodeChef
    基准时间限制:2 秒 空间限制:131072 KB 分值: 320 难度:7级算法题
     收藏
     关注
    有一个M * N的表格,行与列分别是1 - M和1 - N,格子中间写着行与列的最大公约数Gcd(i, j)(1 <= i <= M, 1 <= j <= N)。
     
    例如:M = 5, n = 4。
     
      1 2 3 4 5
    1 1 1 1 1 1
    2 1 2 1 2 1
    3 1 1 3 1 1
    4 1 2 1 4 1
     
    给出M和N,求这张表中有多少个质数。
    Input
    第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 1000)
    第2 - T + 1行:每行2个数M,N,中间用空格分隔,表示表格的宽和高。(1 <= M, N <= 5 * 10^6)
    Output
    共T行,每行1个数,表示表格中质数的数量。
    Input示例
    2
    10 10
    100 100
    Output示例
    30
    2791



    
    
    
    


     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<vector>
     7 #define N 5000009
     8 using namespace std;
     9 #define ll long long 
    10 #define mem(a,b)  memset(a,b,sizeof(a))
    11 int t,n,m,last;
    12 int pre[N],u[N];
    13 int sum[N],summ[N];//ll会MET
    14 ll ans;
    15 bool vis[N];
    16 void init(){
    17     u[1]=1;
    18     int i,j,pree=0;
    19     for(i=2;i<=N;i++){
    20         if(!vis[i]){
    21             pre[++pree]=i;
    22             u[i]=-1;
    23         }
    24         for(j=1;j<=pree&&i*pre[j]<=N;j++)
    25         {
    26             vis[i*pre[j]]=1;
    27             if(i%pre[j]==0){
    28                 u[i*pre[j]]=0;
    29                 break;
    30             }
    31             else{
    32                 u[i*pre[j]]=-u[i];
    33             }
    34         }
    35     }
    36     for(i=1;i<=pree;i++){
    37         for(j=pre[i];j<=N;j+=pre[i]){
    38             sum[j]+=u[j/pre[i]];
    39         }
    40     }
    41     for(i=1;i<=N;i++){
    42         summ[i]=summ[i-1]+sum[i];
    43     }
    44     printf("%d
    ",pree);
    45 }
    46 int main()
    47 {
    48     init();
    49     scanf("%d",&t);
    50     while(t--)
    51     {
    52         scanf("%d%d",&n,&m);
    53         int lim=min(n,m);
    54         ans=0;
    55         for(int i=1;i<=lim;i=last+1){
    56             last=min(n/(n/i),m/(m/i));
    57             ans+=(ll)(n/i)*(m/i)*(summ[last]-summ[i-1]);//i~last  的F(d)都等于 (n/i)*(m/i)
    如 :
    10/7 * 12/7
    10/8 * 12/8
    10/9 * 12/9
    10/10 * 12/10 是相等的
    58 } 59 printf("%lld ",ans); 60 } 61 return 0; 62 }
  • 相关阅读:
    新闻发布项目——接口类(newsTbDao)
    Möbius strip
    The Apache Thrift API client/server architecture
    可以执行全文搜索的原因 Elasticsearch full-text search Kibana RESTful API with JSON over HTTP elasticsearch_action es 模糊查询
    SciDB
    build a real-time analytics dashboard to visualize the number of orders getting shipped every minute to improve the performance of their logistics for an e-commerce portal
    Kafka monitoring Kafka dashboard
    redundant array of independent disks
    the algebra of modulo-2 sums disk failure recovery
    define tensorflow and run it
  • 原文地址:https://www.cnblogs.com/tingtin/p/9526917.html
Copyright © 2011-2022 走看看