国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

玲瓏學院OJ 1091 Black and White【dp+前綴和】經典模型

2019-11-08 02:51:24
字體:
來源:轉載
供稿:網友

1091 - Black and White

Time Limit:4s Memory Limit:128MByte

Submissions:243Solved:75

DESCRipTION

Constroy likes the game called Reversi. He has a long paper tape with nn grids, where each grid should fill by one black chess or one white chess exactly. Constroydislikes the situation with aa consecutive black chesses or bb consecutive white chesses, so he intends to know how many situaions satisfy his PReference.

The answer may be so large, but you only need to give the answer modulo (109+7)(109+7).

INPUTThe first line contains a positive integerTT, which represents there are TT test cases.The following is test cases. For each test case:The only one line contains three integersa,ba,b and nn.It is guaranteed that no more than 50 test cases satisfy n≥104n≥104.1≤T≤103,1≤a,b,n≤1061≤T≤103,1≤a,b,n≤106OUTPUTFor each test case, output in one line, contains one integer, which represents the number of situations satisfy his preference modulo(109+7)(109+7).SAMPLE INPUT101 1 22 3 34 6 55 6 44 5 68 1 99 1 89 9 1016 16 161000000 1000000 1000000SAMPLE OUTPUT0429165301101865534235042057SOLUTION“玲瓏杯”ACM比賽 Round #10題目大意:

一共有N個格子,對于這N個格子來講,要么涂成顏色a,要么涂成顏色b,要求不能有連續的a個顏色a出現,也不能有連續的b個顏色b出現。

問有多少種分配方式。

思路:

1、統計計數問題,考慮dp,設定dp【i】【2】,其中:

①dp【i】【0】表示長度為i的格子,以a顏色結尾的情況數。

②dp【i】【1】表示長度為i的格子,以b顏色結尾的情況數。

2、那么不難推出其狀態轉移方程:dp【i】【0】=Σdp【i-j】【1】(0<j<a)

dp【i】【1】=Σdp【i-j】【0】  (0<j<b)

但是直接轉移時間復雜度爆炸,肯定是TLE的.那么考慮過程維護一個前綴和即可。

Ac代碼:

#include<stdio.h>#include<string.h>using namespace std;#define mod 1000000007int dp[1000050][2];int sum[1000050][2];int main(){    int t;    scanf("%d",&t);    while(t--)    {        int a,b,n;        scanf("%d%d%d",&a,&b,&n);        memset(dp,0,sizeof(dp));        memset(sum,0,sizeof(sum));        dp[0][0]=1,dp[0][1]=1;        sum[0][0]=1,sum[0][1]=1;        for(int i=1;i<=n;i++)        {            if(i<a)dp[i][0]=(dp[i][0]+sum[i-1][1])%mod;            else            {                dp[i][0]=(dp[i][0]+sum[i-1][1]-sum[i-a][1]+mod)%mod;            }            if(i<b)dp[i][1]+=sum[i-1][0];            else            {                dp[i][1]=(dp[i][1]+sum[i-1][0]-sum[i-b][0]+mod)%mod;            }            sum[i][0]=(sum[i-1][0]+dp[i][0])%mod;            sum[i][1]=(sum[i-1][1]+dp[i][1])%mod;        }        int output=(dp[n][0]+dp[n][1])%mod;        printf("%d/n",(output+mod)%mod);    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涿鹿县| 霍城县| 民县| 勃利县| 梅河口市| 天祝| 正定县| 溆浦县| 平谷区| 东山县| 石屏县| 南通市| 博野县| 南和县| 久治县| 石渠县| 清远市| 阿巴嘎旗| 隆化县| 垫江县| 临沧市| 舞钢市| 眉山市| 彭阳县| 林州市| 凤山县| 清苑县| 高雄市| 龙泉市| 普宁市| 广汉市| 资阳市| 宣威市| 滦平县| 肇庆市| 阿城市| 义乌市| 稻城县| 马尔康县| 拜泉县| 辉县市|