Codeforces Round #354 (Div. 2) E - The Last Fight Between Human and AI

转自别人的题解:(x-k)|P(x)等价于P(k)==0

分两种情况考虑

如果k=0的话,谁先使得a[0]=0就好了

如果k!=0的话,注意到每一步都可以任意改变P(k)的值,因此只有最后一步是有用的,如果所有数都已经确定,那么检查P(k)是否为0,否则胜负取决于最后一步操作的先手。


#include<bits\stdc++.h>
using namespace std;
const int maxn = 1e5+6;
#define LL long long
#define INF 0x3f3f3f3f
int a[maxn],cnt,n,k;
string s;
int main()
{
	scanf("%d%d",&n,&k);
	n++;
	for(int i = 0;i<n;i++)
	{
		cin >> s;
		if (s[0]=='?')
			a[i]=INF;
		else
		{
			int p = 0;
			if (s[0]=='-')
				p=1;
			for(int j = p;j<s.size();j++)
				a[i] = a[i]*10+(int)(s[j]-'0');
			if(s[0]=='-')
				a[i]=a[i]*(-1);
			cnt++;
		}
	}
	if(k==0)
	{
		if(a[0]==INF)
		{
			if(cnt %2 == 1)
				puts("Yes");
			else
				puts("No");
		}
		else if (a[0])
			puts("No");
		else
			puts("Yes");
	}
	else
	{
		if(cnt < n)
		{
			if(n%2==1)
				puts("No");
			else
				puts("Yes");
		}
		else
		{
		    LL now = 0;
		    for(int i = n-1;i>=0&&abs(now)<1e14;i--)
	 	    {
               now = 1LL *now*k+a[i];
		    }
		    printf("%s\n",(now?"No":"Yes"));
		}
	}
}


E. The Last Fight Between Human and AI
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

100 years have passed since the last victory of the man versus computer in Go. Technologies made a huge step forward and robots conquered the Earth! It's time for the final fight between human and robot that will decide the faith of the planet.

The following game was chosen for the fights: initially there is a polynomial

P(x) = anxn + an - 1xn - 1 + ... + a1x + a0, 
with yet undefined coefficients and the integer k. Players alternate their turns. At each turn, a player pick some index j, such that coefficient aj that stay near xj is not determined yet and sets it to any value (integer or real, positive or negative, 0 is also allowed). Computer moves first. The human will be declared the winner if and only if the resulting polynomial will be divisible by Q(x) = x - k.

Polynomial P(x) is said to be divisible by polynomial Q(x) if there exists a representation P(x) = B(x)Q(x), where B(x) is also some polynomial.

Some moves have been made already and now you wonder, is it true that human can guarantee the victory if he plays optimally?

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, |k| ≤ 10 000) — the size of the polynomial and the integer k.

The i-th of the following n + 1 lines contain character '?' if the coefficient near xi - 1 is yet undefined or the integer value ai, if the coefficient is already known ( - 10 000 ≤ ai ≤ 10 000). Each of integers ai (and even an) may be equal to 0.

Please note, that it's not guaranteed that you are given the position of the game where it's computer's turn to move.

Output

Print "Yes" (without quotes) if the human has winning strategy, or "No" (without quotes) otherwise.

Examples
input
1 2
-1
?
output
Yes
input
2 100
-10000
0
1
output
Yes
input
4 5
?
1
?
1
?
output
No
Note

In the first sample, computer set a0 to  - 1 on the first move, so if human can set coefficient a1 to 0.5 and win.

In the second sample, all coefficients are already set and the resulting polynomial is divisible by x - 100, so the human has won.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值