2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛---Half-consecutive Numbers

  1. Half-consecutive Numbers

  2. The numbers 113366101015152121282836364545 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 (1N1016).

    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 -11.

    样例输入
    4
    1
    2
    9
    50
    样例输出
    Case #1: 1
    Case #2: 8
    Case #3: 49
    Case #4: 288
  3. 题目大意:给出一个n,找到一个不小于n的半连续数(half-consecutive.),且这个数为平方数。
  1. 大体程序如下:
  2. #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; 
    }  

    看到我数组里面写的数,可能有的同学就会问这些数是咋来的,下面砸门就先分析一下这些数的特征:
  3. 序号     半连续数          a     b    (为了好说明问题,下面所提到的a,b数不包括平方)
  4. 1              1      ==     1^2*1^2
  5. 8            36      ==     2^2*3^2
  6. 49       1225     ==     5^2*7^2
  7. 288    41616    ==     12^2*17^2
  8. 想必大家都能看懂,下一个数的a等于上一个数的a+b;
  9. 而b的值则要分a的奇偶,当a为奇数时,b=sqrt(a^2*2-1),当a为偶数时,b=sqrt(a^2*2+1);
  10. 到了最重要的环节,就是序号的问题,序号和后面的a也有关系,如果a为偶数,序号=a^2*2;如果a为奇数,序号=a^2*2-1;
  11. 下面就到了我们打表的环节;
  12. #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;
    } 



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值