P4933 master problem solution

CSDN synchronization

Original title link

Briefly meaning of the questions:

How many arithmetic sequence has a number of columns in demand. (Sequence is not necessarily continuous , substring must continuously )

Note: The tolerance can be negative.

A algorithm

For \ (30 \% \) data, \ (n-\ Leq 20 is \) .

Obviously, enumerated sequences, and then verify the violence.

Time complexity: \ (O (n-2 ^ \ n-Times) \) .

Actual score: \ (30pts \) .

Algorithms two

For \ (60 \% \) data, \ (n-\ Leq 100 \) , \ (V \ Leq 2 \ ^ 10. 3 Times \) .

Before enumeration arithmetic sequence \ (2 \) key, and then calculate the tolerance, the next enumeration can be.

Time complexity: \ (O (n-^. 3) \) .

Actual Score: \ (60pts \) ~ \ (100 pts \) (constant depends on the application).

Algorithm for Three

For additional \ (20 \% \) data, all of the electrical configuration of a height of the column arithmetic sequence.

Obviously, in this case the answer is equivalent to \ (. 1 \) ~ \ (n-\) fetch the arithmetic sequence.

why? In this case, as long as the arithmetic sequence takes \ (X, X + Y, X + Y \ X + KY cdots \) , then the original number of columns corresponding to the number of listed:

\[a_x , a_{x+y} \cdots a_{x+ky} \]

Number of columns is necessarily arithmetic, and each column corresponds to a number of such arithmetic sequence .

Then, you use the above (60 \% \) \ violence optimize it, enumerate any two points can form arithmetic sequences, can be calculated.

Of course, there is a special case: i.e. \ (a_i = a_j (i \ not = j) \) , the arbitrary sequences may form the answer, the answer is \ (2-n-^. 1 \) .

Plus front \ (60 \% \) violence:

Time complexity: \ (O (n-^. 3) \) .

Actual score: \ (80pts \) ~ \ (100 pts \) (program still depends on constant).

Optimal Four

Consider \ (\ DP {text} \) .

With \ (f_ {i, j} \) represents, to \ (I \) at the end of tolerance \ (J \) Arithmetical number of columns.

Of course, we do not have to enumerate \ (J \) , we enumerate \ (k <i \) one \ (k \) , and let \ (j = a_i - a_k \ ) transferred.

So (without considering the negative subscript problem) state transition equation is:

\[f_{i,a_i - a_k} = f_{k,a_i - a_k} + 1 \]

Clearly, with a front of the same tolerances number transferred columns.

Finally, remember to length \ (1 \) , \ (2 \) of the arithmetic sequence plus.

Handle negative subscript, we all indices (second dimension) plus \ (N \) , you can open a large array.

Time complexity: \ (O (^ n-2) \) .

Actual score: \ (100 pts \) .

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;

const int MOD=998244353;
const int N=1e3+1;
const int M=4e4+1;

inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
	int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}

int n,a[N],ans=0;
int f[N][M];

int main(){
	n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<=n;i++) {
		ans=(ans+i)%MOD; //长度为 1 , 2 的数列
		for(int j=i-1,t;j>=1;j--) {
			t=a[i]-a[j]; //公差
			ans=(ans+f[j][t+N])%MOD; //记录答案
			f[i][t+N]=(f[i][t+N]+f[j][t+N]+1)%MOD; //转移
		}
	} printf("%d\n",ans);
	return 0;
}

Guess you like

Origin www.cnblogs.com/bifanwen/p/12644203.html