UVA - 1404 Prime k-tuple (素数筛选)
来源:互联网 发布:网校程序源码 编辑:程序博客网 时间:2024/06/06 04:15
Description
{p1,..., pk : p1 <p2 <...< pk} is called a prime k -tuple of distance s if p1, p2,..., pk are consecutive prime numbers andpk - p1 = s . For example, withk = 4 , s = 8 ,{11, 13, 17, 19} is a prime 4-tuple of distance 8.
Given an interval [a, b] , k , and s , your task is to write a program to find the number of primek -tuples of distance s in the interval[a, b] .
Input
The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.
For each data set, there is only one line containing 4 numbers, a , b , k ands(a, b < 2* 109, k < 10, s < 40) .
Output
For each test case, write in one line the numbers of prime k -tuples of distance s .
Sample Input
1100 200 4 8
Sample Output
2题意:如果有k个相邻的素数,满足pk-p1=s,称这些素数组成一个距离为s的素数k元组,输出区间[a, b]内距离为s的k元组思路:首先筛选出素数,然后在区间[a,b]内查找满足的个数,还有我决定了以后刷选用long long#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 100005;int prime[maxn], vis[maxn], cnt;void init() {memset(vis, 0, sizeof(0));vis[1] = vis[0] = 1;cnt = 0;for (ll i = 2; i < maxn; i++) if (!vis[i]) {prime[cnt++] = i;for (ll j = i*i; j < maxn; j += i) vis[j] = 1;}}int check(int n) {if (n < maxn)return vis[n] == 0;for (int i = 0; i < cnt && prime[i]*prime[i] <= n; i++)if (n % prime[i] == 0)return 0;return 1;}int main() {init();int t, a, b, k, s;scanf("%d", &t);while (t--) {scanf("%d%d%d%d", &a, &b, &k, &s);int tmp = 0, ans = 0;vector<int> num;for (int i = a; i <= b; i++)if (check(i))num.push_back(i);int size = num.size();for (int i = 0; i+k-1 < size; i++)if (num[i+k-1] - num[i] == s)ans++;printf("%d\n", ans);}return 0;}
- UVA - 1404 Prime k-tuple (素数筛选)
- UVA 1404 - Prime k-tuple(素树筛选)
- uva 1404 - Prime k-tuple(数论)
- UVa 1644 - Prime Gap(筛选求素数)
- UVa 10539 - Almost Prime Numbers(筛选求素数)
- Prime Distance(大素数筛选)
- Prime Distance(筛选法求素数 )
- poj1595 Prime Cuts (素数筛选)
- UVa1404 & LA3998 Prime k-tuple
- UVa 1644 - Prime Gap(筛法选素数)
- UVA 10200Prime Time(素数)
- POJ 2689 Prime Distance (经典素数筛选)
- POJ 2689 Prime Distance (素数+两次筛选)
- POJ 2689 Prime Distance 求素数距离(两次筛选)
- [ACM] POJ 2689 Prime Distance (大区间素数筛选)
- HDU 2136 Largest prime factor (筛选法求素数)
- 素数筛选(2次):poj2689 Prime Distance
- POJ题目2689 Prime Distance(任何区间素数筛选)
- MySQL错误2003:Can't connect to MySQL server (10060)
- springMVC中遇到的问题
- Asmack之旅(二)packet扩展
- 求字符串最大的重复字串
- VBS基础篇 - 杂项 - 字符集
- UVA - 1404 Prime k-tuple (素数筛选)
- 如何修改android工程的包名
- IIR 滤波器的实现(C++)
- c# 压缩与解压
- 利用poi导出word excel
- 什么是守护进程?
- coco2dx-3.2 MAC版本环境搭配细节
- 自定义组合控件属性,自定义对话框,md5加密,自定义按钮背景,
- storm简介