1. 程式人生 > >SPOJ - POLYNOM Polynomial(數論亂搞)題解

SPOJ - POLYNOM Polynomial(數論亂搞)題解

ble color flag 裏的 false str return include mod

題意 :給你n個數,問你是否存在一個多項式(最多三次方)滿足f(i)= xi。

思路:講一個神奇的思路:

x3 - (x - 1)3 = 3x2 - 3x + 1

x2 - (x - 1)2 = 2x + 1

x - (x - 1) = 1

1 - 1 = 0

看了上面這麽多,其實已經可以發現一件事情了:如果相鄰常數減一次那麽就是0;相鄰一次式減一次降為常數,減兩次為0;相鄰二次式減一次降為一次式,減兩次降為常數,減三次....

由此我們可以知道,如果存在一個多項式(最多三次方)滿足f(i)= xi,那麽我相鄰的數相減4次必為0,如果不滿足,那肯定不是同個式子裏的

代碼:

#include<cstdio>
#include
<set> #include<vector> #include<cmath> #include<queue> #include<cstring> #include<algorithm> typedef long long ll; using namespace std; const int maxn = 500 + 5; const double INF = 0x3f3f3f3f; const ll MOD = 1000000007; ll num[maxn]; int main(){ int T; scanf("%d", &T);
while(T--){ ll n; scanf("%lld", &n); for(int i = 1; i <= n; i++) scanf("%lld", &num[i]); for(int i = 1; i <= 4; i++){ for(int j = n ;j > i; j--) num[j] = num[j] - num[j - 1]; } bool flag = true;
for(int i = 5; i <= n; i++){ if(num[i]){ flag = false; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; } /* Input: 3 1 3 5 0 1 2 3 4 5 0 1 2 4 5 Output: YES YES NO */

SPOJ - POLYNOM Polynomial(數論亂搞)題解