计蒜客 16954 Half-consecutive Numbers(数论+打表)

201 篇文章 10 订阅
16 篇文章 0 订阅

Description

定义 ti=i(i+1)2 t i = i ( i + 1 ) 2 ,给出 n n ,问不小于n的数中满足 tr t r 是完全平方数的最小的 r r

Input

第一行一整数T表示用例组数,每组用例输入一整数 n n (1n1016)

Output

输出满足 tr t r 是完全平方数的最小 r r ,如果不存在则输出1

Sample Input

4
1
2
9
50

Sample Output

Case #1: 1
Case #2: 8
Case #3: 49
Case #4: 288

Solution

首先求出满足 a(a+1)2=b2 a ( a + 1 ) 2 = b 2 的解 (a,b) ( a , b ) ,由于 (a,a+1)=1 ( a , a + 1 ) = 1 ,如果 a a 为偶数,则(a2,a+1)=1,说明 a2 a 2 a+1 a + 1 都是完全平方数,设 a=2x2,a+1=y2 a = 2 x 2 , a + 1 = y 2 ,其中 y y 为奇数,则问题转化为求满足y212是完全平方数的奇数 y y ,如果a是奇数,则 (a,a+12)=1 ( a , a + 1 2 ) = 1 ,说明 a,a+12 a , a + 1 2 都是完全平方数,设 a=y2,a+1=2x2 a = y 2 , a + 1 = 2 x 2 ,其中 y y 为奇数,则问题转化为求满足y2+12是完全平方数的奇数 y y ,综合上面两种情况,枚举奇数y可找到所有满足条件的 a a <script type="math/tex" id="MathJax-Element-31">a</script>,在一个较大范围内枚举打表即可

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
map<ll,int>m;
int check(ll n)
{
    int m=(int)sqrt(n+0.5);
    if((ll)m*m==n)return 1;
    return 0;
}
ll a[]={
1,
8,
49,
288,
1681,
9800,
57121,
332928,
1940449,
11309768,
65918161,
384199200,
2239277041,
13051463048,
76069501249,
443365544448,
2584123765441,
15061377048200,
87784138523761,
511643454094368,
2982076586042449,
17380816062160328};
int main()
{
    //for(int i=1;i<=2e8;i+=2)
    //{
    //  if(check(((ll)i*i-1)/2))printf("%I64d,\n",(ll)i*i-1);
    //  if(check(((ll)i*i+1)/2))printf("%I64d,\n",(ll)i*i);
    //}
    int T,Case=1;
    ll n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        int pos=lower_bound(a,a+24,n)-a;
        printf("Case #%d: %lld\n",Case++,a[pos]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值