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 }
  • 相关阅读:
    区分服务器和客户端,玩家的控制权
    分割字符串
    switch语句的使用
    博客暂停使用
    [题解]洛谷P1041 传染病控制
    [题解]洛谷P2668 斗地主
    [题解]洛谷P4017 最大食物链计数
    [题解]洛谷P1983 车站分级
    [OI学习笔记]倍增LCA
    [OI学习笔记]st表
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3938477.html
Copyright © 2011-2022 走看看