vector 可变长数组 hdu 3823
来源:互联网 发布:阿沁淘宝店叫什么 编辑:程序博客网 时间:2024/05/16 12:53
Problem F
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Besides the ordinary Boy Friend and Girl Friend, here we define a more academic kind of friend: Prime Friend. We call a nonnegative integer A is the integer B’s Prime Friend when the sum of A and B is a prime.
So an integer has many prime friends, for example, 1 has infinite prime friends: 1, 2, 4, 6, 10 and so on. This problem is very simple, given two integers A and B, find the minimum common prime friend which will make them not only become primes but also prime neighbor. We say C and D is prime neighbor only when both of them are primes and integer(s) between them is/are not.
So an integer has many prime friends, for example, 1 has infinite prime friends: 1, 2, 4, 6, 10 and so on. This problem is very simple, given two integers A and B, find the minimum common prime friend which will make them not only become primes but also prime neighbor. We say C and D is prime neighbor only when both of them are primes and integer(s) between them is/are not.
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case only contains two integers A and B.
Technical Specification
1. 1 <= T <= 1000
2. 1 <= A, B <= 150
Each test case only contains two integers A and B.
Technical Specification
1. 1 <= T <= 1000
2. 1 <= A, B <= 150
Output
For each test case, output the case number first, then the minimum common prime friend of A and B, if not such number exists, output -1.
Sample Input
22 43 6
Sample Output
Case 1: 1Case 2: -1
题意:
即求一个数,能使a,b与之相加后,成为素数,并且a与b之间没有其他的素数。
做法:
由于无论a,b与什么数相加之间的差值都相等,所以实际上求的是大于b的与其前一个数的差值是a-b的素数。
所以该题的关键是将20000000之前的素数打表,然后求其每个之间的差值,相等的存放到同一个数组中。
这里主要介绍的是vertor 可变长数组的用法:
初始化:vertor<int> a[100]; //即开的是二维数组,可以包含100行,每行的长度可以不同。
a[2].push_back(i); //将i放入第二行的尾部
a[2].size(); //获得第二行的长度
代码实现:
#include<iostream>#include<vector>#include<stdio.h>#include<string.h>using namespace std;int a[19000000];bool b[20000001];vector <int>sub[155];int main(){ memset(b,0,sizeof(b)); b[1]=1; for(int i=2;i*i<=20000000;i++){ if(b[i]==1) continue; for(int j=i*2;j<=20000000;j+=i){ b[j]=1; } } int times=0; for(int k=2;k<=20000000;k++){ if(b[k]==0){ a[times]=k; //cout<<k<<endl; times++; } } for(int g=0;g<times-1;g++){ int temp=a[g+1]-a[g]; if(temp>150) continue; sub[temp].push_back(a[g+1]); } int m; cin>>m; int sum=0; while(m--){ sum++; long long x,y; cin>>x>>y; int temp1=abs(x-y); int temp2=x>y?x:y; long long res=0; for(int d=0;d<sub[temp1].size();d++){ if(sub[temp1][d]>=temp2){ res=sub[temp1][d]; break; } } cout<<"Case "<<sum<<": "; if(res==0) cout<<"-1"<<endl; else cout<<res-temp2<<endl; } //system("pause"); return 0;}
- vector 可变长数组 hdu 3823
- hdu 3823 素数打表+vector可变长数组 好烦的一道好题。。。。。
- 可变长的数组
- 可变长数组
- 可变长数组
- JavaScript可变长数组
- 不定长数组:vector
- 不定长数组 vector
- 不定长数组vector
- Lisp语言:可变长数组
- Lisp语言:可变长数组
- C++可变长数组测试
- Problem E: 可变长数组
- Problem E: 可变长数组
- C实现可变长数组
- 07:可变长数组、多维数组
- STL_不定长数组-vector
- C99可变长数组VLA详解
- USC newweek2 H hdu 3335
- USC newweek2 G
- DP 记忆化搜索 poj 1088
- poj 1743 字符串 后缀数组 不可重叠最长重复子串
- 取多次方的前n位
- vector 可变长数组 hdu 3823
- 最长公共子序列 poj1458
- 回文串 poj 1159
- DFS 剪枝1 poj 1011
- DFS poj 3009
- DP poj 2192
- 线段树的实现(求段和)
- 线段树优化 lazy算法 poj3468
- 云计算平台管理的三大利器Nagios、Ganglia和Splunk