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;
}
菜鸟成长记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值