判断素数时,如果数据量很大的话,我们要进行预处理,即打印一张素数表。
①如何去列素数表:用循环for一遍,把素数的倍数都标记为合数。。
1 // 列素数表 2 #define NUM 1000000 3 int array[NUM] = { 1, 1, 0 }; // 用来标记素数的数组 4 // 1不是素数,0是素数 5 int i, j; 6 for( i=2; i<NUM; i++ ) 7 if( array[i] == 0 ) // 说明i是素数 8 for( j=i+i; j<NUM; j+=i ) // 第二重for循环是为了把i的倍数都标记为不是素数 9 array[j] = 1;
②统计素数n前的素数个数(用数组),这里已经用到了动态规划的思想。。如果i为素数,array[i] = array[i-1]+1;素数个数加1。如果i为合数,array[i] = array[i-1]; 素数个数不变。
1 // 统计素数n前的素数个数 2 for( i=2; i<NUM; i++ ) 3 if( array[i] == 0 ) 4 buffer[i] = buffer[i-1] + 1; 5 else 6 buffer[i] = buffer[i-1];
③ 统计质因子的平均值
1 // 统计质因子的平均值 2 for( i=2; i<NUM; i++ ) 3 if( array[i] == 0 ) // 说明i是素数 4 for( j=i+i; j<NUM; j+=i ) // 第二重for循环是为了把i的倍数都标记为不是素数 5 { 6 array[j] = 1; 7 count[j]++; // j的质素数因子增加1; 8 sum[j] += i; // j的质数因子和增加 9 } 10 int n; 11 scanf( "%d", &n ); 12 if( n == 1 ) 13 printf( "0\n" ); 14 else if( array[n] == 0 ) 15 printf( "%d\n", n ); 16 else 17 printf( "%d\n", sum[n]/count[n] );
SZU Problem(A68):Special Prime Number
Judge Info
- Memory Limit: 32768KB
- Case Time Limit: 10000MS
- Time Limit: 10000MS
- Judger: Number Only Judger
Description
What kind prime number will be called as Special Prime Number here? Let me ask you another question first, do you know what is palindrome integer? Palindrome integer is equal to its revers. For example, 12321, 23432 are palindrome integers. The prime number which we called as Special Prime Number here is palindrome number. In this problem you will be given an integer . You are asked to write a program find out the smallest Special Prime Number M, which .
Input
The input will contains multiple test cases. The first line of the input is a single integer which is the number of test cases. T test cases follow.
Each test case contains an integers in a single line.
Output
For each input test case, your program must output a single integer, the smallest Special Prime Number which greater than or equal to N, in a single line.
Sample Input
2 31 180
Sample Output
101 18
1 #include <stdio.h> 2 #define N 635 3 #define M 3100 4 int array[M] = { 1, 1, 0 }; // 用来标记素数的表 5 int primer[M]; 6 int buffer[N]; // 用来装既是素数又是回文的表 7 int main() 8 { 9 int t, i, j, p = 0, num, n; 10 // 先打前3000左右的素数表 11 for( i=2; i<M; i++ ) 12 if( array[i] == 0 ) 13 { 14 primer[p++] = i; 15 for( j=i+i; j<M; j+=i ) 16 array[j] = 1; 17 } 18 t = p; 19 p = 0; 20 // 再找出既是素数又是回文的表 21 for( i=2; i<=10000060; i++ ) 22 { 23 num = 0; 24 j = i; 25 // 利用构造法判断回文数字 26 while( j != 0 ) 27 { 28 num = num*10 + j%10; 29 j /= 10; 30 } 31 if( i == num ) // 如果构造出来的数跟原来的数相同就是回文 32 { 33 if( i < M && array[i] == 0 ) 34 buffer[p++] = i; 35 else 36 { 37 for( j=0; j<t; j++ ) 38 if( i % primer[j] == 0 ) 39 break; 40 if( j == t ) 41 buffer[p++] = i; 42 } 43 } 44 } 45 scanf( "%d", &t ); 46 while( t-- ) 47 { 48 scanf( "%d", &n ); 49 for( i=0; i<N; i++ ) 50 if( buffer[i] >= n ) 51 break; 52 printf( "%d\n", buffer[i] ); 53 } 54 return 0; 55 }
SZU Problem(I80):Number of prime number
Judge Info
- Memory Limit: 32768KB
- Case Time Limit: 1000MS
- Time Limit: 1000MS
- Judger: Number Only Judger
Description
素数是指在大于1的自然数中,除了1和此整数自身外,没法被其他自然数整除的数,例如2, 3, 5, 7, 11……
求n以内的素数个数,是指小于等于n的素数数量
例如n=4,因为小于4的素数是2、3,所以数量是2
Input
第一行输入一个t,表示下面将有t组测试数据。接下来的t行的每行包含参数n(1<n<=1000000)
Output
对于每一组测试数据,输出的一行是n以内的素数个数。
Sample Input
3 18 9 11
Sample Output
7 4 5
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 const int NUM = 1100000; 6 const int num = 3000; 7 int buffer[NUM] = { 1, 1, 0 }; 8 int array[NUM] = {0}; 9 10 int main() 11 { 12 int i; 13 int j;15 16 // 1100000内的素数标记出来 17 for( i=2; i<=sqrt(NUM); i++ ) 18 { 19 if( buffer[i] == 0 ) 20 { 21 for( j=i+i; j<NUM; j=j+i ) 22 buffer[j] = 1; 23 } 24 } 25 26 27 for( i=2; i<NUM; i++ ) 28 if( buffer[i] == 0 ) 29 array[i] = array[i-1] + 1; 30 else 31 array[i] = array[i-1]; 32 33 int t; 34 cin >> t; 35 36 while( t-- ) 37 { 38 cin >> j; 39 40 cout << array[j] << endl; 41 } 42 43 return 0; 44 }
SZU Problem(J23):Prime Product
Judge Info
- Memory Limit: 65536KB
- Case Time Limit: 1000MS
- Time Limit: 1000MS
- Judger: Number Only Judger
Description
Usually when we talk about pp, we will think about beauty (漂漂).But here exactly not.
As we all know, prime is …. Now I will tell something new about prime. That is prime product. You are given two numbers A, B. We want you find 3 prime a, b, c (a<b<c) between A and B (A B included), then you will get two distances (the distance between a and b, and the distance between b and c), which to make the product of two distance biggest. Your task is to tell us the abs of different between two distances.
Input
The first line of input contains T(1<=T<=10000), the number of test cases. There is only line for each test case. It contains two integers A, B(1<=A, B<=100000).
Output
For each test case, output the result. Output -1 if there are no 3 primes between A and B.
Sample Input
2 1 10 2 20
Sample Output
1 1
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 100005 4 int array[N] = { 1, 1, 0 }; // 用来打印素数表的数组 5 int buffer[N]; // 用来存前面有多少个素数的数组 6 int main() 7 { 8 int t, i, j, a, b, c, max; 9 // 首先打印素数表并统计某个素数之前有多少个素数 10 for( i=2; i<N; i++ ) 11 if( array[i] == 0 ) 12 { 13 buffer[i] = buffer[i-1] + 1; 14 for( j=i+i; j<N; j+=i ) 15 array[j] = 1; 16 } 17 else 18 buffer[i] = buffer[i-1]; 19 20 scanf( "%d", &t ); 21 while( t-- ) 22 { 23 scanf( "%d%d", &a, &b ); 24 int temp; 25 // 保证a的值小于b的值 26 if( a > b ) 27 { 28 temp = a; 29 a = b; 30 b = temp; 31 } 32 33 if( buffer[b] - buffer[a] < 3 ) 34 printf( "-1\n" ); 35 else 36 { 37 for( i=0; i<N; i++ ) 38 if( array[i] == 0 && i >= a ) 39 break; 40 a = i; 41 for( i=b; i>=0; i-- ) 42 if( array[i] == 0 && i <= b ) 43 break; 44 b = i; 45 // 不能for一遍来找,这样会超时,直接从中间往两边找^^ 46 max = 0; 47 temp = (a+b)/2; 48 for( i=temp; ; i++ ) 49 if( array[i] == 0 ) 50 { 51 if((i-a)*(b-i) > max ) 52 { 53 max = (i-a)*(b-i); 54 c = i; 55 } 56 break; 57 } 58 for( i=temp; ; i-- ) 59 { 60 if( array[i] == 0 ) 61 { 62 if( (i-a)*(b-i) > max ) 63 c = i; 64 break; 65 } 66 } 67 printf( "%d\n", abs((b-c)-(c-a)) ); 68 } 69 } 70 return 0; 71 }
SZU Problem(L54):calculating the magical value
Judge Info
- Memory Limit: 65536KB
- Case Time Limit: 3000MS
- Time Limit: 3000MS
- Judger: Number Only Judger
Description
Each number is very magical and has a magical value. The magic value is the average of prime numbers.
The average of prime numbers is the sum of all the prime factors of the number divided by the number of the prime factors.
If a number hasn't a prime factor, its magic value is 0.
Give a positive integer n, calculated its magical value.
00 doesn't know how to calculate, so please help him solve this problem.
Input
The first line of input contains T (1<=T<=1000000), the number of test cases. For each test case, only one line with one positive integer n (n <= 1000000)
Output
For each test case, Output the magical value of the positive integer n in one line, rounded up to two fractional digits.
Sample Input
3 1 2 6
Sample Output
0 2.00 2.50
1 #include <stdio.h> 2 #include <math.h> 3 #define NUM 1100000 4 int array[NUM] = {0}; 5 int sum[NUM] = {0}; 6 int count[NUM] = {0}; 7 8 int main() 9 { 10 int i; 11 int j; 12 13 // 列出素数表, 素数用0来表示 14 for( i=2; i<NUM; i++ ) 15 { 16 if( array[i] == 0 ) 17 { 18 for( j=i+i; j<NUM; j+=i ) 19 { 20 array[j] = 1; 21 count[j]++; 22 sum[j] += i; 23 } 24 } 25 } 26 27 int t; 28 scanf( "%d", &t ); 29 while( t-- ) 30 { 31 int n; 32 scanf( "%d", &n ); 33 if( n == 1 ) 34 printf( "0\n" ); 35 else if( array[n] == 0 ) 36 printf( "%.2lf\n", n*1.0 ); 37 else 38 printf( "%.2lf\n", sum[n]*1.0/count[n] ); 39 } 40 return 0; 41 }