Then comes T lines, each line starts with an integer deg (1<=deg<=4), meaning that f(x)'s degree is deg. Then follows deg integers, representing a n to a 0 (0 < abs(a n) <= 100; abs(a i) <= 10000 when deg >= 3, otherwise abs(a i) <= 100000000, i<n). The last integer is prime pri (pri<=10000).
Remember, your task is to solve f(x) 0 (mod pri*pri)
4 2 1 1 -5 7 1 5 -2995 9929 2 1 -96255532 8930 9811 4 14 5458 7754 4946 -2210 9601
Case #1: No solution! Case #2: 599 Case #3: 96255626Case #4: No solution!
这是一个数学的思维题,算这个方程的f(x) = a nx n +...+ a 1x +a 0,找到f(X)%(m*m)=0的这个x,
这个数要从0—m*m-1中找到,如果要对m*m取余等于0一定会对m取余等0,要是对m取余等于0,那么这个数减去m对m取余还是0,
所以就先可以在(0,m-1)中对m取余等于找一找,若没有则更找不到对m*m取余等于0,如果找到了,再每回加m,计算对m*m取余,看是否能为0
#include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; ll a[10]; ll n,m,mm; long long suan(ll x) { ll ans=a[n]; for(int i=n;i>=1;i--) { ans=ans*x+a[i-1]; } return ans; } int main() { ll T; cin>>T; ll aa; ll ca=0; while(T--) { ca++; cin>>n; memset(a,0,sizeof(a)); for(ll i=n;i>=0;i--) { cin>>a[i]; } cin>>m; mm=m*m; int flag=0; for(int i=0;i<m;i++) { ll tmp=suan(i); if(tmp%m==0) { //cout<<i<<"__"<<endl; for(ll j=i;j<mm;j+=m) { ll tmp1=suan(j); if(tmp1%mm==0) {flag=1;aa=j;break;} } } if(flag==1) break; } if(flag==1) { printf("Case #%lld: %lld\n",ca,aa); } else { printf("Case #%lld: No solution!\n",ca); } } return 0; }