Codeforces Beta Round #7

A - Kalevitch and Chess

A famous Berland’s painter Kalevitch likes to shock the public. One of his last obsessions is chess. For more than a thousand years people have been playing this old game on uninteresting, monotonous boards. Kalevitch decided to put an end to this tradition and to introduce a new attitude to chessboards.

As before, the chessboard is a square-checkered board with the squares arranged in a 8 × 8 grid, each square is painted black or white. Kalevitch suggests that chessboards should be painted in the following manner: there should be chosen a horizontal or a vertical line of 8 squares (i.e. a row or a column), and painted black. Initially the whole chessboard is white, and it can be painted in the above described way one or more times. It is allowed to paint a square many times, but after the first time it does not change its colour any more and remains black. Kalevitch paints chessboards neatly, and it is impossible to judge by an individual square if it was painted with a vertical or a horizontal stroke.

Kalevitch hopes that such chessboards will gain popularity, and he will be commissioned to paint chessboards, which will help him ensure a comfortable old age. The clients will inform him what chessboard they want to have, and the painter will paint a white chessboard meeting the client’s requirements.

It goes without saying that in such business one should economize on everything — for each commission he wants to know the minimum amount of strokes that he has to paint to fulfill the client’s needs. You are asked to help Kalevitch with this task.

Input

The input file contains 8 lines, each of the lines contains 8 characters. The given matrix describes the client’s requirements, W character stands for a white square, and B character — for a square painted black.

It is guaranteed that client’s requirments can be fulfilled with a sequence of allowed strokes (vertical/column or horizontal/row).

Output

Output the only number — the minimum amount of rows and columns that Kalevitch has to paint on the white chessboard to meet the client’s requirements.

Examples

Input

WWWBWWBW
BBBBBBBB
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW

Output

3

Input

WWWWWWWW
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW

Output

1

今天做評獎的各種統計,做完統計腦子已經蒙了。一道簡單題,結果我理解錯題意了。
這道題的題意有點繞,一開始的棋盤全爲白色,輸入客戶要求的棋盤,輸出至少幾筆才能達成客戶的要求。
判斷每一行/列是否全爲B,如果全爲B,就增加一次刷的次數。
注意特判如果刷的次數是16,表示全爲B,這時只需要刷8次即可。

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

char m[10][10];

int main(void)
{
	int cnt, ans = 0;
	for (int i = 0; i < 8; i++)
		cin >> m[i];
	for (int i = 0; i < 8; i++)
    {
        cnt = 0;
        for (int j = 0; j < 8; j++)
        {
            if (m[i][j] == 'B')
                cnt++;
        }
        if (cnt == 8)
            ans++;
    }
    for (int j = 0; j < 8; j++)
    {
        cnt = 0;
        for (int i = 0; i < 8; i++)
        {
            if (m[i][j] == 'B')
                cnt++;
        }
        if (cnt == 8)
            ans++;
    }
    if (ans == 16)
        ans = 8;
    cout << ans << endl;
	return 0;
}

C - Line

A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from  - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist.

Input

The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0.

Output

If the required point exists, output its coordinates, otherwise output -1.

Examples

Input

2 5 3

Output

6 -3

擴展歐幾里得

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

typedef long long ll;

ll exgcd(ll a, ll b, ll &x, ll &y)
{
    if(b == 0)
	{
        x = 1;
        y = 0;
        return a;
    }
    ll d = exgcd(b, a % b, x, y);
    ll z = x; x = y; y = z - y * (a / b);
    return d;
}

int main(void)
{
    ll a, b, c, x, y;
    scanf("%lld%lld%lld", &a, &b, &c);
    c = -c;
    ll d = exgcd(a, b, x, y);
    if (c % d == 0) 
		printf("%lld %lld\n", x * c / d, y * c / d);
    else 
		printf("-1\n");
    
    return 0;
}

題解:https://blog.csdn.net/lihuanz/article/details/82319636

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章