zoukankan      html  css  js  c++  java
  • POJ2689 HDU2824 筛法、欧拉函数

    传送门什么的@百度。。

                                      Prime Distance
    Time Limit: 1000MS        Memory Limit: 65536K
    
    Description
    The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
    Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
    
    Input
    Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.
    
    Output
    For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.
    
    Sample Input
    
    2 17
    14 17
    
    Sample Output
    
    2,3 are closest, 7,11 are most distant.
    There are no adjacent primes.
    
    Source
    Waterloo local 1998.10.17
    The Euler function
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    
    
    Problem Description
    The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)
     
    
    Input
    There are several test cases. Each line has two integers a, b (2<a<b<3000000).
     
    
    Output
    Output the result of (a)+ (a+1)+....+ (b)
     
    
    Sample Input
    
    3 100
    
     
    
    Sample Output
    
    3042
    
     
    
    Source
    2009 Multi-University Training Contest 1 - Host by TJU
     
    HDU2824题目

    后缀数组暂时放了。。因为感觉复赛回来之后代码也就忘差不多了。。?

    写的都是O(n)的筛法。。欧拉函数的写法是贾教的。。

    codes:

     1 #include<set>
     2 #include<map>
     3 #include<cmath>
     4 #include<queue>
     5 #include<cstdio>
     6 #include<cstdlib>
     7 #include<cstring>
     8 #include<iostream>
     9 #include<algorithm>
    10 using namespace std;
    11 const int N = 500000*2;
    12 #define rep(i,n) for(int i=0;i<n;i++)
    13 #define Rep(i,n) for(int i=1;i<=n;i++)
    14 #define For(i,l,r) for(int i=l;i<=r;i++)
    15 int prime[N+10],l,r,primes[N+10],last,now,ansmin,ansmax;
    16 int ans1,ans2,ans3,ans4;
    17 bool check[N+10],checks[N+10];
    18 
    19 void PRIME(int n){
    20     For(i,2,n){
    21         if(!check[i]) prime[++prime[0]]=i;
    22         Rep(j,prime[0]){
    23             if(prime[j]*i>n) break;
    24             check[prime[j]*i]=true;
    25             if(!(i%prime[j])) break; 
    26         }
    27     }
    28 }
    29 
    30 void query(int l,int r){
    31     memset(checks,false,sizeof(checks));
    32     primes[0]=0;ansmin=1e9;ansmax=0;
    33     ans1=ans2=ans3=ans4=last=now=0;
    34     Rep(i,prime[0]){
    35         For(j,l/prime[i],r/prime[i]){
    36             if(prime[i]*j<l||j==1) continue;
    37             checks[prime[i]*j-l]=true;
    38         }
    39     }
    40     if(l==1) checks[0]=true;
    41     rep(i,r-l+1)
    42         if(!checks[i]) { 
    43             primes[++primes[0]]=i+l;
    44             now=i+l;
    45             if(now-last>ansmax && last){
    46                 ansmax=now-last;
    47                 ans1=last;ans2=now;
    48             }
    49             if(now-last<ansmin){
    50                 ansmin=now-last;
    51                 ans3=last;ans4=now;
    52             }
    53             last=now;
    54         }
    55     if(primes[0]>1) printf("%d,%d are closest, %d,%d are most distant.
    ",ans3,ans4,ans1,ans2);
    56     else            puts("There are no adjacent primes.");
    57 }
    58 
    59 int main(){
    60     PRIME(N);
    61     while(scanf("%d%d",&l,&r)!=EOF) query(l,r);
    62     return 0;
    63 }
     1 #include<set>
     2 #include<map>
     3 #include<queue>
     4 #include<cstdio>
     5 #include<cstdlib>
     6 #include<cstring>
     7 #include<iostream>
     8 #include<algorithm>
     9 using namespace std;
    10 const int N = 3000000;
    11 #define rep(i,n) for(int i=0;i<n;i++)
    12 #define Rep(i,n) for(int i=1;i<=n;i++)
    13 #define For(i,l,r) for(int i=l;i<=r;i++)
    14 
    15 int a,b;
    16 int phi[N+3],prime[1000000];
    17 bool check[N+3];
    18 
    19 void PHI(int n){
    20     phi[1]=1;
    21     For(i,2,n){
    22         if(!check[i]){
    23             prime[++prime[0]]=i;
    24             phi[i]=i-1;
    25         }
    26         Rep(j,prime[0]){
    27             if(prime[j]*i>n) break;
    28             check[prime[j]*i]=true;
    29             if(i%prime[j]) phi[i*prime[j]]=phi[i]*(prime[j]-1);
    30             else {
    31                 phi[i*prime[j]]=phi[i]*prime[j];
    32                 break;
    33             }
    34         }
    35     } 
    36 }
    37 
    38 int main(){
    39     PHI(N);
    40     while(scanf("%d%d",&a,&b)!=EOF) {
    41         long long sum=0;
    42         For(i,a,b) sum+=phi[i];
    43         printf("%I64d
    ",sum);
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    Qt 4套件的组成适用于Qt 4.5以后的版本
    GTK+, Qt, wxWidgets compare
    为什么选择Qt
    [转]零基础学Qt 4编程实例之四:理解并正确使用名字空间
    [转]Qt 4常见的IDE及其优缺点比较推荐Qt Creator和Eclipse
    *nix系统下验证Qt 4安装正确与否的方法和步骤
    Debian install matlab2010—also ok for ubuntu series!
    我推荐的Qt资源网站、论坛、博客等来自《零基础学Qt 4编程》一书的附录
    ubuntu debian fedora Mac install pgplot steps!!
    64位WIN7 配置IIS遇到问题
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3999425.html
Copyright © 2011-2022 走看看