题意
给定 a , b , k a, b, k a,b,k, 在每一回合中,可以将 a 或 b 除以 c (c > 1)。问是否可以在恰好的 k 回合中使得 a = b a=b a=b
思路
这道题赛时写了二十五分钟,赛后写了三十五分钟,还是不够快。犯的错误如下:
- 求约数个数最后没有考虑这个数本身是质数,在循环最后少了 if(x > 1) ++ret;
- 没有考虑算法本身复杂度,求解质因数+T: O ( n T ) O(\sqrt n T) O(nT), 1 e 9 \sqrt {1e9} 1e9 大约是 1e5 !!!开方是对指数相除!
- calc(g)忘乘2了,记得要把全过程在纸上演算模拟一遍
知识
- 素数个数的上限:1-n的素数个数大约为 n / l n n n/lnn n/lnn
- 因子个数的上限: d ( n ) < O ( n ) d(n) < O(\sqrt n) d(n)<O(n), 估计:1e12的数最多有5000个质因子
- 预处理 n \sqrt {n} n 的素数来分解质因数, O ( n l n n ) O(\sqrt {\dfrac{n}{lnn}}) O(lnnn)
代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#define NDEBUG
#include<assert.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef unsigned int ui;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') {
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
void chkmin(ll &x, ll y) {if(y < x) x = y;};
void chkmax(ll &x, ll y) {if(y > x) x = y;};
#define rep(i, a, n) for(int i = (a); i <= (n); ++i)
#define per(i, a, n) for(int i = (n); i >= (a); --i)
#define sz(v) ((int)(v).size())
#define all(x) (x).begin(),(x).end()
#define lowbit(x) (x) & (-x)
#define endl '\n'
#define rd read()
#define pb push_back
#define mst(a, b) memset((a), (b), sizeof(a));
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
#define ls (u << 1)
#define rs (u << 1 | 1)
#define mod ((int)1e9+7)
#define maxn (int)(1e5+5)
int t;
int a, b, k;
int cnt, prime[6000010];
bool vis[maxn];
void sieve(int n) {
vis[1] = true;
for(int i = 2; i <= n; ++i) {
if(!vis[i]) prime[++cnt] = i;
for(int j = 1; j <= cnt && i * prime[j] <= n; ++j) {
vis[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
}
int calc(int x) {
if(x == 1) return 0;
int ret = 0;
rep(i, 1, cnt) {
if(prime[i] > sqrt(x)) break;
while(x % prime[i] == 0) ++ret, x /= prime[i];
}
if(x > 1) ++ret;
return ret;
}
int main() {
cin >> t;
sieve(100000);
while(t--) {
a = rd, b = rd, k = rd;
int g = __gcd(a, b);
int mn = 2;
if(a == g) --mn;
if(b == g) --mn;
int mx = calc(a / g) + calc(b / g) + calc(g) * 2;
if(a == b && k == 1) {
puts("No");
continue;
}
if(mn <= k && k <= mx) puts("Yes");
else puts("No");
}
return 0;
}