Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求解各路大神帮忙看下,讨论版的测试数据都过了- -拍点也都过了,但一交就WA

Posted by hzoi at 2014-03-24 06:18:18 on Problem 2689
#include <cstdio>
#include <cstring>
#include <iostream>
#define INF 0x7f7f7f7f
using namespace std;

const int rf = 46340;

int maxans,minans,minx,miny,maxx,maxy;
int i,j,ll,rr,x = 0,y = 0,z,temp;
int len,l,r;
bool v[1010000],flag;
int su[5000];

void db(){
	memset(v,1,sizeof(v));
	for (i = 2; i <= rf; i++){
		if (v[i]){
			su[++len] = i; 
			for (j = i+i; j <= rf; j+=i) v[j] = 0;
		}
	}
}

int main(){
	db();
	while (~scanf("%d%d",&l,&r)){
		memset(v,1,sizeof(v));
		minans = INF; maxans = -INF;
		x = 0; y = 0;
		if (l == 1) v[0] = 0;
		for (i = 1; i <= len; i++){
			ll = l/su[i]; rr = r/su[i];
			for (j = l/su[i]; j <= r/su[i]; j++)
				if (j > 1 && su[i]*j-l >= 0) v[su[i]*j-l] = 0;
		}
		for (i = l; i < r; i++){
			if (v[i-l]){
				y = i;
				if (x > 0){
					if (y-x > maxans){
						maxans = y-x;
						maxx = x; maxy = y;
					}
					if (y-x < minans){
						minans = y-x;
						minx = x; miny = y;
					}
				}
			x = y;
			}
		}
		if (v[r-l]){
			if (x > 0){
				if (r-x > maxans){
					maxans = r - x;
					maxx = x; maxy = r;
				}
				if (r-x < minans){
					minx = x; miny = r;
				}
			}
		}
		if (maxans > 0)
				printf("%d,%d are closest, %d,%d are most distant\n",minx,miny,maxx,maxy);
		else 
			printf("There are no adjacent primes.\n");
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator