Alarm
DESCRIPTION
Given a number sequence [3,7,22,45,116,...][3,7,22,45,116,...]. Please tell me the kk-th number.
INPUT
A number T (T<100)T (T<100) indicates the number of the input cases. Then for each case there only is one integer k (1≤k≤10000)k (1≤k≤10000).
OUTPUT
For each case, ouput the kk-th number of the sequence in one line.
SAMPLE INPUT
2 1 4
SAMPLE OUTPUT
3
45
题目简洁易懂,数学知识很重要啊。找呀找呀找规律:
数列的第n项就是第n个素数的平方减去n(就拿第5个数116来说,第五个素数是11,所以116=11*11-5),不信的话可以自己试试。。
#include<cstdio>
#include<cstring>
#define N 1000000+11
int sushu[N];
int a[10005];
void su(){//素数打表
int k=1;
memset(sushu,0,sizeof(sushu));
for(int i=2;i<N;i++){
if(!sushu[i]){
for(int j=2*i;j<N;j+=i){
sushu[j]=1;
}
}
if(!sushu[i]){
sushu[i]=1;
a[k++]=i;//按顺序记录素数
if(k>10001)//记下前10000个就行
break;
}
}
}
int main(){
int t,k;
long long sum;
su();
scanf("%d",&t);
while(t--){
scanf("%d",&k);
sum=(long long)a[k]*a[k]-k;//数型转换
printf("%lld\n",sum);
}
return 0;
}