题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6672
Problem Description
度度熊有一个递推式
其中 a1=1。现给出 n,需要求 an.
Input
第一行输入一个整数 T,代表 T (1≤T≤100000) 组数据。
接下 T 行,每行一个数字 n (1≤n≤1e12)。
Output
输出 T 行,每行一个整数表示答案。
Sample Input
5
1
2
3
4
5
Sample Output
1
1
0
3
0
这种题就是找规律,多写几个就能发现规律
我们先列举出部分
n | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
an | 1 | 1 | 0 | 3 | 0 | 3 | 5 | 4 | 1 | 9 | 1 | 6 |
n | 3 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
an | 9 | 7 | 2 | 15 | 2 | 9 | 13 | 10 | 3 | 21 | 3 | 12 |
由上述数据找出如下规律:6个数为一组
1 | 5 | 9 | 13 |
---|---|---|---|
1 | 4 | 7 | 10 |
0 | 1 | 2 | 3 |
3 | 9 | 15 | 18 |
0 | 1 | 2 | 3 |
3 | 6 | 9 | 12 |
代码如下:
#include<iostream>
using namespace std;
int main()
{
int n;
long long int a;
cin>>n;
for(int i=0;i<n;i++){
cin>>a;
long long int num = a/6;
if(a%6==1){
cout<<4*num+1<<endl;
}
else if(a%6==2){
cout<<3*num+1<<endl;
}
else if(a%6==3){
cout<<num<<endl;
}
else if(a%6==4){
cout<<num*6+3<<endl;
}
else if(a%6==5){
cout<<num<<endl;
}
else if(a%6==0){
cout<<3*num<<endl;
}
}
}