2018.4.21 T2

Physics Question
[Problem Description]
In physics class, Enos is doing an electrical experiment. Enos has a row of lamp sockets, which can connect m
small bulbs in total. He also has n kinds of small light bulbs, each of which is marked with rated power pi,ai, and different small
light bulbs different rated power. In order to make the light bulbs on the lamp socket change from dark to bright after the power is turned on, it is required that
the small light bulbs should be connected from small to large according to the rated power. In order to avoid the monotonous lighting sequence after the power is turned on, it
is stipulated that there cannot be more than ai continuous i-th small light bulbs. However, Enos found that some small bulbs
were attached to the sockets and could not be removed. Now
Enos ? Some kind of small light bulbs may not be used, but the total number of
small must be exactly m.
[Input format]
Read data from the file physics.in.
The first line contains two positive integers n and m, which respectively represent the number of small bulbs and the number of interfaces on the lamp socket.
Next n lines, each line has two positive integers pi , ai , the meanings are as described above.
The next line consists of m integers bi. If bi is 0, it means that the ith interface has no light bulb; otherwise
, it means that the ith interface has fixed a light bulb of the bi th kind.
【Output format】
Output to the file physics.out.
One integer per line represents the number of different lighting sequences. Since the answer can be quite large, you only need to
output the remainder of dividing the answer by 1 000 000 007. If there is no feasible solution, output

line −1

----------------------------------------------------------------------

Obviously it is DP, each type of bulb is obviously continuous, the number of solutions %P==0, the data is difficult to create and you are not stuck

f[i][j][k] represents the ith bit, the jth bulb, the number of solutions of length k, which is transferred in the code

#include <cstdio>
using namespace std;
const int N=105,P=1000000007;
int f[N][N][N];
int pd[N][N][N];
int p[N],a[N]={1},b[N];
int n,m,years;
int main() {
	freopen("physics.in","r",stdin);
	freopen("physics.out","w",stdout);
	scanf ("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
		scanf ("%d%d",&p[i],&a[i]);
	for (int i=1;i<=m;i++)
		scanf ("%d",&b[i]);
	f[0][0][1]=1;pd[0][0][1]=1;//pd is a judgment of -1, the transfer is the same as f, you can ignore it
	for (int i=1;i<=m;i++) {
		for (int j=1;j<=n;j++){
			if (b[i] && j!=b[i]) continue;//b[i]!=0 ,只需转移b[i]
			for (int k=0;k<=n;k++) {
				if (p[k]>p[j]) continue;
				for (int l=1;l<=a[k];l++){
					if (j==k) f[i][j][l]=(f[i][j][l]+f[i-1][k][l-1])%P;// Equal length before and after plus 1
					else f[i][j][1]=(f[i][j][1]+f[i-1][k][l])%P;//The length of 1 is transferred by all other cases
					if (j==k) pd[i][j][l]|=pd[i-1][k][l-1];
					else pd[i][j][1]|=pd[i-1][k][l];
				}
			}
		}
	}
	int che = 0;
	for (int j=1;j<=n;j++)
		for (int k=1;k<=a[j];k++)  {
			ans=(ans+f[m][j][k])%P;
			if (pd[m][j][k]) che=1;
		}
	if (!che) puts("-1");
	else printf("%d",ans);
}
	

and two-dimensional

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324608068&siteId=291194637