• BZOJ2187:fraction


    Sol

    分情况讨论

    1. (lfloorfrac{a}{b} floor+1le lceilfrac{c}{d} ceil-1)
      直接取 (q=1,p=lfloorfrac{a}{b} floor+1)
    2. (a=0)
      那么 (q> frac{pd}{c})
      直接取 (p=1,q=lfloorfrac{d}{c} floor+1)
    3. (a<b)(cle d)
      那么递归处理 (frac{d}{c} < frac{q}{p} < frac{b}{a})
    4. (age b)
      那么递归处理 (frac{a~mod~b}{b} < frac{p}{q}-lfloorfrac{a}{b} floor < frac{c}{d}-lfloorfrac{a}{b} floor)

    形式上类似于欧几里得算法,把 ((a,b)) 转化成 ((a~mod~b,b)) 可能就是是类欧几里得算法的精髓

    # include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    inline ll Gcd(ll x, ll y) {
    	if (!x || !y) return x | y;
    	return !y ? x : Gcd(y, x % y);
    }
    
    inline void Solve(ll a, ll b, ll c, ll d, ll &x, ll &y) {
    	register ll g = Gcd(a, b), nx, ny;
    	a /= g, b /= g, g = Gcd(c, d), c /= g, d /= g;
    	nx = a / b + 1, ny = c / d + (c % d > 0) - 1;
    	if (nx <= ny) x = nx, y = 1;
    	else if (!a) x = 1, y = d / c + 1;
    	else if (a < b && c <= d) Solve(d, c, b, a, y, x);
    	else Solve(a % b, b, c - d * (a / b), d, x, y), x += y * (a / b);
    }
    
    ll a, b, c, d, x, y;
    
    int main() {
    	while (scanf("%lld%lld%lld%lld", &a, &b, &c, &d) != EOF) Solve(a, b, c, d, x, y), printf("%lld/%lld
    ", x, y);
        return 0;
    }
    
    
  • 相关阅读:
    [Sql Server][原创]
    SQL Server T-SQL高级查询
    SQL 网文链接
    Epicor系统二次开发
    lambda表达式的变量作用域
    写一个正则表达式匹配手机号
    函数装饰器在类方法中的使用方法
    关于Django的session的使用 (装饰器版)
    Django ORM相关操作(2)
    Django ORM相关操作(1)
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/10092042.html
Copyright © 2020-2023  润新知