题目:

https://ac.nowcoder.com/acm/contest/23479/J

求[l,r]区间内所有合数的最小公倍数%(1e9+7)

  1. 用埃氏筛筛掉所有质数,剩下为合数

  2. 求最小公倍数有3种方法

    • 直接求最小公倍数
    • 用$A*B/gcd(A,B)$就是两数之积 / 最大公因数
    • 分解质因数:$A = 48=22223$ $B=210=2357$,然后两者共有的质因数C:(2,3),两者不共有的A:(2,2,2),B:(5,7)三者相乘 $232225*7 = 1680$

3.每一个合数的质因子都用unordered_map来维护,看这个质因子是否多于之前质因子的个数,将所有维护好的质因子相乘就是最小公倍数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#define debug(a) cout << #a << " = " << a << endl;
#define debug2(a,b) cout<<#a<<" = "<<a<<' '<<#b<<" = "<<b<<endl;
#define debug3(a,b,c) cout<<#a<<" = "<<a<<' '<<#b<<" = "<<b<<' '<<#c<<" = "<<c<<endl;
#define debug4(a,b,c,d) cout<<#a<<" = "<<a<<' '<<#b<<" = "<<b<<' '<<#c<<" = "<<c<<' '<<#d<<" = "<<d<<endl;
#define debugx(a, idx) cout << #a << "[" << idx << "] = " << a[idx] << endl;
#define debugarr(a, x) cout<<#a<<": "; for(int i = 0; i < x; i++) cout << a[i] << ' '; cout << endl;
#define debugvec(a) cout<<#a<<": "; for(int i = 0; i < a.size(); i++) cout << a[i] << ' '; cout << endl;
#define sc(x) scanf_s("%d",&x)
#define SC(x) scanf_s("%lld",&x)

#include <iostream>
#include<algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> piii;
const int N = 30010;

//快速幂
//ll qpow(ll b, ll n) {
// ll ret = 1;
// b %= mod;
// for (; n; n >>= 1, b = (b * b) % mod)
// if (n & 1) ret = (ret * b) % mod;
// return ret;
//}
ll res = 1;
const int mod = 1000000007;
bool st[N];
unordered_map<int, bool> mp;
unordered_map<int, int> mp2;
ll gcd(ll a, ll b)
{
ll temp = 0;
while (temp = a % b)
{
a = b;
b = temp;
}
return b;
}

void divide(int n) {
unordered_map<int, int> mp1;
mp1.clear();
for (int i = 2; i <= n / i; i++) {
if (n % i == 0) {
int s = 0;
while (n % i == 0) {
n /= i;
s++;
}
//cout << i << " " << s << endl;
mp1[i] += s;
}
}
//if (n > 1)cout << n << " " << 1 << endl;
//puts("");
if(n > 1)mp1[n] += 1;
for (auto m : mp1) {
int x = m.first, y = m.second;
//cout << x << " " << y << endl;
if (mp2[x] < y)for (int i = 0; i < y - mp2[x]; i++)res = (res % mod * x) % mod;
mp2[x] = max(y, mp2[x]);
}
//cout << endl;
}

void get_prime(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i])mp[i] = true;//把素数存起来
for (int j = i; j <= n; j += i) {//不管是合数还是质数,都用来筛掉后面它的倍数
st[j] = true;
}
}
}

void f() {
int l, r;
cin >> l >> r;
get_prime(r);
vector<int> v;
for (int i = l; i <= r; i++) {
if (mp[i] == false) {
v.push_back(i);
}
}
int n = v.size();
if (n == 0) {
cout << -1 << endl;
return;
}
//res = v[0];
//a * b / gcd(a, b) << endl;
//res = ((res * v[i]) % mod / gcd(res, v[i])) % mod;
for (int i = 0; i < n; i++) {
divide(v[i]);
}
cout << res % mod<< endl;
}
int main()
{
f();
return 0;
}