1. 程式人生 > 實用技巧 >《2020牛客NOIP賽前集訓營-提高組(第二場)》

《2020牛客NOIP賽前集訓營-提高組(第二場)》

提高組都是神仙打架%%%

T1:

對於每個素數i,顯然f[i] = i.

那麼根據唯一分解定理,對於每個合數i,如果它分解後,有多個素因子,那麼顯然為1.

否組就是最小的素因子。

那麼,我們可以線上性篩中處理即可。

線性篩,其實就是利用素數和一個和當前素數肯定有不同素因子的數乘積來篩合數的過程。

那麼,在判斷中,當這個數首先要滿足只有一個素因子,那麼在滿足i % prime[j]時,顯然篩出的合數只有一個素因子prime[j].

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair
<double,int> pii; const int N = 1e7+5; const int M = 1e6+5; const LL Mod = 1e9+7; //#define rg register #define pi acos(-1) #define INF 1e18 #define CT0 cin.tie(0),cout.tie(0) #define IO ios::sync_with_stdio(false) #define dbg(ax) cout << "now this num is " << ax << endl; namespace
FASTIO{ inline int read() { int x = 0,f = 1;char c = getchar(); while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();} return x * f; } void
print(int x){ if(x < 0){x = -x;putchar('-');} if(x > 9) print(x/10); putchar(x%10+'0'); } } using namespace FASTIO; bool vis[N]; int prime[N],tot = 0,f[N]; void init() { for(int i = 2;i < N;++i) { if(!vis[i]) { prime[++tot] = i; f[i] = i; } for(int j = 1;j <= tot && prime[j] * i < N;++j) { vis[i * prime[j]] = 1; f[i * prime[j]] = 1; if(i % prime[j] == 0 && f[i] != 1) f[i * prime[j]] = prime[j]; if(i % prime[j] == 0) break; } } } int main() { init(); int a,b;a = read(),b = read(); LL ans = 0; for(int i = a;i <= b;++i) ans += f[i]; printf("%lld\n",ans); system("pause"); return 0; }
View Code

T2: