习题10-4 素数间隔 UVa1644

1.题目描述:点击打开链接

2.解题思路:根据题意可知最大的素数在int范围内,可以先算出1299709以内的所有素数,随后二分查找n附近的素数的位置即可。

3.代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define N 1300000
int vis[N];
vector<int>primes;
void init()
{
	int m = sqrt(N + 0.5);
	for (int i = 2; i <= m;i++)
	if (!vis[i])
	for (int j = i*i; j < N; j += i)
		vis[j] = 1;
	for (int i = 2; i <= N;i++)
	if (!vis[i])
		primes.push_back(i);
}
int main()
{
	//freopen("test.txt", "r", stdin);
	int n;
	init();
	while (scanf("%d", &n) != EOF&&n)
	{
		if (!vis[n])cout << 0 << endl;
		else
		{
			int L = 0, R = 100000;
			while (L < R)
			{
				int m = L + (R - L) / 2;
				if (primes[m] > n)R = m;
				else L = m + 1;
			}
			cout << primes[L] - primes[L - 1] << endl;
		}
	}
	return 0;
}
时间: 2024-08-08 17:47:36

习题10-4 素数间隔 UVa1644的相关文章

C语言程序设计教程(第三版)课后习题10.4

1353: C语言程序设计教程(第三版)课后习题10.4 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 296  Solved: 219[Submit][Status][BBS] Description 有n个整数,使前面各数顺序向后移m个位置,最后m个数变成前面m个数,见图.写一函数:实现以上功能,在主函数中输入n个数和输出调整后的n个数. Input 输入数据的个数n n个整数移动的位置m Output 移动后的n个数 Sample Input

hdu 5943(素数间隔+二分图匹配)

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 200    Accepted Submission(s): 64 Problem Description There is a kindom of obsession, so people in this kingdom do things ver

问题 1047: C语言程序设计教程(第三版)课后习题10.5

/******************************************************************** @file Main.cpp @date 2017-06-01 12:17:46 @author Zoro_Tiger @brief 问题 1047: C语言程序设计教程(第三版)课后习题10.5 http://www.dotcpp.com/oj/problem1047.html ***************************************

问题 1043: C语言程序设计教程(第三版)课后习题10.1

/******************************************************************** @file Main.cpp @date 2017-05-29 12:55:07 @author Zoro_Tiger @brief 问题 1043: C语言程序设计教程(第三版)课后习题10.1 http://www.dotcpp.com/oj/problem1043.html ***************************************

习题10-5 不同素数之和 UVa1213

1.题目描述:点击打开链接 2.解题思路:本题利用加法原理解决,设第k次累加之和为数字n的方案有d(n,k)种,那么不难得到如下递推式: d(n,k)=sum{d(n-pi,k-1)}(n-pi≥0) 其中pi代表第i个素数,这不难理解,假设第k-1次累加后数字之和为n-pi,方案有d(n-pi,k-1)种,第k次只有一种方案,就是加上pi.那么根据乘法原理知道,最后一步加上pi而得到数字n的方法有d(n-pi,k-1)*1种,而这样的pi只要满足pi≤n均是可能的,因此再根据加法原理,便得到了

第一章 课后习题 10

1 #include <iostream> 2 using namespace std; 3 int main() 4 { void sort(int x,int y,int z); 5 int x,y,z; 6 cin>>x>>y>>z; 7 sort(x,y,z); 8 return 0; 9 } 10 void sort(int x,int y,int z) 11 { 12 int temp; 13 if(x>y) {temp=x;x=y;y=t

习题10-6 连续素数之和 UVa1210

1.解题思路:点击打开链接 2.解题思路:本题要求寻找连续个素数相加为n的个数.由于n的范围不大, 因此可以事先打表.计算好所有的连续和的个数,最后直接输出即可. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<set> #include<vecto

1046: C语言程序设计教程(第三版)课后习题10.4

题目描述 有n个整数,使前面各数顺序向后移m个位置,最后m个数变成前面m个数,见图.写一函数:实现以上功能,在主函数中输入n个数和输出调整后的n个数. 输入 输入数据的个数n n个整数 移动的位置m 输出 移动后的n个数 样例输入 10 1 2 3 4 5 6 7 8 9 10 2 样例输出 9 10 1 2 3 4 5 6 7 8 1 #include <stdio.h> 2 #define N 100 3 4 // 移动一次 5 move(int a[], int n) 6 { 7 int

APUE 第三版 习题 10.5

这是书本上的答案: See ''Implementing Software Timers'' by Don Libes (C Users Journal, vol. 8, no. 11, Nov. 1990) for an example. A copy of this paper is available online at http://www.kohala.com/start/libes.timers.txt. 我参考上面提到的文档,使用 alarm() 以及time() 简略测试了一下.