“玲珑杯”ACM 热身赛 # 0.5 A -- Alarm 【素数打表】【找规律】

A -- Alarm

Time Limit:1s Memory Limit:128MByte



【思路】找规律:the k-th = 第k个素数^2 - k;

           3=2^2-1  7=3^2-2  22=5^2-3  45=7^2-4......

AC代码:

#include<cstdio>
#define H 1000005
long long s[H]={1,1};
long long x[H];
void sushu()
{
	long long i, j;
	for(i = 2; i <= H; i++)
	{
		if(s[i] == 1) continue;
		for(j = i*2; j <= H; j=j+i)
		    s[j] = 1;
	}
}
void fun()
{
	long long i, j = 1;
	for(i = 2; i <= H; i++)
	{
		if(s[i] == 0)
		{
			x[j] = i*i-j;
			j++;
		}
	}
}
int main()
{
	sushu();
	fun();
	long long t, k;
	scanf("%lld", &t);
	while(t--)
	{
		scanf("%lld", &k);
		printf("%lld\n", x[k]);
	}
	return 0;
} 
时间: 2024-12-09 04:09:08

“玲珑杯”ACM 热身赛 # 0.5 A -- Alarm 【素数打表】【找规律】的相关文章

【ZOJ】3785 What day is that day? ——浅谈KMP应用之ACM竞赛中的暴力打表找规律

首先声明一下,这里的规律指的是循环,即找到最小循环周期.这么一说大家心里肯定有数了吧,“不就是next数组性质的应用嘛”. 先来看一道题 ZOJ 3785 What day is that day? Time Limit: 2 Seconds      Memory Limit: 65536 KB It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are multiple tes

素数打表——找出1~100以内的素数

素数筛选法: #include <iostream> using namespace std; //素数打表模板 bool is_prime[100]; int main() { for(int i = 2;i<100;i++) { is_prime[i] = 1; } for(int i=2;i*i<100;i++){ if(is_prime[i]){ for(int j=i*i;j<100;j+=i){ is_prime[j] = 0; } } } //枚举1~100以内

Bestcoder round 18---A题(素数筛+素数打表+找三个素数其和==n)

Primes Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12    Accepted Submission(s): 11 Problem Description Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p

标记素数法(模板)+素数打表

#include <stdio.h> #include <string.h> #define N 3000000 int f[3000000]; int main() { memset(f, 0, sizeof(f)); int i, j; f[0]=1; f[1]=1; for(i=2; i<=N; i++) { if(f[i]==0) { for(j=i*2; j<=N; j+=i) { f[j]=1; //不是素数 } } } // 打印所有发f[i]==0, 即

Fermat’s Chirstmas Theorem (素数打表的)

Fermat’s Chirstmas Theorem Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice SDUTOJ 2093 Description In a letter dated December 25, 1640; the great mathematician Pierre de Fermat wrote to Marin Mersenne

“玲珑杯”ACM比赛 Round #19题解&amp;源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】

A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT 5 20 1314 SAMPLE OUTPUT 5 21 1317 SOLUTION “玲珑杯”ACM比赛 Round #19 题目链接:http://www.ifrog.cc/acm/problem/1145 分析: 这个题解是官方写法,官方代码如下: 1 #include <iostream>

“玲珑杯”ACM比赛 Round #19 B -- Buildings (RMQ + 二分)

“玲珑杯”ACM比赛 Round #19 Start Time:2017-07-29 14:00:00 End Time:2017-07-29 16:30:00 Refresh Time:2017-07-29 16:42:55 Private B -- Buildings Time Limit:2s Memory Limit:128MByte Submissions:590Solved:151 DESCRIPTION There are nn buildings lined up, and th

“玲珑杯”ACM比赛 Round #18 A 暴力水 C dp

“玲珑杯”ACM比赛 Round #18 计算几何你瞎暴力 题意:如果从一个坐标为 (x1,y1,z1)的教室走到(x2,y2,z2)的距离为 |x1−x2|+|y1−y2|+|z1−z2|.那么有多少对教室之间的距离是不超过R的呢? tags:坐标范围很小,瞎暴力 #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring>

“玲珑杯”ACM比赛 Round #18

"玲珑杯"ACM比赛 Round #18 Start Time:2017-07-15 12:00:00 End Time:2017-07-15 15:46:00 A -- 计算几何你瞎暴力 Time Limit:5s Memory Limit:256MByte Submissions:1764Solved:348 DESCRIPTION 今天HHHH考完了期末考试,他在教学楼里闲逛,他看着教学楼里一间间的教室,于是开始思考: 如果从一个坐标为 (x1,y1,z1)(x1,y1,z1)的