Codeforces Beta Round #13

A - Numbers

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.

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

int a, t, sum;

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

int main(void)
{
	cin >> a;
	for (int i = 2; i <= a - 1; i++)
	{
		t = a;
		while (t)
		{
			sum += t % i;
			t /= i;
		}
	}
	int x = sum, y = a - 2;
	int gc = gcd(x, y);
	x /= gc;
	y /= gc;
	cout << x << "/" << y << endl;
	return 0;
}

C - Sequence

Little Petya likes to play very much. And most of all he likes to play the following game:

He is given a sequence of N integer numbers. At each step it is allowed to increase the value of any number by 1 or to decrease it by 1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help.

The sequence a is called non-decreasing if a1 ≤ a2 ≤ … ≤ aN holds, where N is the length of the sequence.

Input

The first line of the input contains single integer N (1 ≤ N ≤ 5000) — the length of the initial sequence. The following N lines contain one integer each — elements of the sequence. These numbers do not exceed 109 by absolute value.

Output

Output one integer — minimum number of steps required to achieve the goal.

Examples

Input

5
3 2 -1 2 11

Output

4

Input

5
2 1 1 1 1

Output

1

题解:https://www.cnblogs.com/zjbztianya/archive/2013/09/06/3305003.html

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;

ll dp[N], a[N], b[N];

int main(void)
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) 
		scanf("%lld", &a[i]), b[i] = a[i];
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            if (j == 1)
				dp[j] += abs(a[i] - b[j]);
            else
                dp[j] = min(dp[j - 1], dp[j] + abs(a[i] - b[j]));
        }
    printf("%lld\n",dp[n]);
    return 0;
}
发布了162 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43772166/article/details/102956430