codefores 13A(进制数和最大公约数)

C - Numbers
Crawling in process... Crawling failed Time Limit:1000MSMemory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status

Description

Little Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18.

Now he wonders what is an average value of sum of digits of the number A written in all bases from 2 to A - 1.

Note that all computations should be done in base 10. You should find the result as an irreducible fraction, written in base 10.

Input

Input contains one integer number A (3 ≤ A ≤ 1000).

Output

Output should contain required average value in format «X/Y», whereX is the numerator and Y is the denominator.

Sample Input

Input
5
Output
7/3
Input
3
Output
2/1

Sample Output

Hint

In the first sample number 5 written in all bases from 2 to 4 looks so: 101, 12, 11. Sums of digits are 2, 3 and 2, respectively.

 

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int GCD(int m,int n){  
	if(m>n) swap(m,n);  
	int r=n,a,b;  
	while(m>0){  
		r=n%m;  
		n=m;  
		m=r;  
	}  
	return n;  
}  
int count( int x,int jinzhi){
	int ret=0;
	while(x>0){
		ret+=x%jinzhi;
		x/=jinzhi;
	}
	return ret;
}
int main(){
	int n;
	while(scanf("%d",&n)!=EOF){		
		int sum=0;
		for(int i=2;i<n;i++)
			sum+=count(n,i);
		int p=sum/GCD(sum,n-2);
		int q=(n-2)/GCD(sum,n-2);
		printf("%d/%d\n",p,q);
	}
	return 0;	
}


 

你可能感兴趣的:(codefores 13A(进制数和最大公约数))