Square Number(HDU-2281)

Problem Description

Find the biggest integer n (1 <= n <= N) and an integer x to make them satisfy 

Input

The input consists of several test cases. Each test case contains a integer N, 1<=N<=10^18.The input ends with N = 0.

Output

In one line for each case, output two integers n and x you have found.

Sample Input

1
2
0

Sample Output

1 1
1 1

题意:输入一个数 N,找到满足上式中的 n、x,且 n<=N

思路:

将右边式子的分子求和化简,有:x^2=\frac{(n+1)(2*n+1)}{6}

移项化简,有:(4n+3)^2 - 48*x^2 = 1

满足佩尔方程的形式,套入公式求解即可。

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
//#define LL long long
const int MOD=10007;
const int N=100+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;

long long n[N],x[N];
vector<long long> nn,xx;
void init(){
    n[0]=7LL,x[0]=1LL;
    nn.push_back(1LL),xx.push_back(1LL);
    for(int i=1;;i++){
        n[i]=7LL*n[i-1]+48LL*x[i-1];
        x[i]=n[i-1]+7LL*x[i-1];
        if(n[i]<0)
            break;
        if((n[i]-3)%4==0){
            nn.push_back((n[i]-3)/4);
            xx.push_back(x[i]);
        }
    }
    nn.push_back((long long)1e18+5);
}
int main(){
    init();
    long long k;
    while(scanf("%lld",&k)!=EOF&&k){
        for(int i=0;i<nn.size();i++){
            if(k<nn[i]){
                printf("%lld %lld\n",nn[i-1],xx[i-1]);
                break;
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值