題目連結: https://tioj.ck.tp.edu.tw/problems/1615
# 內容
給定二數 A、B,求 A!+B! 的質因數個數
# 思路
將其拆成 A!*(B!/A! + 1),B>=A,
再去尋找 A 以下的質數及 B!/A!+1 的質因數量就好啦!!
記得最後可能還要加一喔~
# 程式碼
#include <bits/stdc++.h> | |
using namespace std; | |
#define int long long | |
#define maxn 1000000 | |
bitset < maxn > lib; | |
vector < int > prime; | |
signed main(){ | |
ios::sync_with_stdio ( false ); | |
lib[0] = lib[1] = true; | |
for ( int i = 2 ; i < maxn ; i++ ){ | |
if ( !lib[i] ){ | |
prime.push_back ( i ); | |
for ( int j = i ; j < maxn ; j += i ) | |
lib[j] = true; | |
} | |
} | |
int a, b, len = prime.size(); | |
int ans, tmp; | |
while ( cin >> a >> b ){ | |
if ( a > b ) swap ( a, b ); | |
//prime that less than A | |
ans = upper_bound ( prime.begin(), prime.end(), a ) - prime.begin(); | |
tmp = 1; | |
for ( int i = a + 1 ; i <= b ; i++ ) tmp *= i; | |
++tmp; | |
for ( int i = 0 ; i < ans ; i++ ) | |
while ( tmp % prime[i] == 0 ) tmp /= prime[i]; | |
//B! / A! +1 -> prime | |
for ( int i = ans ; i < len ; i++ ){ | |
if ( tmp % prime[i] == 0 ){ | |
++ans; | |
while ( tmp % prime[i] == 0 ) tmp /= prime[i]; | |
} | |
} | |
// if prime too large | |
if ( tmp > maxn ) ans++; | |
cout << ans << '\n'; | |
} | |
} |