CF1155E-Guess the Root(高斯消元)

题目链接:https://codeforces.com/contest/1155/problem/E

E. Guess the Root

Description

Jury picked a polynomial f(x)=a0+a1⋅x+a2⋅x2+⋯+ak⋅xk. k≤10 and all ai are integer numbers and 0≤ai<106+3. It’s guaranteed that there is at least one i such that ai>0.

Now jury wants you to find such an integer x0 that f(x0)≡0mod(106+3) or report that there is not such x0.

You can ask no more than 50 queries: you ask value xq and jury tells you value f(xq)mod(106+3).

Note that printing the answer doesn’t count as a query.

Interaction

To ask a question, print “? xq” (0≤xq<106+3). The judge will respond with a single integer f(xq)mod(106+3). If you ever get a result of −1 (because you printed an invalid query), exit immediately to avoid getting other verdicts.

After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

fflush(stdout) or cout.flush() in C++;
System.out.flush() in Java;
flush(output) in Pascal;
stdout.flush() in Python;
see documentation for other languages.
When you are ready to answer, print “! x0” where x0 is the answer or −1 if there is no such x0.

You can ask at most 50 questions per test case.

Hack Format

To hack, use the following format.

The only line should contain 11 integers a0,a1,…,a10 (0≤ai<106+3, max0≤i≤10 ai>0) — corresponding coefficients of the polynomial.

Examples

input

1000002

0

output

? 0

? 1

! 1

input

5

2

1

output

? 2

? 1

? 0

! -1

Note

The polynomial in the first sample is 1000002+x2.
The polynomial in the second sample is 1+x2.

题意:

每次给出x的值,询问多项式 f(x)=a0+a1⋅x+a2⋅x2+⋯+ak⋅xk的模106+3意义下的结果,最多询问50次,问是否存在x,且0<=x<106+3,使得f(x)≡0 (mod 106+3).

解法:高斯消元

任意取11个不同的数然后询问11次,然后构建相应的增广矩阵,然后用高斯消元求出a0,a1,a2…a10,然后枚举0~106+2,代入多项式看是否存在满足条件的值,复杂度o(k*(1e6+3));
(如果还没接触过高斯消元,推荐一篇博客吧:传送门

附代码:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod=1e6+3;
ll a[15][15],x[15],ans[15];
int n=11,m=11;

ll quick_pow(ll a,ll b,ll c)
{
	ll res=1ll;
	while(b){
		if(b&1) res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res;
}

ll ask(ll que)
{
	ll res=0;
	printf("? %I64d\n",que);
	fflush(stdout);
	scanf("%I64d",&res);
	return res;
}

ll gcd(ll a,ll b)
{
	return b==0ll?a:gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
	return a/gcd(a,b)*b;
}

ll inv(ll a,ll b)
{
	return quick_pow(a,b-2,b);
}

int gauss()
{
	int row=1,col=1;
	for(;row<=n&&col<=m;row++,col++){
		int cur=row;
		for(int i=row+1;i<=n;i++){
			if(abs(a[i][col])>abs(a[cur][col])){
				cur=i;
			}
		}
		if(cur!=row){
			for(int i=col;i<=m+1;i++) swap(a[cur][i],a[row][i]);
		}

		if(a[row][col]==0){
			row--;
			continue;
		}

		for(int i=row+1;i<=n;i++){
			if(a[i][col]!=0){
				ll LCM=lcm(a[i][col],a[row][col]);
				ll tmpa=LCM/a[i][col];
				ll tmpb=LCM/a[row][col];

				for(int j=col;j<=m+1;j++) a[i][j]=(a[i][j]*tmpa-a[row][j]*tmpb)%mod;
			}
		}
	}

	for(int i=row;i<=n;i++) if(a[i][col]!=0) return -1;
	if(row<=m) return m-row+1;
	for(int i=n;i>=1;i--){
		ll tmp=a[i][m+1];
		for(int j=i+1;j<=m;j++) tmp=(tmp-x[j]*a[i][j])%mod;
		x[i]=tmp*inv(a[i][i],mod)%mod;
	}
	return 0;
}

int main()
{
	for(int i=1;i<=11;i++){
		for(int j=1;j<=11;j++) a[i][j]=quick_pow(i,j-1,mod);
		a[i][12]=ask((ll)i); 
	}
	int get=gauss();
	//for(int i=1;i<=11;i++) printf("%I64d\n",x[i]);
	if(get==-1) return printf("! -1\n"),0;
	else{
		for(ll i=0;i<mod;i++){
			for(int j=0;j<=10;j++) ans[j]=quick_pow(i,(ll)j,mod);
			ll res=0;
			for(int j=0;j<=10;j++){
				res+=ans[j]*x[j+1];
				res%=mod;
			}
			if(res==0) return printf("! %I64d\n",i),0;
		}
	}
	printf("! -1\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值