11-08-UPC训练赛 Know your Aliens

Know your Aliens

Our world has been invaded by shapeshifting 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.

输入
The input consists of a single line that contains a string S of length N (1 ≤ N ≤ 104),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.
输出
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 263.

样例输入
【样例1】
HHH
【样例2】
AHHA
【样例3】
AHHHAH
样例输出
【样例1】
0
1
【样例2】
2
-1 10 -21
【样例3】
3
1 -23 159 -297

题意:偶数位置上有外星人和人类,如果第i个位置为H说明第2i个位置为人类,A说明第2i个位置为外星人,构造一个函数p(i)使得在i位置如果是人类则p(i)>0,如果是外星人则p(i)<0,保证最高项系数为1或者-1,按顺序输出系数。

假设第2i个位置为人类,2i+2位置为外星人,则p(2*i)*p(2i+2)<0,说明p(x)在2i和2i+2之间存在一个零点,很明显取2i+1为零点,因此对于每一个外星人和人类之间都会存在一个零点因此构造零点式方程(x-x1)(x-x2)…(x-xn),将零点式展开,首项系数为正好为1。最后分析有奇数个零点和偶数个零点两种情况(可以以二次函数和三次函数为例),确定系数的正负即可.

ps 已知零点求系数的方式

ll a[maxn];//零点
ll b[maxn];//系数
void getpos(int cnt)//cnt为零点个数
{
    b[1]=1;
    for(int i=1;i<=cnt;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];
    }
} 

附上代码

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
#define ll long long
char s[maxn];
ll a[maxn];
ll b[maxn];
void getpos(int cnt)
{
    b[1]=1;
    for(int i=1;i<=cnt;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 main()
{
    scanf("%s",s);
    ll cnt=0;
    int len=strlen(s);
    for(int i=0;i<len-1;i++)
    {
        if(s[i]!=s[i+1])
            a[++cnt]=2*(i+1)+1;//零点 
    }  
    printf("%lld\n",cnt);
    getpos(cnt);
    int flag=0;
    if((s[0]=='H'&&cnt%2==1)||(s[0]=='A'&&cnt%2==0))
        flag=1;
 
    for(int i=cnt+1;i>=1;i--)
    {
        if(flag==1) cout<<-1*b[i]<<' ';
        else
        cout<<b[i]<<' ';
        if(i==1)cout<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值