UVa : 10976 (Fractions Again?!)



// http://uva.onlinejudge.org/external/109/10976.html
// Runtime : 0.076s
// Tag : Math, number theory 


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#define INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL unsigned long long
using namespace std;

int Gcd (int a, int b)
{
    if ( b == 0 ) return a;
    return Gcd (b, a % b);
}

int Lcm (int a, int b)
{
    return (a * b) / Gcd (a, b);
}

int equals (int a, int b)
{
    int n = Lcm (a, b);
    int d = n / a - n / b;
    int g = Gcd (d, n);
    d /= g;
    n /= g;

    if ( d == 1 ) return n;
    return -1;
}

string stringFormat (int a, int b, int c)
{
    string ret = "1/";
    stringstream ss;
    ss << a;

    ret += ss.str ();
    ret += " = 1/";
	stringstream ss1;
    ss1 << b;
    ret += ss1.str ();
    ret += " + 1/";
    stringstream ss2;
    ss2 << c;
    ret += ss2.str ();

    return ret;
}

int main ()
{
    int k;
    string str [10000];

    while ( scanf ("%d", &k) != EOF ) {
        int cnt = 0;

        for ( int i = k + 1; i <= 2 * k; i++ ) {
            int e = equals (k, i);
            if ( e != -1 ) { str [cnt++] = stringFormat (k, e, i); }
        }

        printf ("%d\n", cnt);

        for ( int i = 0; i < cnt; i++ )
            cout << str [i] << endl;
    }

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s