Comet OJ - Contest # 10 C. fish big splash

Portal

The meaning of problems:
find the smallest \ (X \) , satisfies \ (\ {X FRAC (X +. 1)} {2} \ = 0 n-%, n-\ Leq 12 is 10 ^ {} \) .
A plurality of sets of data, \ (T \ Leq 100 \) .

Ideas:

  • Consider modular arithmetic seems to be directly related to what the rest of the secondary, but more complicated.
  • Notes that more special is that the final result is \ (0 \) , then we will consider the conversion problem divisible.
  • It is equivalent to equation \ (n | \ frac {x (x + 1)} {2} \) i.e. \ (2N | X (+ X. 1) \) .
  • Noting \ (n-\) range, then we can \ ((O \ sqrt {n }) \) to enumerate \ (P, Q \) , satisfies \ (PQ = 2N \) .
  • Then there \ (pq | the X-(the X-+ 1) \) , may wish to set \ (= the X-qb, the X-+ 1 = PA \) , then there \ (PA-qb = 1 \) , now equivalent to know \ (P, Q \) , the smallest positive solution to solve \ (a, b \) such that \ (X \) is minimized.
  • Obviously, this is a classic problem of extended Euclid.

Because this title cards often, so we will enumerate enumeration into prime factors, take note of when the last quality factorization should first prime number sieve out, or will be T.
Finally, the extended Euclidean get the solution when the note, we can choose Positive or negative term treatment, I chose a negative term treatment by direct answer.
(This has nothing to do tangled for a while, it is to deal with the time and location of the item being processed by the first processing regardless of which one is, regardless of which is the second negative term treatment.)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 35, MAX = 1e6 + 5;

int T;
ll n;

bool chk[MAX];
int prime[MAX], tot;

void pre() {
    for(int i = 2; i < MAX; i++) {
        if(!chk[i]) {
            chk[i] = 1;
            prime[++tot] = i;
            for(ll j = 1ll * i * i; j < MAX; j += i) {
                chk[j] = 1;
            }
        }
    }
}

void exgcd(ll a, ll b, ll &x, ll &y) {
    if(b == 0) {
        x = 1, y = 0;
        return ;
    }
    exgcd(b,a%b,x,y);
    ll z = x ;
    x = y;
    y = z - y * (a / b);
}

int cnt;
ll tmp[N];

ll calc(ll a, ll b) {
    ll x, y;
    exgcd(a, b, x, y);
    y = (y % a + a) % a;
    if(y * b >= 0) y -= a;
    return -y * b;//按负项处理
}

ll dfs(int num, ll a, ll b) {
    if(num > cnt) return calc(a, b);
    return min(dfs(num + 1, a * tmp[num], b), dfs(num + 1, a, b * tmp[num]));
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    pre();
    cin >> T;
    while(T--) {
        cin >> n;
        n <<= 1;
        cnt = 0;
        for(int i = 1; i <= tot; i++) {
            if(prime[i] > n) break;
            if(n % prime[i] == 0) {
                tmp[++cnt] = 1;
                ll now = prime[i];
                while(n % prime[i] == 0) {
                    tmp[cnt] *= now;
                    n /= prime[i];
                }
            }
        }
        if(n > 1) tmp[++cnt] = n;
        ll ans = dfs(1, 1, 1);
        cout << ans << '\n';
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/heyuhhh/p/11502960.html