zoukankan      html  css  js  c++  java
  • jxust素数

    The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, ‹24, 25, 26, 27, 28› between 23 and 29 is a prime gap of length 6.

    Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k.

    Input

    The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero.

    Output

    The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output.

    Sample Input
    10
    11
    27
    2
    492170
    0
    Sample Output
    4
    0
    6
    0
    114

    题目求是n的左右两个素数之差是多少,如果n本身就是素数的话,输出0就行了。代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 const int maxn = 1299709;
     5 int a[maxn+10];
     6 
     7 int main(){
     8     a[0] = a[1] = 1;
     9     for(int i = 2; i <= maxn; i++){
    10         if(!a[i]){
    11             for(int j = 2; j*i < maxn; j++){
    12                 a[i*j] = 1;
    13             }
    14         }
    15     }
    16     int n;
    17     while(~scanf("%d",&n)&&n){
    18         if(a[n] == 0) cout << 0 << endl;
    19         else{
    20             int i,j;
    21             i=j=n;
    22             while(a[i])i++;
    23             while(a[j])j--;
    24             cout << i - j << endl;
    25         }
    26     }
    27     return 0;
    28 }
    View Code
     
    由于哥德巴赫猜想,一个大于2的偶数都可以由两个素数想加而得。给定一个数 6 < n < 1000000 并且是偶数,求出是由哪两个素数而得,输出格式请看代码:
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <set>
     4 using namespace std;
     5 const int maxn = 1e6;
     6 int a[maxn+10], b[maxn+10];
     7 set<int> s;
     8 int main(){
     9     int k = 0;
    10     for(int i = 2; i <= maxn; i++){
    11         if(!a[i]){
    12             s.insert(i);
    13             b[k++] = i;
    14             for(int j = 2; j*i < maxn; j++){
    15                 a[i*j] = 1;
    16             }
    17         }
    18     }
    19     int n;
    20     while(~scanf("%d",&n)&&n){
    21         for(int i = 0; i < k; i ++){
    22             int y = n - b[i];
    23             if(s.count(y)){
    24                 printf("%d = %d + %d
    ",n,b[i],y);
    25                 break;
    26             }
    27         }
    28     }
    29     return 0;
    30 }
    View Code
    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In this problem you are to write a program that will cut some number of prime numbers from the list of prime numbers between (and including) 1 and N. Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the C*2 prime numbers from the center of the list if there are an even number of prime numbers or (C*2)-1 prime numbers from the center of the list if there are an odd number of prime numbers in the list.
    Input
    Each input set will be on a line by itself and will consist of 2 numbers. The first number (1 <= N <= 1000) is the maximum number in the complete list of prime numbers between 1 and N. The second number (1 <= C <= N) defines the C*2 prime numbers to be printed from the center of the list if the length of the list is even; or the (C*2)-1 numbers to be printed from the center of the list if the length of the list is odd.
    Output
    For each input set, you should print the number N beginning in column 1 followed by a space, then by the number C, then by a colon (:), and then by the center numbers from the list of prime numbers as defined above. If the size of the center list exceeds the limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed. Each number from the center of the list should be preceded by exactly one blank. Each line of output should be followed by a blank line. Hence, your output should follow the exact format shown in the sample output.
    Sample Input
    21 2
    18 2
    18 18
    100 7
    Sample Output
    21 2: 5 7 11
    
    18 2: 3 5 7 11
    
    18 18: 1 2 3 5 7 11 13 17
    
    100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67
    

    题意是给定两个数N C 在0~N的范围内,如果素数个数是奇数,则输出2×C-1个,如果是偶数,则输出2×C个,都是从中间开始依次往两边。
    代码写长了,请谅解。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 using namespace std;
     5 const int maxn = 1e3;
     6 int a[maxn+10],b[maxn+10];
     7 
     8 int main(){
     9     a[0] = a[1] = 1;
    10     b[0] = 1;
    11     int k = 1;
    12     for(int i = 2; i <= maxn; i++){
    13         if(!a[i]){
    14             b[k++] = i;
    15             for(int j = 2; i*j <= maxn; j++){
    16                 a[j*i] = 1;
    17             }
    18         }
    19     }
    20     int n, c;
    21     while(~scanf("%d%d",&n,&c)){
    22         printf("%d %d:",n,c);
    23         int pos = lower_bound(b,b+k,n)-b;
    24         if(a[n]==0 || n == 1)pos++;
    25         //cout << pos << endl;
    26         if(pos&1){
    27             if(c*2-1>=pos){
    28                 for(int i = 0; i < pos; i++){
    29                     printf(" %d",b[i]);
    30                 }
    31                 cout << endl;
    32             }
    33             else{//1
    34                 int cnt = (pos - (c*2 - 1)) / 2;
    35                 for(int i = cnt; i < pos - cnt; i++){
    36                     cout << ' ' << b[i];
    37                 }
    38                 cout << endl;
    39             }
    40         }
    41         else{
    42             if(2*c>=pos){
    43                 for(int i = 0; i < pos; i++){
    44                     cout << ' ' << b[i];
    45                 }
    46                 cout << endl;
    47             }
    48             else{
    49                 int cnt = (pos - 2*c)/2;
    50                 for(int i = cnt; i < pos - cnt; i++){
    51                     cout << ' ' << b[i];
    52                 }
    53                 cout << endl;
    54             }
    55         }
    56         cout << endl;
    57     }
    58     return 0;
    59 }
    View Code

    由N组数据(N<500000)。每组数据分别给出t1和t2,(0 < t1 < t2 < 1000000)。 求在t1~t2之间(包含)即是素数,回文数也是素数的由多少个?

    直接打表0~m之间由多少个,然后输出b[t2]-b[t1]就行了。

     1 #include <iostream>
     2 #include <cstdio>
     3 #define ll long long
     4 using namespace std;
     5 const int maxn = 1e6;
     6 int a[maxn+10],b[maxn];
     7 int _get(int x){
     8     int sum = 0;
     9     while(x){
    10         sum += x % 10;
    11         x /= 10;
    12     }
    13     return sum;
    14 }
    15 int main(){
    16     a[0] = a[1] = 1;
    17     for(int i = 2; i <= maxn; i++){
    18         if(!a[i]){
    19             for(int j = 2; j*i < maxn; j++){
    20                 a[i*j] = 1;
    21             }
    22         }
    23         if(a[i] == 0 && a[_get(i)] == 0){
    24             b[i] = b[i-1] + 1;
    25         }
    26         else b[i] = b[i-1];
    27     }
    28     //cout << b[3] << ' ' << b[13] << endl;
    29     int n, l, r;
    30     cin >> n;
    31     while(n--){
    32         int ans = 0;
    33         scanf("%d%d",&l,&r);
    34         printf("%d
    ",b[r]-b[l-1]);
    35     }
    36     return 0;
    37 }
    View Code
    这题就是给定A1~AH和B1~BH,然后求下面公式的答案就行,模运算。
    (A1B1+A2B2+ ... +AHBH)mod M.
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 
     5 int get_pow(int x, int n, int m){
     6     int ans = 1;
     7     while(n){
     8         if(n&1){
     9             ans = (ans * x)%m;
    10         }
    11         x = x*x;
    12         x %= m;
    13         n/=2;
    14     }
    15     return ans;
    16 }
    17 int main(){
    18     int a, b, t, m, h;
    19     //cout << get_pow(2,3,16)<< endl;
    20     cin >> t;
    21     while(t--){
    22         int ans = 0;
    23         cin >> m >> h;
    24         for(int i = 1; i <= h; i++){
    25             scanf("%d%d",&a,&b);
    26             //cin >> a >> b;
    27         //    cout << ans << endl;
    28             ans = (ans%m + get_pow(a%m,b,m)%m)%m;
    29         }
    30         cout << ans%m << endl;
    31     }
    32     return 0;
    33 }
    View Code
    lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
    this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.

    InputThere are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)
    OutputFor each test case, you should output the a^b's last digit number.
    Sample Input
    7 66
    8 800
    Sample Output
    9
    6

    这题是求a^b的答案的个位数答案。
     1 #include <iostream>
     2 #include <cstdio>
     3 #define ll long long
     4 using namespace std;
     5 ll get_pow(int x, int n){
     6     if(n == 1) return x;
     7     if(n&1) return x*get_pow((x*x)%10,n/2);
     8     else return get_pow((x*x)%10,n/2);
     9 }
    10 int main(){
    11     ll a, b;
    12     while(~scanf("%lld%lld",&a,&b)){
    13         cout << get_pow(a%10,b)%10<<endl;
    14     }
    15     return 0;
    16 }
    View Code
    Read the program below carefully then answer the question.
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include<iostream>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include<vector>

    const int MAX=100000*2;
    const int INF=1e9;

    int main()
    {
      int n,m,ans,i;
      while(scanf("%d%d",&n,&m)!=EOF)
      {
        ans=0;
        for(i=1;i<=n;i++)
        {
          if(i&1)ans=(ans*2+1)%m;
          else ans=ans*2%m;
        }
        printf("%d ",ans);
      }
      return 0;
    }

    InputMulti test cases,each line will contain two integers n and m. Process to end of file.
    [Technical Specification]
    1<=n, m <= 1000000000OutputFor each case,output an integer,represents the output of above program.Sample Input
    1 10
    3 100
    Sample Output
    1
    5

    看代码猜题意,用这段代码肯定会爆掉,通过代码我求出的通式是:n为奇数时:An = (2^(n+1)-1)/3 n为偶数时 An = A(n-1) 然后就是写代码了,不过由个/3。
    模运算不好弄,所以可以看成(2^(n+1)-1)%3m 求出的结果在除以3就行了。这题用这方法错了几次,之后把int改为long long就行了,好粗心!!!
     1 #include <iostream>
     2 #include <cstdio>
     3 #define ll long long
     4 using namespace std;
     5 ll m,n;
     6 int get_pow(ll x, ll n){
     7     ll ans = 1;
     8     while(n){
     9         if(n&1){
    10             ans = (ans * x) % (3*m);
    11         }
    12         x = (x*x)%(3*m);
    13         n/=2;
    14     }
    15     return ans%(3*m);
    16 }
    17 void solve(){
    18     if(n&1){
    19         //cout << get_pow(2,n+1) << endl;
    20         ll a = get_pow(2,n+1) + 3*m -1;
    21         cout << a%(3*m) / 3<< endl;
    22     }
    23     else{
    24         //cout << get_pow(2,2) << endl;
    25         ll a = get_pow(2,n+1) + 3*m -2;
    26         cout << a%(3*m) / 3<< endl;
    27     }
    28 }
    29 int main(){
    30     while(~scanf("%lld%lld",&n,&m)){
    31         solve();
    32     }
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    中海洋朗讯杯比赛总结[2014年12月]
    青理工ACM比赛总结和反思[2014年11月]
    程序员技术练级攻略
    一天能学会的计算机技术
    UVa 1597
    回滚机制
    超时和重试机制
    降级特技
    限流详解
    隔离术
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/6629008.html
Copyright © 2011-2022 走看看