Lonlife-ACM 1010 - Alarm(找规律+素数打表)

来源:互联网 发布:黄山招聘美工 编辑:程序博客网 时间:2024/05/29 14:01

此文章可以使用目录功能哟↑(点击上方[+])

 Lonlife-ACM 1010 - Alarm

Accept: 0    Submit: 0
Time Limit: 1s    Memory Limit : 128MByte

 Problem Description

Given a number sequence [3,7,22,45,116,...]. Please tell me the k-th number.

 Input

A number T (T<100) indicates the number of the input cases. Then for each case there only is one integer k (1≤k≤10000).

 Output

For each case, ouput the k-th number of the sequence in one line.

 Sample Input

2
1
4

 Sample Output

3
45

 Hint

 Problem Idea

解题思路:

【题意】
给出数列的前5项:3,7,22,45,116

问该数列的第n项是多少

【类型】
找规律+素数打表
【分析】

很显然,此题除了找规律就只能找规律

当然这是在不借助外力的情况下

如果借助外力的话就毫无悬念了

在这介绍一下这种神奇的“外力

链接->数列查询(OEIS)

打开网页之后,在框框内输入数列的已知项


点“Search”键查询


可见,这个数列的第n项=第n个素数的平方-n

那么我们需要做的就是将前10000个素数打表找出来

此题得解

【时间复杂度&&优化】
O(nlogn)

题目链接→Lonlife-ACM 1010 - Alarm

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<bitset>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define eps 1e-9#define LL long long#define PI acos(-1.0)#define bitnum(a) __builtin_popcount(a)using namespace std;const int N = 104730;const int M = 100005;const int inf = 1000000007;const int mod = 1000000007;int prime[N],k=1;bool v[N];void get_prime(){    memset(v,false,sizeof(v));    for(int i=2;i<N;i++)        if(!v[i])        {            prime[k++]=i;            for(int j=i+i;j<N;j+=i)                v[j]=true;        }}int main(){    get_prime();    int t,n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        printf("%lld\n",1ll*prime[n]*prime[n]-n);    }    return 0;}
菜鸟成长记

0 0