subject
Topic: T group queries, each time a minimum positive integer satisfying x* (x + 1)% (2n) = 0.
T<=100, n<=1e12
Train of thought:
x -> x*(x + 1) % (2n) == 0
Convert to a | 2n, b = 2n | a;
Suppose ap = x + 1, x = bq.
The minimum positive integer solution bq of ap - bq = 1 is obtained simultaneously.
Because gcd(a, b) = 1 is the condition for the equation to be solved.
When enumerating, only enumerating each prime factor of a should be given 2n.
First, the prime number in 1e6 is pretreated, assuming that the number of different quality factors in D and 2n is K.
The time complexity of solving the first extended Euclidean equation is logn.
Overall time complexity
T(n) = O(T(D + 2^K * log n)
AC code;
/* find min x -> x*(x + 1) % (2n) == 0 -> suppose a | 2n, b = 2n | a; -> suppose ap = x + 1, x = bq, -> */ #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1e18; const int maxn = 1e6 + 10; bool isprime[maxn]; int cntp, prime[maxn]; void init(){ memset(isprime, 1, sizeof(isprime)); cntp = isprime[0] = isprime[1] = 0; for(int i = 2; i < maxn; ++i){ if(isprime[i]){ prime[++cntp] = i; } for(int j = 1; j <= cntp && i * prime[j] < maxn; ++j){ isprime[i * prime[j]] = 0; if(i % prime[j] == 0) break ; } } } ll fac[50]; void getfac(ll n){ fac[0] = 0; for(int i = 1; i <= cntp && prime[i] <= n; ++i){ if(n % prime[i] == 0){ fac[++fac[0]] = 1; while(n % prime[i] == 0) n /= prime[i], fac[fac[0]] *= prime[i]; } } if(n > 1) fac[++fac[0]] = n; } ll exgcd(ll a, ll b, ll &x, ll &y){ if(!b) return x = 1, y = 0, a; ll d = exgcd(b, a % b, x, y); ll t = x; x = y, y = t - (a / b) * y; return d; } ll ans; void dfs(int cur, ll a, ll b){ if(cur == fac[0] + 1){ ll x, y; exgcd(a, b, x, y); // cout << "---" << a << " " << b << " " << y << endl; y = -y % a; if(y <= 0) y += a; ans = min(ans, y * b); return ; } dfs(cur + 1, a * fac[cur], b); dfs(cur + 1, a, b * fac[cur]); } int main(){ init(); int T; scanf("%d", &T); while(T--){ ll n; scanf("%lld", &n); if(n == 1){ puts("1"); continue ; } n *= 2; getfac(n); ans = INF; dfs(1, 1, 1); printf("%lld\n", ans); } return 0; }