CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26
阿新 • • 發佈:2017-08-06
mes def using pac main code names codeforce ces
/* CodeForces - 837E - Vasya‘s Function [ 數論 ] | Educational Codeforces Round 26 題意: f(a, 0) = 0; f(a, b) = 1 + f(a, b-gcd(a, b)); 求 f(a, b) , a,b <= 1e12 分析: b 每次減 gcd(a, b) 等價於 b/gcd(a,b) 每次減 1 減到什麽時候呢,就是 b/gcd(a,b)-k 後 不與 a 互質 可先將 a 質因數分解,b能除就除,不能除就減到最近的a的因子的倍數,即模擬整個過程 由於 a 至多只有 64個因子 (a <= 2^64) ,復雜度挺低 */ #include <bits/stdc++.h> using namespace std; #define LL long long const LL INF = 1e18; const int N = 1e5; LL a, b; map<LL, int> mp; map<LL, int>::iterator it; void GetFactors(LL x) { for (LL i = 2; i*i <= x; i++) { if (x % i == 0) { while (x % i == 0) { mp[i]++; x /= i; } } } if (x != 1) mp[x] = 1; } int main() { scanf("%lld%lld", &a, &b); GetFactors(a); LL ans = 0; while (b) { for (it = mp.begin(); it != mp.end(); it++) { while ( (it->second) > 0 && b % (it->first) == 0) { b /= it->first; --(it->second); } } LL mi = INF, x = -1; for (it = mp.begin(); it != mp.end(); it++) { if ((it->second) > 0 && b % (it->first) < mi) { mi = b % (it->first); x = it->first; } } if (x == -1) { ans += b; break; } ans += mi; b -= mi; } printf("%lld\n", ans); }
CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26