自己写的正数分数类,也作为红书第一题,留念。。。
alpc 加油。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define nMax 1010
#define inf 0x7fffffff
#define LL long long
template<typename T>
T gcd(T a,T b) { return b==0 ? a : gcd(b,a%b); }
template<typename T>
T lcm(T a,T b) { return a==0 ? b : (a==0 ? b :a * b / gcd( a , b ) ); }
template<typename T>
int length(T a){
int l = 0;
while(a) { l++ ; a/=10; }
return l;
}
template<typename T>
struct Fenshu{
T s,p,q;
Fenshu() {}
Fenshu(T s,T p,T q):s(s),p(p),q(q) {}
void fresh(){
T d = gcd(p,q);
p/=d;q/=d;
if(p>=q){
s += p/q;
p %= q;
}
if(p == 0) q = 1;
return ;
}
friend Fenshu operator + ( const Fenshu& a, const Fenshu& b) {
Fenshu c;
c.s = a.s + b.s;
c.q = lcm(a.q,b.q);
c.p = c.q/a.q*a.p + c.q/b.q*b.p;
c.fresh();
return c;
}
friend Fenshu operator * ( const Fenshu& a, const T& b) {
Fenshu c;
c.s = a.s * b;
c.q = a.q;
c.p = a.p * b;
c.fresh();
return c;
}
void out(){
int ls = length(s),lp = length(p),lq = length(q);
if(q==1) { printf("%lld\n",s); return ; }
if(ls) FOR(i,0,ls) printf(" "); printf("%lld\n",p);
if(ls) printf("%lld ",s); FOR(i,1,max(lp,lq)) printf("-"); printf("\n");
if(ls) FOR(i,0,ls) printf(" "); printf("%lld\n",q);
}
};
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int n;
while(~scanf("%d",&n)){
Fenshu< LL > a(1,0,1);
FOR(i,2,n){
a = a + Fenshu< LL > (0,1,i);
}
a = a * n;
a.out();
}
return 0;
}