[素數篩] 求n的不同因數之和能組成的連續1~x的x ZOJ4040
阿新 • • 發佈:2018-11-22
Number Theory
Time Limit: 1 Second Memory Limit: 65536 KB
Given an integer , calculate the smallest positive integer that is not a sum of distinct divisors of .
Input
There are multiple test cases. The first line of the input contains an integer (about 2000), indicating the number of test cases. For each test case:
The first line contains one integer ().
Output
For each test case output one line indicating the answer.
Sample Input
5
102
28
8
5
1
Sample Output
13
57
16
2
2
Author: LIU, Mingrui
Source: ZOJ Monthly, June 2018
#include <bits/stdc++.h> #define ll long long using namespace std; const int mn = 1e6; int cnt; bool is[mn + 10]; int pm[mn + 10]; void prime() { for (int i = 2; i <= 1e6 + 3; i++) { if(is[i]) continue; pm[++cnt] = i; for (int j = i + i; j <= 1e6 + 3; j += i) is[j] = 1; } } int main() { prime(); int T; scanf("%d", &T); while (T--) { ll n; scanf("%lld", &n); ll ans = 1; for (int i = 1; i <= cnt && pm[i] * pm[i] <= n; i++) { if (ans + 1 < pm[i]) break; int t = 0; while (n % pm[i] == 0) { t++; n /= pm[i]; } ll tans = ans; ll tt = pm[i]; for (int j = 1; j <= t; j++) { ans += tans * tt; tt *= pm[i]; if (ans + 1 < tt) break; } } if (n > 1 && ans + 1 >= n) ans *= (n + 1); printf("%lld\n", ans + 1); } return 0; }