玲珑学院 1010 - Alarm

本文介绍了一种通过计算素数平方减去其位置值来获取特定数列中第k个元素的方法。使用C++实现,涉及素数筛选、内存初始化等步骤,并提供了完整的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1010 - Alarm

Time Limit:1s Memory Limit:128MByte

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 (1k10000)

.
OUTPUT
For each case, ouput the k
-th number of the sequence in one line.
SAMPLE INPUT
2
1
4
SAMPLE OUTPUT
3
45
第一次做这种题,我的内心是崩溃的找规律找规律  ans=prime(n)^2-n;
来一个数列查找链接 http://oeis.org/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll a[105000];
ll vis[10006];
void getprime()
{
    memset(a,0,sizeof(a));
    a[1]=1;
    for(ll i=2;i<105000;i++)
    {
        if(a[i]==1) continue;
        for(ll j=2;j*i<105000;j++)
            a[j*i]=1;
    }
    ll ans=1;
    for(ll i=2;i<105000;i++)
    {
        if(!a[i]) vis[ans++]=i;
        if(ans>=10002) break;
    }
}
int main()
{
    ll t,n;
    scanf("%lld",&t);
    getprime();
    while(t--)
    {
        scanf("%lld",&n);
        printf("%lld\n", vis[n]*vis[n]-n);
    }
}

 

转载于:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7182233.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值