zoukankan      html  css  js  c++  java
  • Codeforces 837 E Vasya's Function

    Discription

    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 a and 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


    水水数论,可以发现随着运算的过程,gcd单调不减,所以我们可以每次处理gcd相同的一段。
    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    ll num[21],N=0,mn;
    ll a,b,d,ans=0,A,B;
    
    ll gcd(ll x,ll y){
    	return y?gcd(y,x%y):x;
    }
    
    inline void dvd(ll x){
    	for(int i=2;i*(ll)i<=x;i++) if(!(x%i)){
    		num[++N]=i;
    		while(!(x%i)) x/=i;
    		if(x==1) break;
    	}
    	if(x!=1) num[++N]=x;
    }
    
    inline void solve(){
    	dvd(a);
    	
    	d=gcd(a,b);
    	while(b){
    		A=a/d,B=b/d,mn=1ll<<60;
    		if(A==1){
    			ans+=B;
    			break;
    		}
    		
    		for(int i=1;i<=N;i++) if(!(A%num[i])) mn=min(mn,B%num[i]);
    		ans+=mn,b-=mn*d,d=gcd(b,a);
    	}
    	
    	printf("%I64d
    ",ans);
    }
    
    int main(){
    	scanf("%I64d%I64d",&a,&b);
    	solve();
    	return 0;
    }
    
     
  • 相关阅读:
    Cypress自动化框架笔记
    SSH 用公钥免密登录,需要改文件权限
    python函数的可变参数 *和**
    Socket进程处理被中断的系统调用及Accept函数返回EINTR错误处理 (转)
    perror 与 strerror
    perror 函数
    strerror 函数
    getch ()函数 (来自百度百科)
    C语言运算符及其优先级汇总表口诀
    第四章
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8607727.html
Copyright © 2011-2022 走看看