Lonlife-ACM 1010 - Alarm(找規律+素數打表)

此文章可以使用目錄功能喲↑(點擊上方[+])

 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;
}
菜鳥成長記

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章