NYOJ 178 law of finding a law (Laplace interpolation formula), nyojlago
Link: click here
Question:
Description
You must have seen this question: Find the rule between these numbers and write the next one that satisfies the rule.
For example: 2 5 10 17 26, we can see that these numbers conform to the n * n + 1 formula, and the next number is 37.
This generic formula has more than one, so the answer is not unique. However, if the N number is known and its formula is a polynomial with a number less than N, the answer will be unique.
Now I will give you a series. Please find out the rule and find the number of the next number?
-
Input
-
The first line is an integer T, indicating the number of groups of test data (T <= 20)
The first row of each group of test data is an integer N (1 <= N <= 5)
The next row contains N integers, indicating the N integers learned by the series (the values of these N integers are not greater than 1000 ).
-
Output
-
Output the next number that conforms to the rule
-
Sample Input
-
221 252 5 10 17 26
-
Sample output
-
337
Idea: Use of the Laplace interpolation formula .,
A Discrete Mathematical Method:
In actual problems,
By simplifying the tested functions, the approximate function P (x) of the actual function f (x) of discrete data can be constructed to calculate the function value from unknown points, it is the basic idea of interpolation.
Code:
# Include <math. h> # include <queue> # include <deque> # include <vector> # include <stack> # include <stdio. h> # include <ctype. h> # include <string. h> # include <stdlib. h ># include <iostream >#include <algorithm> using namespace std; # define Max (a, B) a> B? A: B # define Min (a, B) a> B? B: a # define mem (a, B) memset (a, B, sizeof (a) int dir [4] [2] = {1, 0 }, {-1.0}, {}, {0,-1 }}; const double eps = 1e-6; const double Pi = acos (-); static const int inf = ~ 0U> 2; static const int maxn = 110; int in [100], out [100], Map [200]; int T, I, j, n; double Laplace (double x, int n) // function definition {double xy [5] [5]; for (int I = 0; I <n; I ++) // input the interpolation point {xy [I] [0] = I + 1; cin> xy [I] [1];} double lag = 0.0; for (int I = 0; I <n; I ++) {double Tianji = 1.0; for (int j = 0; j <n; j ++) {if (I! = J) ji = ji * (x-xy [j] [0])/(xy [I] [0]-xy [j] [0]); // basic function} lag = lag + ji * xy [I] [1]; // function value} return lag;} int main () {// freopen ("Intput.txt", "r", stdin); // freopen ("Output(2).txt", "w", stdout); cin> T; while (T --) {cin> n; cout <Lago (n + 1, n) <endl;} return 0 ;}
When you want to give up, think of why you persist until now!