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 contains one integer number A (3 ≤ A ≤ 1000).
Output should contain required average value in format «X/Y», where X is the numerator and Y is the denominator.
5
7/3
3
2/1
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.
给出一个n,计算用2~n-1进制表示n时,所有位上的数字的和。
本以为会有什么比较好的方法,后来想想,暴力吧,然后..水过了....
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int val(int x,int base)
{
int ans=0;
while(x)
{
ans+=x%base;
x/=base;
}
return ans;
}
int main()
{
int n,ans=0;
scanf("%d",&n);
for(int i=2;i<n;++i)
{
ans+=val(n,i);
}
n-=2;
int tp=__gcd(ans,n);
printf("%d/%d\n",ans/tp,n/tp);
return 0;
}