linux 批量查詢與替換
阿新 • • 發佈:2021-08-10
D. Another Problem About Dividing Numbers
【思路】
首先將k=1的情況特判,這種情況只有a%b=0或者b%a=0而且a!=b時才能滿足條件
然後觀察a、b兩個數都可以被除到1,將a,b兩個數進行質因數分解,分解出的質因數個數之和為sum,如果sum>=k則輸出YES,否則輸出NO
/* * @Author: Ykui.Chen * @Date: 2021-08-12 17:22:11 * @Last Modified by: Ykui.Chen * @Last Modified time: 2021-08-12 18:30:34 */ #include <bits/stdc++.h> #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); inline int read() { int X = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') X = (X << 3) + (X << 1) + ch - '0', ch = getchar(); return X * w; } #define ll long long using namespace std; const ll maxn = 1e5 + 7; bool number[maxn + 5]; ll prime[maxn + 5]; ll c = 0; void isprime() { int i, j; memset(number, true, sizeof(number)); for (i = 2; i <= maxn; i++) { if (number[i]) prime[c++] = i; for (j = 0; j < c && prime[j] * i <= maxn; j++) { number[prime[j] * i] = false; if (i % prime[j] == 0) //保證每個合數只會被它的最小質因數篩去,因此每個數只會被標記一次 break; } } } ll cal(ll x) { ll num = 0; for (ll i = 0; i < c; i++) { if (x % prime[i] == 0) { while (x % prime[i] == 0) { num++; x /= prime[i]; } } } if (x > 1) num++; return num; } int main() { IOS isprime(); ll t; cin>>t; while (t--) { ll a, b, k; cin>>a>>b>>k; ll num1 = 0, num2 = 0; num1 = cal(a); num2 = cal(b); ll sum = num1 + num2; if (k == 1) { if (a % b == 0 || b % a == 0) { if (a != b) { puts("YES"); } else puts("NO"); } else puts("NO"); } else { if (k <= sum) { puts("YES"); } else puts("NO"); } } return 0; }
這裡有一個困擾了我很久的點,就是要先用素數篩,篩出質數,在用到質因數分解中,否則會超時
F. Interesting Function
【思路】
將l到r之間的數量轉換為[0,r]-[0,l]
計算每一位的貢獻
#include <bits/stdc++.h> #define int long long using namespace std; int cal(int x) { int ans = 0; while (x) { ans += x; x /= 10; } return ans; } int main() { int t; cin >> t; while (t--) { int l, r; cin >> l >> r; int ans = cal(r) - cal(l); cout << ans << endl; } return 0; }