hdu 3823 素数打表+vector可变长数组 好烦的一道好题。。。。。
来源:互联网 发布:淘宝嘉年华持续时间 编辑:程序博客网 时间:2024/05/19 00:17
http://acm.hdu.edu.cn/showproblem.php?pid=3823
Prime Friend
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2855 Accepted Submission(s): 580
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之间没有其他的素数。
vector开变长数组
初始化:vertor<int> a[100]; //即开的是二维数组,可以包含100行,每行的长度可以不同。
a[2].push_back(i); //将i放入第二行的尾部
a[2].size(); //获得第二行的长度
心路历程:...
一开始写了一遍。。运行不了。。debug发现prime数组开小了
开大。。。提交。。。超内存...
max改成 ? : 。。降了一半
cin .cout改成scanf.. 没变化。。
把isprime()拉到主函数里来。。。 没变化。
发现素数打表有一个地方写错。。。改正了。。没变化。。
把素数的数组改成bool(一开始用Int)。。。好了一点。。。
最后不用G++提交了。。。换了C++提交。。。然后就过了。。。。。。。好气哦。。。做了这么久的题
//hdu 3823#include <iostream>#include <cstring>#include <cstdio>#include <string>#include <algorithm>#include <vector>using namespace std;typedef long long ll;//题意:给出两个数字a,b。使a + x, b + x都是素数,并且它们之间没有素数。求出这样的最小的x。//http://www.cnblogs.com/yumao1006/archive/2012/08/30/2664591.html/*暴力打表,找出20000000以内的素数,求出相邻素数的差,存放在vector的数组中*///学习::把max函数换成?:能省一半内存。。。。//学习::把cin cout 换成scanf,能省一半时间。。。//一开始,素数打表那里条件写成i < maxn 。。。错了好多次啊。。const int maxn = 20000000 + 5;//在这里面素数的差才可能在150以内bool pp[maxn];//筛选法找素数int prime[maxn/2];//存放素数vector <int>vec[155];void isprime(){memset(pp, 0, sizeof(pp));pp[1] = 1;for (int i = 2; i*i < maxn; i++){if (!pp[i]) //0是素数{for (int j = i + i; j < maxn; j+=i){pp[j] = 1;}}}}int main(){isprime();//找素数个数,并把素数都放在数组prime[15000]中int times = 0;for (int i = 2; i < maxn; i++){if (!pp[i]){prime[times++] = i;}}for (int i = 0; i < times-1; i++){int tmp = prime[i + 1] - prime[i];if (tmp <= 150)vec[tmp].push_back(prime[i + 1]);}int t, cnt=0;int a, b;scanf("%d", &t);while (t--){//cin >> a >> b;scanf("%d%d", &a, &b);int abss = abs(a - b);//两个数的差一直都不变int maxx = a > b ? a : b;//一开始的最大int res = 0;//加上x后的最大for (int i=0;i<vec[abss].size();i++){if (vec[abss][i] >= maxx){res = vec[abss][i]; //找到一个能满足的最小的就行了break;}}printf("Case %d: ", ++cnt);//printf(res ? "%d\n" : "-1\n", res - maxx);if (res == 0)puts("-1");else//cout << res - maxx << endl;printf("%d\n", res - maxx);}system("pause");return 0;}
0 0
- hdu 3823 素数打表+vector可变长数组 好烦的一道好题。。。。。
- vector 可变长数组 hdu 3823
- 杭电4548 好长的筛选素数,打表,细节特别要注意l代表后面的大的那个数
- hdoj 素数回文 1431 (素数打表&&转换)好题
- 一道递归的好题
- 一道阿姆斯特朗回旋好题( Convex HDU
- 可变长的数组
- 一道素数打表的水题,顺便记下打表高效方法
- 【C++心路历程6】打表大法好 素数没烦恼
- 一道好题
- dfs好题一道
- 一道搜索好题
- 一道搜索好题
- 好长的SQL!
- 好长的序列
- 好长的SQL
- 一道超级好的笔试题
- 【BZOJ2759】一道动态树的好题
- Java关闭线程的安全方法
- python的应用
- linux网络 socket
- hihococer 1077 线段树
- oc中import和include的区别
- hdu 3823 素数打表+vector可变长数组 好烦的一道好题。。。。。
- hdu1233
- Android酷炫实用的开源框架(UI框架)
- 数据结构--排序
- 黑客技术之“手动操作栈”
- RxJava 2.0中backpressure(背压)概念的理解
- 前端学习之jQuery
- Java 8 Lambda函数编程入门(三)
- malloc和new区别