国产探花免费观看_亚洲丰满少妇自慰呻吟_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);    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桐城市| 胶州市| 华阴市| 东乡| 隆回县| 绥化市| 永定县| 开封县| 长春市| 乐东| 瓮安县| 珠海市| 兰溪市| 中西区| 泸溪县| 萨迦县| 确山县| 抚顺市| 桐城市| 西吉县| 武夷山市| 广州市| 息烽县| 垣曲县| 萝北县| 南溪县| 赣州市| 岳西县| 南京市| 遂昌县| 湘潭市| 海南省| 翁源县| 延吉市| 磐安县| 镇坪县| 西充县| 南和县| 宁化县| 云南省| 永登县|