Codeforces Beta Round #13

本文提供了两道算法竞赛题目“A-Numbers”和“C-Sequence”的详细解答。A-Numbers涉及在不同进制下求平均数的不可约分数表达;C-Sequence则探讨如何以最少步骤使数列非递减。代码实现采用C++,通过枚举和动态规划策略高效解决。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值