codeforces 837E 数论
2017-11-10 15:06
369 查看
Vasya is studying number theory. He has denoted a function f(a, b) such that:
f(a, 0) = 0;
f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of aand b.
Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating
this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly.
Input
The first line contains two integer numbers x and y (1 ≤ x, y ≤ 1012).
Output
Print f(x, y).
Example
Input
Output
Input
Output
1
题意:定义F(a,0) = 0,F(a,b) = 1 + F(a,b - GCD(a,b)。给定 x 和 y (<=1e12)求F(x,y)。
假设 a=A*gcd b=B*gcd 一轮后 A*gcd,(B-1)*gcd 当(B-k)%T == 0时 gcd变化 T为A的因子,由于因子数量很多,所以我们利用质因子来处理因子的问题,要找到最小的k,其实就是找最小的B%T ,这个想想就懂了,然后找到这个乘到gcd里面的因子,这个因子将会是
所有y%T==0 的质因子组成,把这些质因子挑出来,其实就相当于找到了这个因子,并从y中剔除。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> v;
int main()
{
ll x,y;
scanf("%lld%lld",&x,&y);
ll tt=__gcd(x,y);
x/=tt,y/=tt;
for(ll i=2;i*i<=x;i++)
{
while(x%i==0)
{
x/=i;
v.push_back(i);
}
}
if(x>1) v.push_back(x);
ll ans=0;
while(y)
{
ll hh=y;
for(int i=0;i<v.size();i++)
hh=min(hh,y%v[i]);
y-=hh;
ans+=hh;
vector<ll> v1;
for(int i=0;i<v.size();i++)
{
if(y%v[i]==0)
{
y/=v[i];
}
else v1.push_back(v[i]);
}
v.swap(v1);
}
printf("%lld\n",ans );
}
f(a, 0) = 0;
f(a, b) = 1 + f(a, b - gcd(a, b)), where gcd(a, b) is the greatest common divisor of aand b.
Vasya has two numbers x and y, and he wants to calculate f(x, y). He tried to do it by himself, but found out that calculating
this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly.
Input
The first line contains two integer numbers x and y (1 ≤ x, y ≤ 1012).
Output
Print f(x, y).
Example
Input
3 5
Output
3
Input
6 3
Output
1
题意:定义F(a,0) = 0,F(a,b) = 1 + F(a,b - GCD(a,b)。给定 x 和 y (<=1e12)求F(x,y)。
假设 a=A*gcd b=B*gcd 一轮后 A*gcd,(B-1)*gcd 当(B-k)%T == 0时 gcd变化 T为A的因子,由于因子数量很多,所以我们利用质因子来处理因子的问题,要找到最小的k,其实就是找最小的B%T ,这个想想就懂了,然后找到这个乘到gcd里面的因子,这个因子将会是
所有y%T==0 的质因子组成,把这些质因子挑出来,其实就相当于找到了这个因子,并从y中剔除。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> v;
int main()
{
ll x,y;
scanf("%lld%lld",&x,&y);
ll tt=__gcd(x,y);
x/=tt,y/=tt;
for(ll i=2;i*i<=x;i++)
{
while(x%i==0)
{
x/=i;
v.push_back(i);
}
}
if(x>1) v.push_back(x);
ll ans=0;
while(y)
{
ll hh=y;
for(int i=0;i<v.size();i++)
hh=min(hh,y%v[i]);
y-=hh;
ans+=hh;
vector<ll> v1;
for(int i=0;i<v.size();i++)
{
if(y%v[i]==0)
{
y/=v[i];
}
else v1.push_back(v[i]);
}
v.swap(v1);
}
printf("%lld\n",ans );
}
相关文章推荐
- Codeforces 396B On Sum of Fractions(数论)
- 【Codeforces711E】ZS and The Birthday Paradox [数论]
- !codeforces 399C Cards-数论&贪心-(暴力枚举)
- CodeForces 396 B.On Sum of Fractions(数论)
- CodeForces - 376C Divisible by Seven(数论:同余定理)(找规律)
- CodeForces - 359C Prime Number(数论)
- CodeForces - 58B Coins(数论)
- CodeForces 346A Alice and Bob(数论)
- CodeForces 7C Line Exgcd(数论)
- Codeforces 776C Molly's Chemicals 【数论】
- Codeforces 510D Fox And Jumping【数论+Dp+压缩空间】好题~
- 数论初步———codeforces 7C 模板题!!!!
- codeforces 510D D. Fox And Jumping(dp+数论)
- CodeForces 7C Line Exgcd(数论)
- CodeForces 743C Vladik and fractions (数论)
- codeforces 487C C. Prefix Product Sequence(数论+构造)
- CodeForces - 635C XOR Equation(搜索||数论)
- Codeforces 382B Number Busters(数论)
- 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions
- codeforces 710D Two Arithmetic Progressions [同余方程]【数论】