Educational Codeforces Round 63 (Rated for Div. 2) -E

E. Guess the Root
time limit per test:2 seconds
memory limit per test:256 megabytes
inputstandard input
outputstandard output
题目链接:http://codeforces.com/contest/1155/problem/E
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≤10ai>0) — corresponding coefficients of the polynomial.

Examples
inputCopy

1000002

0
outputCopy
? 0

? 1

! 1
inputCopy

5

2

1

outputCopy
? 2

? 1

? 0

! -1
Note
The polynomial in the first sample is 1000002+x21000002+x^21000002+x2.

The polynomial in the second sample is 1+x21+x^21+x2.

系统生成一个函数f(x)=a0+a1x+a2x2+a3x3+...+a10x10f(x)=a_0+a_1x+a_2x^2+a_3x^3+...+a_{10}x^{10}f(x)=a0+a1x+a2x2+a3x3+...+a10x10,在不多于50次询问的情况下回答一个使f(x)%(1e6+3)=0的x,或者回答-1表示这样的x不存在。
询问:输出一个数x,系统输入f(x)%(1e6+3)的值,需要使用清空缓冲区的相关函数。

线性代数应用题…可以使用矩阵的变换去解n元一次方程组。方法就是把a1−a10a_1-a_{10}a1a10当做待求量(a0询问一个0直接得到),依次询问1,2,3,…,10(其实只要是十次方不超过longlong的范围都ok,从中选取10个方程,因为有10个待求量) 然后就已知了十个方程组成的方程组,把他看成10∗1110*111011的矩阵,进行行变换变换成左侧10∗1010*101010为单位阵的情况,最右侧一列的每一项就是相应的系数,第一项就是a1,以此类推,这样就求得了函数,之后只要把1-1e6+3之间的数都用这个函数试一下就可以了。因为之前询问次数比较少,也可以在本地跑完是0的情况下提交一次验证结果。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=15;
const int mod=1e6+3;
ll qpow(ll a,ll b)
{
    ll x=a,ans=1;
    while(b)
    {
        if(b&1) ans=ans*x%mod;
        x=x*x%mod;
        b>>=1;
    }
    return ans;
}
ll qqpow(ll a,ll b)
{
    ll x=a,ans=1;
    while(b)
    {
        if(b&1) ans=ans*x;
        x=x*x;
        b>>=1;
    }
    return ans;
}
ll inv(ll x)
{
    return qpow(x,mod-2);
}
ll a[N][N];
int main()
{
    ll a0;
    printf("? 0\n");
    fflush(stdout);
    scanf("%lld",&a0);
    if(a0==0)
    {
        printf("! %d",0);
        return 0;
    }
    if(a0==-1) while(1);
    ll x=(mod-a0)%mod;
    //ll tmp;
    for(int i=0;i<10;i++)
    {
        for(int j=1;j<=10;j++)
            a[i][j]=qqpow(i+1,j);
        printf("? %d\n",i+1);
        fflush(stdout);
        scanf("%lld",&a[i][11]);
        if(a[i][11]==0)
        {
            printf("! %d",i+1);
            return 0;
        }
        if(a[i][11]==-1) while(1);
        a[i][11]=(a[i][11]-a0+mod)%mod;
    }
//    for(int i=0;i<10;i++)
//    {
//        for(int j=1;j<=11;j++)
//            printf("%11lld ",a[i][j]);
//        printf("\n");
//    }
    for(int i=1;i<10;i++)
    {
        for(int k=0;k<i;k++)
        {
            ll p=a[i][k+1];
            for(int j=k+1;j<=11;j++)
            {
                a[i][j]=((a[i][j]-p*a[k][j])%mod+mod)%mod;
            }
//
//    for(int i=0;i<10;i++)
//    {
//        for(int j=1;j<=11;j++)
//            printf("%11lld ",a[i][j]);
//        printf("\n");
//    }

        }
        ll q=a[i][i+1];
        for(int j=i+1;j<=11;j++)
        {
            a[i][j]=(a[i][j]*inv(q)+mod)%mod;
        }
    }
//
//    for(int i=0;i<10;i++)
//    {
//        for(int j=1;j<=11;j++)
//            printf("%11lld ",a[i][j]);
//        printf("\n");
//    }
    for(int i=8;i>=0;i--)
    {
        for(int k=10;k>i+1;k--)
        {
            ll p=a[i][k];
            for(int j=k;j<=11;j++)
            {
                a[i][j]=((a[i][j]-p*a[k-1][j])%mod+mod)%mod;
            }

//    for(int i=0;i<10;i++)
//    {
//        for(int j=1;j<=11;j++)
//            printf("%11lld ",a[i][j]);
//        printf("\n");
//    }
        }
        ll q=a[i][i+1];
        for(int j=1;j<=11;j++)
        {
            a[i][j]=(a[i][j]*inv(q)+mod)%mod;
        }
    }

    for(int i=0;i<mod;i++)
        if((qpow(i,1)*a[0][11]%mod+qpow(i,2)*a[1][11]%mod+qpow(i,3)*a[2][11]%mod+qpow(i,4)*a[3][11]%mod+qpow(i,5)*a[4][11]%mod+
           qpow(i,6)*a[5][11]%mod+qpow(i,7)*a[6][11]%mod+qpow(i,8)*a[7][11]%mod+qpow(i,9)*a[8][11]%mod+qpow(i,10)*a[9][11]%mod)%mod==x)
    {
        printf("? %d\n",i);
        fflush(stdout);
        int tt;
        scanf("%d",&tt);
        if(tt==0)
        {
            printf("! %d",i);
            return 0;
        }
    }
    printf("! -1");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值