2021牛客暑期多校训练营3-E(Math)

https://ac.nowcoder.com/acm/contest/11254/E

枚举y <= 1000 的情况发现,对于每一个整数x一定有(x, x^3)满足。

#include <iostream>

int main(){

    for (int i = 1; i <= 1000; ++i) {
        for (int j = i; j <= 1000; ++j) {
            if((i * i + j * j)%(i * j + 1)==0){
                printf("(x:%d, y:%d)\n", i, j);
            }
        }
    }

    return 0;
}

在这里插入图片描述

x^2 + y^2 = k(xy + 1) // k是正整数
⇒ y^2 - kxy - k + x^2 = 0 // (1)
可以将x看成一个固定的值。
由韦达定理有 y1 + y2 = kx
对于一对满足条件的(x, y),有一对(x, kx - y)也满足条件。
所以(y, x)也满足条件,此时y是固定的,有(y, ky - x)条件。
对于上面打表找出的特例:(x,x^3)带入(1)式,得到 k = x^2。
所以有:
(x, x^3)
(x^3, x^5 - x)


无线套娃下去
直到y超出范围。

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef __int128 ll;  // unsigned long long 会溢出

vector<ll> da;

template<typename T>void write(T x){

    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9){
        write(x/10);
    }

    putchar(x%10+'0');
}

template<typename T> void read(T &x){

    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}

int main(){

    da.push_back(1);
    // i * i * i <= 1e18
    for (ll i = 2; i <= 1e6; ++i) {
        ll x = i, y = i * i * i, k = i * i;
        while (y <= 1e18){
            da.push_back(y);  // 将y值添加到序列
            ll tmp = k * y - x;
            x = y;
            y = tmp;
        }
    }
    // 排序
    sort(da.begin(), da.end());

    int t;
    cin >> t;
    while (t--){
        ll n;
        read(n);
        int ind = upper_bound(da.begin(), da.end(), n) - da.begin();
        write(ind);
        cout << endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值