跳到正文

题解

题解:AT_abc420_g sqrt(n²+n+X)

luuia约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月19日
篇幅
约 3 分钟

版本

  1. v1当前

这种傻逼题也能放 G??

Question

给定 xx,求出所有使得 n2+n+x\sqrt{n^2+n+x} 为整数的 nn。

Solution

不妨设 m=n2+n+xm = \sqrt{n^2+n+x},那么可以推得:

(2m−2n+1)(2m+2n+1)=4x−1(2m-2n+1)(2m+2n+1) = 4x-1

那么直接枚举每个 4x−14x-1 的因数算出来 nn check 一下就行了,时间复杂度 O(x)O(\sqrt x)。

Code

粘了一个 int128 的开根,不过好像不需要 int128。

C++
/**
 *    author: luuia
 *    created: 2025.08.24 20:32:16
**/
#include<bits/stdc++.h>
using ll = long long;
using ld = long double;
using i128 = __int128;
#define For(i,j,k) for(int i = j;i <= k;i++)
#define Rep(i,j,k) for(int i = j;i >= k;i--)
#define pb push_back
using namespace std;
const int N = 1e6 + 10,p = 998244353;
bool sqrtt(i128 x){if(x < 0) return 0;ld dx = x;
    i128 r = floor(sqrt(dx) + 0.5L),rr = (i128)r * r;
    if(rr == x) return 1;if(((i128)(r + 1) * (r + 1)) == x) return 1;
    if(r > 0 && ((i128)(r - 1) * (r - 1)) == x) return 1;return 0;
}void sol(){ll x;cin >> x;ll m = x * 4 - 1,n = llabs(m);vector<ll> o;
    set<ll> s;for(ll i = 1;i * i <= n;i++){if(n % i) continue;
        for(auto f : {1,-1}){ll a = f * i,b = m / a;if(m % a) continue;
            if(a & 1 == 0 || b & 1 == 0 || a + b & 3 || b - a - 2 & 3) continue;
            i128 t = (b - a - 2) / 4,v = t * t + t + x;
            if(sqrtt(v) && !s.count(t)) s.insert(t),o.pb(t);
        }
    }sort(o.begin(),o.end());cout << o.size() << '\n';
    for(auto x : o) cout << x << " ";cout << '\n';
}int main(){
    // freopen("input.in","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    ll T = 1;while(T--) sol();return 0;
}
原文