本文主要是介绍[CF_1282D]Guess the Root,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Guess the Root
题解
拉格朗日板题
按理说只要n+1个点就可以表示出一个n阶的多项式,对于求法很容易想到高斯消元。
但高斯消元法太麻烦了,于是,我们便开始了拉格朗日插值法。因为它更方便。
拉格朗日插值法可以通过点值求出原式,即。
将所有的点带入后就成了一个关于的多项式,从到枚举即可。
源码
一个忘了在外面预处理inv的蒟蒻
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long LL;
#define int LL
const int mo=1e6+3;
int y[15];
int qkpow(int a,int s){int t=1;while(s){if(s&1)t=t*a%mo;a=a*a%mo;s>>=1;}return t;
}
signed main(){for(int i=0;i<=10;i++){cout<<"? "<<i<<endl;cin>>y[i];if(!y[i]){cout<<"! "<<i<<endl;return 0; }for(int j=0;j<=10;j++)if(i!=j)y[i]=y[i]*qkpow(i-j,mo-2)%mo;}for(int i=11;i<mo;i++){int S=0;for(int j=0;j<=10;j++){int sum=y[j];for(int k=0;k<=10;k++){if(k==j)continue;sum=sum*(i-k)%mo%mo;}S=(S+sum)%mo;}if(!S){cout<<"! "<<i<<endl;return 0;}}cout<<"! -1"<<endl;return 0;
}
谢谢!!!
这篇关于[CF_1282D]Guess the Root的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!