codeforces 837E
来源:互联网 发布:ubuntu传文件 编辑:程序博客网 时间:2024/06/07 08:12
题解
首先我们知道
令
则
设gcd(a, b) = d
则
若a为质数, b < a
那么我们可以递归求解
a不为质数且a,b互质时我们找最接近b且和a不互质的数字就可
每次递归会约去一个质因子,1e12最多十几次…
code:
#include <bits/stdc++.h>using namespace std;typedef long long ll;namespace IO{ inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} return x*f; }}namespace Number_Theory{ ll fac[15]; int len; bool isPrime[1000010]; ll p[1000001 / 5]; int k; void init() { memset(isPrime, true, sizeof isPrime); for(int i = 2; i < 1000001; ++i) { if(isPrime[i])p[++k] = i; for(int j = 1; j <= k && i * p[j] < 1000001; ++j) { isPrime[i * p[j]] = 0; if(i % p[j] == 0) break; } } } void div(ll n) { memset(fac, 0, sizeof fac); len = 0; for(int i = 1; p[i] * p[i] <= n && i <= k; ++i) { if(n % p[i] == 0) { fac[++len] = p[i]; while(n % p[i] == 0) n /= p[i]; } } if(n > 1) fac[++len] = n; } ll Get(ll x) { ll pos = 0; int id = -1; for(int i = 1; i <= len; ++i) { ll tmp = x / fac[i] * fac[i]; if(tmp >= pos) { pos = tmp; id = i; } } return pos; } ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);} bool judge(ll x) { if(x < 1000001) return isPrime[x]; for(int i = 1; i <= k && p[i] * p[i] <= x; ++i) { if(x % p[i] == 0) return false; } return true; } ll solve(ll x, ll y) { if(judge(x)) return y; // ll d; div(x); ll pos = Get(y); ll d = gcd(x, pos); return y - pos + solve(x / d, pos / d); }}int main(){ Number_Theory::init(); ll x, y; x = IO::read(), y = IO::read(); ll ans = 0; ans += y / x; y %= x; Number_Theory::div(x); printf("%I64d\n", ans + (y == 0 ? 0 : Number_Theory::solve(x, y))); return 0;}
阅读全文
0 0
- codeforces 837E
- codeforces 837E 数论
- codeforces 163E e-Government
- 【Codeforces 163E】E-Government
- Codeforces 78E Evacuation
- 【dp】codeforces 83E
- Codeforces 124 E
- Codeforces 231E
- Codeforces 231E - Cactus
- Codeforces #163 Div2 E
- codeForces 35E
- Codeforces 35E
- Playlist codeforces 268E
- Codeforces 148E(Porcelain)
- codeforces round#177 E
- codeforces 203E Transportation
- CodeForces 254 E Dormitory
- codeforces日记371e
- C# 能否获取一个对象所占内存的大小?
- Spring的简介
- Windows系统使用WMIC的获取硬盘ID的问题
- [LeetCode] 560. Subarray Sum Equals K
- Spring mail
- codeforces 837E
- java后台中的框架概念之间的关系
- 排序算法之快速排序
- js实现鼠标悬停文字上显示图片
- hdfs的shell操作
- LintCode:字符串置换211
- RC522读M1卡原理图及调试
- jQuery 中lable radio select 的一些操作
- Date,DateTime,String等日期格式的相互转化