codeforce343A

题目地址:http://codeforces.com/problemset/problem/343/A

比赛的时候就囧了,只推出a<b的时候最少需要b个电阻。

后来看了题解,知道

题意:用最少的1Ω电阻拼出指定阻值(a/b)电阻.元件之间可以以串联或并联的方式连接.

思路: 显然电阻越并越小,a/b的整数部分可以串联若干1Ω电阻解决.此时,有这样一条重要结论:如果最少用K个电阻构成a/bΩ电阻,那么b/a也需K个(只需改变所有的串并联关系即可).所以此时若b>a,只需交换a,b的值,重复上一步骤.

是我物理知识太弱了吗?

cf用__int64。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;

int main()
{
    __int64 a, b;
    while(scanf("%I64d%I64d",&a, &b) != EOF)
    {
        __int64 ans = 0;
        while(true)
        {
            if(a/b >= 1)
            {
                ans += a/b;
                a -= b*(a/b);
                if(a%b == 0)
                   break;
            }
            else
            {
                swap(a, b);
            }
        }
        printf("%I64d
",ans);
    }
    return 0;
}


 

原文地址:https://www.cnblogs.com/james1207/p/3329085.html