HDU - 4569 Special equations(同余模定理)
生活随笔
收集整理的這篇文章主要介紹了
HDU - 4569 Special equations(同余模定理)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出多項式,現在給出多項式的每一項的系數,再給出模mod
現在問滿足f(x)%(p*p)==0的最小x是多少
題目分析:我們可以知道,滿足f(x)%(p*p)==0的x一定滿足f(x)%p==0,而且這個題目給的p比較小,我們可以先找一遍滿足后面公式的所有x,然后因為f(x)是多項式,所以根據同余模定理,我們可以知道f(x)一定滿足f(x+k*p*p)%(p*p)==f(x)%(p*p),然后暴力枚舉一下即可,因為有了第一步的篩選,所以能進入到第二步的可行解x并不多
代碼:
#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e4+100;LL a[10];int n;LL fun(LL x) {if(n==1)return a[1]*x+a[0];else if(n==2)return a[2]*x*x+a[1]*x+a[0];else if(n==3)return a[3]*x*x*x+a[2]*x*x+a[1]*x+a[0];elsereturn a[4]*x*x*x*x+a[3]*x*x*x+a[2]*x*x+a[1]*x+a[0]; }int ans[N];int main() { // freopen("input.txt","r",stdin);int w;cin>>w;int kase=0;while(w--){scanf("%d",&n);for(int i=n;i>=0;i--)scanf("%lld",a+i);int p;scanf("%d",&p);int cnt=0;for(int i=0;i<p;i++)if(fun(i)%p==0)ans[cnt++]=i;printf("Case #%d: ",++kase);if(cnt==0){printf("No solution!\n");continue;}bool flag=false;for(int i=0;i<cnt;i++)for(int j=0;j<p;j++)if(fun(ans[i]+j*p)%(p*p)==0){printf("%d\n",ans[i]+j*p);flag=true;goto end;}end:if(!flag)printf("No solution!\n");}return 0; }?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的HDU - 4569 Special equations(同余模定理)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018宁夏邀请赛 - Goldbach
- 下一篇: HDU- 2973 YAPTCHA(威尔