zoukankan      html  css  js  c++  java
  • bzoj 2818: Gcd GCD(a,b) = 素数

    2818: Gcd

    Time Limit: 10 Sec  Memory Limit: 256 MB
    Submit: 1566  Solved: 691
    [Submit][Status]

    Description

    给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
    数对(x,y)有多少对.

    Input

    一个整数N

    Output

    如题

    Sample Input

    4

    Sample Output

    4

    HINT

    hint

    对于样例(2,2),(2,4),(3,3),(4,2)


    1<=N<=10^7

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<cstdlib>
     5 using namespace std;
     6 
     7 typedef long long LL;
     8 const int maxn = 1e7+1;
     9 bool s[maxn];
    10 int prime[maxn],len = 0;
    11 int mu[maxn];
    12 int g[maxn];
    13 int sum1[maxn];
    14 void  init()
    15 {
    16     memset(s,true,sizeof(s));
    17     mu[1] = 1;
    18     for(int i=2; i<maxn; i++)
    19     {
    20         if(s[i] == true)
    21         {
    22             prime[++len]  = i;
    23             mu[i] = -1;
    24             g[i] = 1;
    25         }
    26         for(int j=1; j<=len && (long long)prime[j]*i<maxn; j++)
    27         {
    28             s[i*prime[j]] = false;
    29             if(i%prime[j]!=0)
    30             {
    31                 mu[i*prime[j]] = -mu[i];
    32                 g[i*prime[j]] = mu[i] - g[i];
    33             }
    34             else
    35             {
    36                 mu[i*prime[j]] = 0;
    37                 g[i*prime[j]] = mu[i];
    38                 break;
    39             }
    40         }
    41     }
    42     for(int i=1; i<maxn; i++)
    43         sum1[i] = sum1[i-1]+g[i];
    44 }
    45 
    46 int main()
    47 {
    48     int a;
    49     init();
    50     while(scanf("%d",&a)>0)
    51     {
    52         LL sum = 0;
    53         for(int i=1,la = 0 ; i<=a; i = la+1)
    54         {
    55             la = a/(a/i);
    56             sum = sum + (long long)(sum1[la] - sum1[i-1])*(a/i)*(a/i);
    57         }
    58         printf("%lld
    ",sum);
    59     }
    60     return 0;
    61 }

     spoj 

    4491. Primes in GCD Table

    Problem code: PGCD

    Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greatest common divisor) table! So he now has a table (of height a and width b), indexed from (1,1) to (a,b), and with the value of field (i,j) equal to gcd(i,j). He wants to know how many times he has used prime numbers when writing the table.

    Input

    First, t ≤ 10, the number of test cases. Each test case consists of two integers, 1 ≤ a,b < 107.

    Output

    For each test case write one number - the number of prime numbers Johnny wrote in that test case.

    Example

    Input:
    2
    10 10
    100 100
    Output:
    30
    2791

    一样的题,只不过 GCD(x,y) = 素数 .  1<=x<=a ; 1<=y<=b;
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<cstdlib>
     5 using namespace std;
     6  
     7 typedef long long LL;
     8 const int maxn = 1e7+1;
     9 bool s[maxn];
    10 int prime[maxn],len = 0;
    11 int mu[maxn];
    12 int g[maxn];
    13 int sum1[maxn];
    14 void  init()
    15 {
    16     memset(s,true,sizeof(s));
    17     mu[1] = 1;
    18     for(int i=2;i<maxn;i++)
    19     {
    20         if(s[i] == true)
    21         {
    22             prime[++len]  = i;
    23             mu[i] = -1;
    24             g[i] = 1;
    25         }
    26         for(int j=1;j<=len && (long long)prime[j]*i<maxn;j++)
    27         {
    28             s[i*prime[j]] = false;
    29             if(i%prime[j]!=0)
    30             {
    31                 mu[i*prime[j]] = -mu[i];
    32                 g[i*prime[j]] = mu[i] - g[i];
    33             }
    34             else
    35             {
    36                 mu[i*prime[j]] = 0;
    37                 g[i*prime[j]] = mu[i];
    38                 break;
    39             }
    40         }
    41     }
    42     for(int i=1;i<maxn;i++)
    43         sum1[i] = sum1[i-1]+g[i];
    44 }
    45  
    46 int main()
    47 {
    48     int T,a,b;
    49     init();
    50     scanf("%d",&T);
    51     while(T--)
    52     {
    53         scanf("%d%d",&a,&b);
    54         if(a>b) swap(a,b);
    55         LL sum = 0;
    56         for(int i=1,la = 0 ;i<=a;i = la+1)
    57         {
    58             la = min(a/(a/i),b/(b/i));
    59             sum = sum + (long long)(sum1[la] - sum1[i-1])*(a/i)*(b/i);
    60         }
    61         printf("%lld
    ",sum);
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    大小写敏感性
    Select的深入应用(1)
    SQL模式匹配
    返回日期和时间范围
    利用枚举管理事务逻辑
    自动记录数据的改变时间
    操作日期和时间
    关于Rational Functional Tester (RFT)的简单介绍
    html布局 左右固定,中间只适应,三种方法实现
    js混合计算字符串字节长度
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3938477.html
Copyright © 2011-2022 走看看