ラベル 最大公約数 の投稿を表示しています。 すべての投稿を表示
ラベル 最大公約数 の投稿を表示しています。 すべての投稿を表示

2012年7月6日金曜日

AOJ Volume2 0238: Time To Study

/********************************************
*    2012/07/03                                *
*    AOJ Volume2 0238                        *
*    Time To Study                            *
*    crane                                              *
*********************************************/
#include<iostream>
using namespace std;

int main(){
    int t;
    while(cin >> t, t){//目標時間
        int n;
        cin >> n;        //勉強回数

        int sum = 0;
        int st, et;        //開始時間, 終了時間
        for(int i=0; i<n; i++){
            cin >> st >> et;
            sum += et - st;
        }
       
        if( t <= sum)    cout << "OK" << endl;
        else{
            cout << t - sum << endl;
        }
    }
    return 0;
}

2012年7月3日火曜日

AOJ Volume0 0005 GCD and LCM

最大公約数はユークリッドの互除法で、
最小公倍数は  xy/gcd(x,y)で。

/***************************
*    2012/05/11           
*    AOJ Volume0 0005     
*    GCD and LCM          
*    crane                
****************************/

#include<iostream>
using namespace std;

int gcd(int x, int y);    //最大公約数 ユークリッドの互除法
int lcm(int x, int y, int gcd);    //最小公倍数  lcm(x,y)=xy/gcd(x,y)

int main(){
    int x, y;
    int g_res;
    long l_res;
    while(cin >> x >> y){
        g_res = gcd(x,y);
        l_res = lcm(x,y,g_res);
        cout << g_res << " " << l_res << "\n";
    }
    return 0;
}


int gcd(int x, int y){
    if(y==0)  return x;
    else      return gcd(y, x%y);
}

int lcm(int x, int y, int gcd){
    return x/gcd*y;