Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
54989 Ghost_Chris 连续的正整数 C++ 运行超时 0 1000 MS 264 KB 1069 2022-08-08 11:46:57

Tests(0/10):


#include <bits/stdc++.h> #define ll long long using namespace std; inline ll read(){ register ll x(0); register char c(getchar()); register short w(1); for (;c < '0' || c > '9';c = getchar()) if (c == '-') w = -1; for (;c >= '0' && c <= '9';c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * w; } int t; int main(){ ios::sync_with_stdio(false); t = read(); for (;t--;){ ll n(read()),p(read());bool flag(false); if (p == 1) { cout << 1 << endl; continue; } if (n * (n - 1) % p == 0){ if (n % p == 0) cout << 1 << endl; else cout << n - 1 << endl; continue; } if (n == 2 && p % 2 == 1){ cout << (p - n) * p / 2 << endl; continue; } for (int i(2);i <= sqrt(n);i++) if (n % i == 0) { cout << 1 << endl; flag = true; break; } else if ((n - 1) % i == 0) { cout << n - i << endl; flag = true; break; } else { cout << i << endl; flag = true; break; } if (flag) continue; cout << -1 << endl; } return 0; }


测评信息: