1. 程式人生 > >Codeforces Educational Round 1 (ABCDE)

Codeforces Educational Round 1 (ABCDE)

比賽連結:http://codeforces.com/contest/598

A. Tricky Sum

time limit per test:1 second

memory limit per test:256 megabytes

In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum.

For example, for n = 4 the sum is equal to  - 1 - 2 + 3 - 4 =  - 4, because 1, 2 and 4 are 20, 21 and 22 respectively.

Calculate the answer for t values of n.

Input

The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of values of n to be processed.

Each of next t lines contains a single integer n (1 ≤ n ≤ 109).

Output

Print the requested sum for each of t

 integers n given in the input.

Examples

input

2
4
1000000000

output

-4
499999998352516354

Note

The answer for the first sample is explained in the statement.

題目大意:求1到n的和,數字為2的次冪減,其餘加

題目分析:分別求和然後減一下即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define ll long long
using namespace std;

int main() {
    int t;
    ll n;
    cin >> t;
    while (t--) {
        cin >> n;
        ll sum = (n & 1) ? (n + 1) / 2 * n : n / 2 * (n + 1);
        ll temp = 0;
        while (n) {
            n >>= 1;
            temp++;
        }
        temp--;
        ll sum2 = 2;
        while (temp) {
            sum2 <<= 1;
            temp--;
        }
        sum2 -= 1;
        cout << sum - (sum2 << 1) << endl;
    }
}

 

B. Queries on a String

time limit per test:2 seconds

memory limit per test:256 megabytes

You are given a string s and should process m queries. Each query is described by two 1-based indices liri and integer ki. It means that you should cyclically shift the substring s[li... riki times. The queries should be processed one after another in the order they are given.

One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.

For example, if the string s is abacaba and the query is l1 = 3, r1 = 6, k1 = 1 then the answer is abbacaa. If after that we would process the query l2 = 1, r2 = 4, k2 = 2 then we would get the string baabcaa.

Input

The first line of the input contains the string s (1 ≤ |s| ≤ 10 000) in its initial state, where |s| stands for the length of s. It contains only lowercase English letters.

Second line contains a single integer m (1 ≤ m ≤ 300) — the number of queries.

The i-th of the next m lines contains three integers liri and ki (1 ≤ li ≤ ri ≤ |s|, 1 ≤ ki ≤ 1 000 000) — the description of the i-th query.

Output

Print the resulting string s after processing all m queries.

Examples

input

abacaba
2
3 6 1
1 4 2

output

baabcaa

Note

The sample is described in problem statement.

題目大意:一個字串,n個操作,每次將l到r位置旋轉k個單位長度,求操作後的字串

題目分析:串長很短,旋轉操作具有周期性,因此k對子串長取模即可,旋轉可以通過經典的三段逆序方式來實現

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 10005;
char s[MAX];

void reverse(char* s, int l, int r) {
    while (l < r) {
        swap(s[l++], s[r--]);
    }
}

int main() {
    scanf("%s", s);
    int m, l, r, k;
    scanf("%d", &m);
    while (m--) {
        scanf("%d %d %d", &l, &r, &k);
        l--;
        r--;
        k %= (r - l + 1);
        reverse(s, r - k + 1, r);
        reverse(s, l, r - k);
        reverse(s, l, r);
    }
    printf("%s\n", s);
}

 

C. Nearest vectors

time limit per test:2 seconds

memory limit per test:256 megabytes

You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.

Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and π. For example, opposite directions vectors have angle equals to π.

Input

First line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of vectors.

The i-th of the following n lines contains two integers xi and yi (|x|, |y| ≤ 10 000, x2 + y2 > 0) — the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).

Output

Print two integer numbers a and b (a ≠ b) — a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.

Examples

input

4
-1 0
0 -1
1 0
1 1

output

3 4

input

6
-1 0
0 -1
1 0
1 1
-4 -5
-4 -6

output

6 5

題目大意:平面上給n個向量(用點的座標表示),求向量夾角最小的點的編號

題目分析:這個題除了精度毒瘤外就沒什麼了,極角排序後暴力即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long double const PI = acos(-1.0);
int const MAX = 100005;
pair <long double, int> a[MAX];
int n, x, y;

int main () {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &x, &y);
        a[i] = make_pair(atan2(y, x), i);
    }
    sort(a, a + n);
    long double mi = 10000.0, tmp;
    int ans1 = 0, ans2 = 0;
    for (int i = 0; i < n; i++) {
        tmp = a[(i + 1) % n].first - a[i].first;
        if (tmp < 0) {
            tmp += 2.0 * PI;
        }
        if (tmp < mi) {
            mi = tmp;
            ans1 = a[i].second + 1;
            ans2 = a[(i + 1) % n].second + 1;
        }
    }
    printf("%d %d\n", ans1, ans2);
}

 

D. Igor In the Museum

time limit per test:1 second

memory limit per test:256 megabytes

Igor is in the museum and he wants to see as many pictures as possible.

Museum can be represented as a rectangular field of n × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.

At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.

For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.

Input

First line of the input contains three integers nm and k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process.

Each of the next n lines contains m symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum.

Each of the last k lines contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.

Output

Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.

Examples

input

5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3

output

6
4
10

input

4 4 1
****
*..*
*.**
****
3 2

output

8

題目大意:輸入一個n*m的字元矩陣,k組詢問,每組詢問是一個點,求與該點所在的聯通塊直接相鄰的'*'字元的數量

題目分析:DFS即可,DFS時對每個聯通塊標號

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int const MAX = 1005;
int n, m, k;
char s[MAX][MAX];
bool vis[MAX][MAX];
int mp[MAX][MAX];
vector<int> ans;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int DFS(int id, int x, int y) {
    if (s[x][y] == '*') {
        return 1;
    }
    vis[x][y] = true;
    mp[x][y] = id;
    int num = 0;
    for (int i = 0; i < 4; i++) {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (!vis[xx][yy] && xx > 0 && yy > 0 && xx <= n && yy <= m) {
            num += DFS(id, xx, yy);
        }
    }
    return num;
}

int main() {
    scanf("%d %d %d", &n, &m, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%s", s[i] + 1);
    }
    int id = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (s[i][j] == '.' && !vis[i][j]) {
                int res = DFS(id, i, j);
                ans.push_back(res);
                id++;
            }
        }
    }
    int x, y;
    while (k--) {
        scanf("%d %d", &x, &y);
        printf("%d\n", ans[mp[x][y]]);
    }
}

 

E. Chocolate Bar

time limit per test:2 seconds

memory limit per test:256 megabytes

You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar.

In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.

For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 32 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 22 = 4).

For several given values nm and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - ksquares are not necessarily form a single rectangular piece.

Input

The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values nm and k to process.

Each of the next t lines contains three integers nm and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively.

Output

For each nm and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly ksquares.

Examples

input

4
2 2 1
2 2 3
2 2 2
2 2 4

output

5
5
4
0

Note

In the first query of the sample one needs to perform two breaks:

  • to split 2 × 2 bar into two pieces of 2 × 1 (cost is 22 = 4),
  • to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 12 = 1).

In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.

題目大意:n*m的巧克力塊,求在吃k塊的情況下,最小切割代價,每次切割的代價為切割長度的平方

題目分析:資料範圍很小,隨便dp即可,dp[i][j][k]表示高為i,長為j的巧克力塊吃k塊的最小代價,暴力列舉橫切和豎切的情況,以橫切為例,假設在高為h的地方切一刀,會把巧克力分為h*j和(i-h)*j兩塊,假設當前列舉的切割後的上半部分的塊數為up,下半部分則為k-up,易得轉移方程dp[i][j][k] = min(dp[i][j][k], dp[h][j][up] + dp[i - h][j][k - up] + j * j,若i*j<=50,如果要吃大小為i*j的巧克力塊的話是不需要額外切割的,比如樣例的第四個,因此初始化時將i*j<=50的dp[i][j][i*j]設為0

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const INF = 1e9;

int dp[35][35][55];

void pre() {
    for (int i = 0; i <= 30; i++) {
        for (int j = 0; j <= 30; j++) {
            for (int k = 0; k <= 50; k++) {
                dp[i][j][k] = INF;
            }
        }
    }
    for (int i = 0; i <= 30; i++) {
        for (int j = 0; j <= 30; j++) {
            dp[i][j][0] = 0;
            if (i * j <= 50) {
                dp[i][j][i * j] = 0;
            }
        }
    }
    for (int i = 1; i <= 30; i++) {
        for (int j = 1; j <= 30; j++) {
            for (int k = 1; k <= min(i * j, 50); k++) {
                for (int h = 1; h <= (i >> 1); h++) {
                    for (int up = 0; up <= k; up++) {
                        dp[i][j][k] = min(dp[i][j][k], dp[h][j][up] + dp[i - h][j][k - up] + j * j);
                    }
                }
                for (int v = 1; v <= (j >> 1); v++) {
                    for (int left = 0; left <= k; left++) {
                        dp[i][j][k] = min(dp[i][j][k], dp[i][v][left] + dp[i][j - v][k - left] + i * i);
                    }
                }
            }
        }
    }
}

int main() {
    pre();
    int t, n, m, k;
    scanf("%d", &t);
    while (t--) {
        scanf("%d %d %d", &n, &m, &k);
        printf("%d\n", dp[n][m][k]);
    }
}