[CSP-S Simulation Test]: Flower (DP)

Topic Portal (internal title 111)


Input Format

  An integer $ T $, represents a set of test data.
  One row for each test, two integers, respectively, and $ L $ $ S $.


Output Format

  Each set of data and outputs an integer answer.


Sample

Sample input 1:

1
3 7

Sample output 1:

7

Sample input 2:

2
4 2
10 11

Sample Output 2:

4
410199993


Data range and tips

Sample $ 1 $ explanation:

  A total of $ 7 $ kinds of forms, each form can be constructed $ 1 $ a program.

Sample $ 2 $ explanation:

  AAA
  ABB
  Baan
  BBB

data range:

  For $ 60 \% $ data, $ L \ leqslant 30, S \ leqslant 26 $.
  For $ 80 \% $ data, $ L \ leqslant 10,000, S \ leqslant 26 $.
  For $ 100 \% $ data, $ L \ leqslant 100,000, S \ leqslant 100,000 $.


answer

Violence does not give points, causing many want to bias the live exam of $ dalao $ $ TLE0 $ ......

Consider $ DP $.

Consider first $ 80 $-point approach.

Because of the relationship in dealing with the current position of only the last two, so we can set $ dp [i] [j] [k] [0/1] $ represent processing to the $ i $, the current position is $ k $, the last is the $ j $, triple the number of whether the program has appeared.

Transfer very violent, enumerate $ j, k $, and then enumerate what the current position is just fine.

They found that only the same shape and different, and are independent of specific cases, so consider changing the definition of a state, that my approach and solution to a problem is slightly different set $ dp [i] [0/1] [0/1 ] $ represent processing to the $ i $, whether on a same, whether there have been three even can be.

Four state transition equation is given below:

  $\alpha.dp[i][0][0]=(dp[i-1][0][0]+dp[i-1][1][0])\times (S-1)$:可以由上一位二连或不是二连转移过来,只要与上一位不同即可,所以要乘上$(S-1)$;但是不能三连边没有。

  $\beta.dp[i][0][1]=(dp[i-1][0][1]+dp[i-1][1][1])\times (S-1)$:不能创造三连,只能继承上面的三连,$(S-1)$与上式同理。

  $\gamma.dp[i][1][0]=dp[i-1][0][0]$:只能由上一位没有二连转移过来,否则会出现三连,那么第三维就不能是$0$了。

  $\delta.dp[i][1][1]=dp[i-1][0][1]+dp[i-1][1][0]$:这次可以继承前面的三连,但是注意不能再出现三连;还可以创造三连,这时候需要上一位是二连。

时间复杂度:$\Theta(n)$。

期望得分:$100$分。

实际得分:$100$分。


代码时刻

#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int L,S;
long long dp[100001][2][2];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&L,&S);
		memset(dp,0,sizeof(dp));
		dp[2][0][0]=1LL*S*(S-1)%mod;
		dp[2][1][0]=S;
		for(int i=3;i<=L;i++)
		{
			dp[i][0][0]=(dp[i-1][0][0]+dp[i-1][1][0])%mod*(S-1)%mod;
			dp[i][0][1]=(dp[i-1][0][1]+dp[i-1][1][1])%mod*(S-1)%mod;
			dp[i][1][0]=dp[i-1][0][0];
			dp[i][1][1]=(dp[i-1][0][1]+dp[i-1][1][0])%mod;
		}
		printf("%lld\n",(dp[L][0][1]+dp[L][1][1])%mod);
	}
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11779145.html