Given an integer n, you have to find
lcm(1, 2, 3, ..., n)
lcm means least common multiple. For example lcm(2, 5, 4) = 20, lcm(3, 9) = 9, lcm(6, 8, 12) = 24.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (2 ≤ n ≤ 108).
Output
For each case, print the case number and lcm(1, 2, 3, ..., n). As the result can be very big, print the result modulo 232.
Sample Input
5
10
5
200
15
20
Sample Output
Case 1: 2520
Case 2: 60
Case 3: 2300527488
Case 4: 360360
Case 5: 232792560
題目大意:給出幾個n,求出1到n的最小公倍數
思路概括:
1、埃拉託斯特尼篩法,這裏有個不錯的圖來表示
一百以內有 25 個素數,它們分別是
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97。
一千以內有 168 個素數,最後幾個是907,911,919,929,937,941,947,953,967,971,977,983,991,997。
一萬以內有 1229 個素數,最後幾個是9901,9907,9923,9929,9931,9941,9949,9967,9973。
十萬以內有 9592 個素數,最後幾個是99901,99907,99923,99929,99961,99971,99989,99991。
一百萬以內有 78498 個素數,最後幾個是999907,999917,999931,999953,999959,999961,999979,999983。
一千萬以內有 664579 個素數,最後幾個是9999901,9999907,9999929,9999931,9999937,9999943,9999971,9999973,9999991。
一億以內有 5761455 個素數,最後幾個是99999931,99999941,99999959,99999971,99999989。
十億以內有 50847534 個素數,最後幾個是999999929,999999937。
一百億以內有 455052511 個素數,最後幾個是9999999929,9999999943,9999999967。
比較好的思路是把主要的數據先都預處理出來。
定義L(x)爲 1, 2, 3, .., x的LCM
則有如下規律:
L(1) = 1L(x+1) = { L(x) * p if x+1 is a perfect power of prime p
{ L(x) otherwise
也就是當x+1是素數p的整數次冪的時候,L(x+1)=L(x)*p;舉例如下:L(2) = 1 * 2
L(3) = 1 * 2 * 3
L(4) = 1 * 2 * 3 * 2 // because 4 = 2^2
L(5) = 1 * 2 * 3 * 2 * 5
L(6) = 1 * 2 * 3 * 2 * 5 // 6 is not a perfect power of a prime
L(7) = 1 * 2 * 3 * 2 * 5 * 7
於是我們可以先把素數連乘的結果預處理出來,然後再對每一個素數的整數次冪根據n的不同進行操作。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define lt k<<1
#define rt k<<1|1
#define lowbit(x) x&(-x)
#define lson l,mid,lt
#define rson mid+1,r,rt
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mem(a, b) memset(a, b, sizeof(a))
//#define int ll
const double pi = acos(-1.0);
const double eps = 1e-6;
const double C = 0.57721566490153286060651209;
const ll mod = 1ll << 32;
const int inf = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const uint INF = 0xffffffff;
const int maxn = 1e8 + 5;
unsigned int pri[5800000];
unsigned int sum[5800000];
bitset<maxn> vis;
int cnt = 1;
void prime()//歐拉篩
{
pri[0] = 0;
for(int i=2; i<maxn; i++)
{
if(!vis[i]) pri[cnt++] = i;
for(int j=1; j<cnt && i * pri[j] < maxn; j++)
{
vis[i * pri[j]] = true;
if(i % pri[j] == 0) break;
}
}
sum[0] = 1;
for(int i=1; i<cnt; i++)//前綴積
{
sum[i] = sum[i-1] * pri[i];
}
}
ll q_mod(int a, int b)//快速冪
{
ll ans = 1;
ll base = a;
while(b)
{
if(b & 1) ans = (ans * base) % mod;
base = (base * base) % mod;
b >>= 1;
}
return ans;
}
int main()
{
prime();
int t;
cin >> t;
int k = 1;
while(t--)
{
int n;
cin >> n;
int pos = upper_bound(pri+1, pri+cnt, n) - pri - 1;//二分查找第一個比n大的數,然後減去1,就小於等於n
ll ans = sum[pos] % mod;
for(int i=1;i<cnt && pri[i] * pri[i] <= n;i++)
{
int x = n;
int y = 0;
while(x/pri[i])//每個素數的最大次方
{
x /= pri[i];
y++;
}
ans = q_mod(pri[i], --y) * ans % mod;
}
cout << "Case " << k++ << ": ";
cout << ans % mod << endl;
}
return 0;
}