题意:如标题
思路:如果n在10^6以内则可以用o(nlogn)的暴力,题目给定的是n<=1e8,暴力显然是不行的,考虑到1到n的最小公倍数可以写成2^p1*3^p2*5^p3*...这种素数的幂的乘积形式,对于当前询问n,可知质数x的指数为(int)log(x,n)(因为要满足是公倍数且最小),因为前n个数有n/logn个质数,这样一次处理为o(n/logn*logn) = o(n)的。由于有T组测试数据,直接T次处理肯定会超时,需要离线处理。具体怎么操作呢?首先将T个询问按n从小到大排序,从小到大处理时,每个质数的指数是非递减的,所以只需在上一次的答案上乘以若干质数。如果记录上一个询问后的每个质数的指数,然后遍历所有质数,看质数有没有增加,这样的时间复杂度为o(T*n/logn+n/logn*logn)=o(Tn/logn),跟直接T次在线处理没什么两样,原因是有很多的质数的指数并不会变化,却也被访问了一次。
一种解决办法是预先计算出每个询问n所增加的质数,从小到大枚举每个质数的每个幂,然后在T个询问中二分,得到第一次出现这个幂的n,这样时间复杂度为o(n/logn*logn*logT)=o(nlogT),查询时就相当于是只查了那个最大的n一样,所以查询复杂度为o(n),总复杂度为o(nlogT),虽然过不了此题,但这个思路却可以对付一些其他T比较大,n稍小的数据。
下面讲另一种o(Tsqrt(n)+n)的思路:核心思想是将质数分为两类,一类是小于等于sqrt(n),一类大于sqrt(n),不难发现对于第二类质数,它们的质数要么是1要么是0,也就是说,对于第二类质数,假设它在某个询问n时加进了答案,那么在以后的询问中就不用再考虑了(如果继续加进答案,那么它的指数会超过1),于是不难得到如下算法:对每个询问,枚举第一类质数,判断他们的质数有没有增加(实际操作时用试乘法而不是求一个对数),而第二类质数只需维护当前乘到了哪个质数就行了。下面是代码:
1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <set> 16 #include <bitset> 17 #include <functional> 18 #include <numeric> 19 #include <stdexcept> 20 #include <utility> 21 22 using namespace std; 23 24 #define mem0(a) memset(a, 0, sizeof(a)) 25 #define mem_1(a) memset(a, -1, sizeof(a)) 26 #define lson l, m, rt << 1 27 #define rson m + 1, r, rt << 1 | 1 28 #define define_m int m = (l + r) >> 1 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 32 #define rep_down1(a, b) for (int a = b; a > 0; a--) 33 #define all(a) (a).begin(), (a).end() 34 #define lowbit(x) ((x) & (-(x))) 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 38 #define pchr(a) putchar(a) 39 #define pstr(a) printf("%s", a) 40 #define sstr(a) scanf("%s", a) 41 #define sint(a) scanf("%d", &a) 42 #define sint2(a, b) scanf("%d%d", &a, &b) 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c) 44 #define pint(a) printf("%d\n", a) 45 #define test_print1(a) cout << "var1 = " << a << endl 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl 48 #define mp(a, b) make_pair(a, b) 49 #define pb(a) push_back(a) 50 51 typedef unsigned int uint; 52 typedef long long LL; 53 typedef pair<int, int> pii; 54 typedef vector<int> vi; 55 56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; 57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 }; 58 const int maxn = 1e8 + 17; 59 const int md = 10007; 60 const int inf = 1e9 + 7; 61 const LL inf_L = 1e18 + 7; 62 const double pi = acos(-1.0); 63 const double eps = 1e-6; 64 65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 68 template<class T>T condition(bool f, T a, T b){return f?a:b;} 69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 70 int make_id(int x, int y, int n) { return x * n + y; } 71 72 int vis[8000000]; 73 unsigned int prime[6000000]; 74 75 int c; 76 77 void init() { 78 const int t = 1e8 + 13; 79 for (int i = 2; i <= t; i ++) { 80 int u = i >> 4, v = i & 15; 81 if (vis[u] & (1 << v)) continue; 82 prime[c ++] = i; 83 if ((LL)i * i > t) continue; 84 for (int j = i * i; j <= t; j += i) { 85 int u = j >> 4, v = j & 15; 86 vis[u] |= 1 << v; 87 } 88 } 89 } 90 91 92 pii node[10010]; 93 unsigned int out[10010]; 94 const int max_sq = 1e4; 95 unsigned int last[10010]; 96 97 int main() { 98 //freopen("in.txt", "r", stdin); 99 int T, cas; 100 cin >> T; 101 cas = T; 102 init(); 103 while (T --) { 104 int n; 105 sint(n); 106 node[cas - T - 1] = make_pair(n, cas - T - 1); 107 } 108 sort(node, node + cas); 109 unsigned int ans = 1; 110 int n = node[0].first; 111 int cur = 0; 112 for (int i = 0; prime[i] < max_sq; i ++) last[i] = 1; 113 rep_up0(i, cas) { 114 int n = node[i].first; 115 for (int j = 0; prime[j] < max_sq; j ++) { 116 while ((LL)last[j] * prime[j] <= n) { 117 last[j] *= prime[j]; 118 ans *= prime[j]; 119 } 120 } 121 while (prime[cur] <= n) { 122 cur ++; 123 if (prime[cur - 1] < max_sq) continue; 124 ans *= prime[cur - 1]; 125 } 126 out[node[i].second] = ans; 127 } 128 rep_up0(i, cas) cout << out[i] << endl; 129 return 0; 130 }