CodeForces 13A Numbers

 

題目鏈接:http://codeforces.com/problemset/problem/13/A

題意:給出一個A,範圍是3-1000,求2-(A-1) 進制下 A的各位數字上的和的平均數,以(分子/分母)的最簡的形式輸出。

分析: 例如 5。

二進制 101 爲2

三進制 12 爲3

四進制 11 爲2

平均值爲7/3;

代碼:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<vector>
#include<map>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
#define Max(a,b) (a>b)?a:b
#define lowbit(x) x&(-x)

int main()
{
    int n,sum=0,tt=0,t;
    scanf("%d",&n);
    for(int i=2;i<=n-1;i++)
    {
        t=n;
        while(t)
        {
            sum+=t%i;
            t/=i;
        }
        tt+=sum/(n-2);
        sum%=(n-2);
    }
    n-=2;
    if(sum!=0)
    {
        t=__gcd(sum,n);
    sum/=t;
    n/=t;
    }
    sum+=n*tt;
    if(sum!=0)
    {
        t=__gcd(sum,n);
    sum/=t;
    n/=t;
    }
    else
        n=1;
    printf("%d/%d\n",sum,n);
}
View Code
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章