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.
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.
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.
10 11 27 2 492170 0Sample 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 }

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 }
21 2 18 2 18 18 100 7Sample 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 }
由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 }

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 }
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 800Sample 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 }
#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 100Sample 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 }