【最简分数】#13 A. Numbers

A. Numbers
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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», where X is the numerator and Y is the denominator.

Sample test(s)
input
5
output
7/3
input
3
output
2/1
Note

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.


题意是说,把一个数字A,变成二进制、三进制……一直到A-1进制,的这么多数的“数字和”的平均数,就是说所有的各位数之和除以(a-2).//因为二进制到A-1进制少了1和本身,所以是减二。

这道题的话,表达成最简分数看起来挺吓人的,实际上,分母分子同时处以最大公约数即可(最大公约数就是像这样的辗转相除法)。

C++:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

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

int val[11];//1024 in base 2

int main()
{
	int a,ans=0;
	scanf("%d",&a);
	for(int i=2;i<a;i++)
	{
		int t=a;//t=temp for a
		int pos=0;
		while(t)
    	{
        	int mod=t%i;
        	ans+=mod;
        	t=(t-mod)/i;
        	if(!t) break;
    	}
		//while(pos>=0)ans+=val[pos--];
	}
	int Valgcd=gcd(ans,a-2);
	printf("%d/%d",ans/Valgcd,(a-2)/Valgcd);
	return 0;
} 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值