13A - Numbers
2016-07-21 20:36
417 查看
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.
Examples
input
output
input
output
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.
给出一个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;
}
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.
Examples
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.
给出一个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;
}
相关文章推荐
- LESS CSS使用方法 , CSS也能跟JS一样玩。
- Hibernate中,One2One的基于注解Annotation的实现
- 接口
- Restaurant
- [shell基础]——uniq命令
- MySQL函数大全【转载】
- NKOI 1905 慢跑小路
- jQuery 3.0 一个小问题的解决办法
- 【CodeForces】597B - Restaurant(贪心)
- button控制视图的隐藏与显示
- 顺序表java实现
- linux tail 命令动态显示日志信息
- 代码审阅软件Understand
- UVA - 11044 Searching for Nessy
- 树莓派声控LED灯
- android:拍照,相册剪裁的实现方式
- 大数相加NYOJ103
- Android EditText 设置键盘为搜索,回车或是下一项
- LeetCode Basic Calculator(用栈计算表达式的值)
- string 的函数