- Half-consecutive Numbers
-
-
The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)ti=21i(i+1), are called half-consecutive.
For given NN, find the smallest rr which is no smaller than NN such that t_rtr is square.
Input Format
The input contains multiple test cases.
The first line of a multiple input is an integer TT followed by TT input lines.
Each line contains an integer N~(1\le N\le 10^{16})N (1≤N≤1016).
Output Format
For each test case, output the case number first.
Then for given NN, output the smallest rr.
If this half-consecutive number does not exist, output -1−1.
样例输入
4 1 2 9 50
样例输出
Case #1: 1 Case #2: 8 Case #3: 49 Case #4: 288
- 题目大意:给出一个n,找到一个不小于n的半连续数(half-consecutive.),且这个数为平方数。
- 大体程序如下:
#include <stdio.h> long long arr[21]={1,8,49, 288,1681,9800,57121, 332928,1940449,11309768, 65918161,384199200,2239277041, 13051463048,76069501249,443365544448, 2584123765441,15061377048200,87784138523761, 511643454094368,2982076586042449}; int main() { int T,k=1; scanf("%d",&T); while(T--) { long long n; scanf("%lld",&n); if(n>arr[20]) { printf("Case #%d: -1\n",k++); } for(int i=0;i<21;i++) { if(arr[i]>=n) { printf("Case #%d: %lld\n",k++,arr[i]); break; } } } return 0; }
看到我数组里面写的数,可能有的同学就会问这些数是咋来的,下面砸门就先分析一下这些数的特征:- 序号 半连续数 a b (为了好说明问题,下面所提到的a,b数不包括平方)
- 1 1 == 1^2*1^2
- 8 36 == 2^2*3^2
- 49 1225 == 5^2*7^2
- 288 41616 == 12^2*17^2
- 想必大家都能看懂,下一个数的a等于上一个数的a+b;
- 而b的值则要分a的奇偶,当a为奇数时,b=sqrt(a^2*2-1),当a为偶数时,b=sqrt(a^2*2+1);
- 到了最重要的环节,就是序号的问题,序号和后面的a也有关系,如果a为偶数,序号=a^2*2;如果a为奇数,序号=a^2*2-1;
- 下面就到了我们打表的环节;
-
#include<stdio.h> #include<math.h> int main(){ long long sum[50]; long long a,b,c; int i,j; sum[0]=1; a=1,b=1; for (i=1;i<50;i++) { if (i%2!=0) { c=(a+b)*(a+b)*2; sum[i]=c; a=a+b; b=sqrt(c+1); } else { c=(a+b)*(a+b)*2; sum[i]=c-1; a=a+b; b=sqrt(c-1); } } for(i=0;i<50;i++) { printf("%I64d\n",sum[i]); } return 0; }
-