ICPC Latin American Regional Contests 2019 K. Know your Aliens(頂點式轉一般式)

Our world has been invaded by shape shifting aliens that kidnap people and steal their identities.You are an inspector from a task force dedicated to detect and capture them. As such, you were given special tools to detect aliens and differentiate them from real humans. Your current mission is to visit a city that is suspected of have been invaded, secretly inspect every person there so as to know whose are aliens and whose aren’t, and report it all to Headquarters. Then they can send forces to the city by surprise and capture all the aliens at once.

The aliens are aware of the work of inspectors like you, and are monitoring all radio channels to detect the transmission of such reports, in order to anticipate any retaliation. Therefore,there have been several efforts to encrypt the reports, and the most recent method uses polynomials.

The city you must visit has N citizens, each identified by a distinct even integer from 2 to 2N. You want to find a polynomial P such that, for every citizen i, P(i) > 0 if citizen i is a human, and P(i) < 0 otherwise. This polynomial will be transmitted to the Headquarters.With the aim of minimizing bandwidth, the polynomial has some additional requirements:each root and coefficient must be an integer, the coefficient of its highest degree term must be either 1 or -1, and its degree must be the lowest possible.

For each citizen, you know whether they’re a human or not. Given this information, you must find a polynomial that satisfies the described constraints.

Input

The input consists of a single line that contains a string S of length N (1 ≤ N ≤ 10^4),where N is the population of the city. For i = 1, 2, . . . , N, the i-th character of S is either the uppercase letter “H” or the uppercase letter “A”, indicating respectively that citizen 2i is a human or an alien.

Output

The first line must contain an integer D indicating the degree of a polynomial that satisfies the described constraints. The second line must contain D + 1 integers representing the coefficients of the polynomial, in decreasing order of the corresponding terms. It’s guaranteed that there exists at least one solution such that the absolute value of each coefficient is less than 2^63.

樣例輸入1複製

HHH

樣例輸出1複製

0
1

樣例輸入2複製

AHHA

樣例輸出2複製

2
-1 10 -21

樣例輸入3複製

AHHHAH

樣例輸出3複製

3
1 -23 159 -297

題意:

給一個由“H”和“A”組成的字符串,H代表人類,A代表外星人,構造一個一元 n 多次方程,使得每個字母 H 對應的下標的2倍對應的因變量 > 0,A對應的因變量 < 0

思路:

給出了若干方程的解,將其轉化爲一般式。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 1e4 + 3;

ll a[N], b[N];///b[i]: (i - 1)次方的係數

int main()
{
    string s;
    while(cin >> s)
    {
        int len = s.size();
        s = "#" + s;
        int tot = 0;
        for(int i = 1;  i < len; ++i)///根
        {
            if(s[i] != s[i + 1])
            {
                a[++tot] = 2 * i + 1;
            }
        }
        b[1] = 1;
        for(int i = 1; i <= tot; ++i)
        {
            for(int j = i + 1; j >= 1; --j)
            {
                b[j] = b[j - 1];
            }
            for(int j = 1; j <= i + 1; ++j)
            {
                b[j - 1] -= a[i] * b[j];
            }
        }
        int flag = 1;
        if(s[1] == 'H' && tot % 2)
            flag = -1;
        if(s[1] == 'A' && tot % 2 == 0)
            flag = -1;
        cout<<tot<<'\n';
        for(int i = tot + 1; i >= 1; --i)
        {
            cout<<b[i] * flag;
            if(i > 1)
                cout<<' ';
        }
        cout<<'\n';
    }
    return 0;
}
/*
HHH
AHHA
AHHHAH
*/

 

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