atcoder-Coloring Dominoes(组合数学)

来源:互联网 发布:nginx优化 2016 编辑:程序博客网 时间:2024/06/08 07:39

Problem Statement

We have a board with a 2×N grid. Snuke covered the board with N dominoes without overlaps. Here, a domino can cover a 1×2 or 2×1 square.

Then, Snuke decided to paint these dominoes using three colors: red, cyan and green. Two dominoes that are adjacent by side should be painted by different colors. Here, it is not always necessary to use all three colors.

Find the number of such ways to paint the dominoes, modulo 1000000007.

The arrangement of the dominoes is given to you as two strings S1 and S2 in the following manner:

  • Each domino is represented by a different English letter (lowercase or uppercase).
  • The j-th character in Si represents the domino that occupies the square at the i-th row from the top and j-th column from the left.

Constraints

  • 1N52
  • |S1|=|S2|=N
  • S1 and S2 consist of lowercase and uppercase English letters.
  • S1 and S2 represent a valid arrangement of dominoes.

Input

Input is given from Standard Input in the following format:

NS1S2

Output

Print the number of such ways to paint the dominoes, modulo 1000000007.


Sample Input 1

Copy
3aabccb

Sample Output 1

Copy
6

There are six ways as shown below:


Sample Input 2

Copy
1ZZ

Sample Output 2

Copy
3

Note that it is not always necessary to use all the colors.


Sample Input 3

Copy
52RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHnRLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn

Sample Output 3

Copy
958681902
题目大意:懒得说了;
题目思路:一开始想的方向还是对的,但是有点浮躁,按道理比赛的时候应该是能想出来的,说思路:对于这道题来说,我们容易感觉到是一道排列组合问题,但是有点模糊,我一开始是想一个一个分析的,但是明显要考虑的太多,我们容易知道每一列只有两种情况,一个竖着的,两个横着放的,那我们尝试从这两个状态去分析,首先分析这两种情况有几种,容易知道第一种是有3种,第二个是有三种,接下来考虑每一种对后面的影响情况,也就是对于当前的这一种情况能产生多少种情况,首先当前为一个竖着的情况,后面如果是1个,那么就有2种,2个的就有2个,第二种情况,后面是1的只有1种情况,为2的有3种
ac代码:
#include<iostream>#include<cmath>#include<queue>#include<cstdio>#include<queue>#include<algorithm>#include<cstring>#include<string>#include<utility>#include<set>#include<map>#include<stack>#include<vector>#define inf 0x3f3f3f3f#define LL long longusing namespace std;LL mod = 1000000007;int T;int n;char s1[100];char s2[100];int cnt[100];int main(){   while(~scanf("%d",&n))   {        scanf("%s",s1);        scanf("%s",s2);        int p = 0;        for(int i = 0;i < n;i++){            if(i+1<n&&s1[i]==s1[i+1]){                cnt[p++] = 2;                i++;            }            else            {                cnt[p++] = 1;            }        }        LL sum = 0;        if(cnt[0]==1)            sum = 3;        else            sum = 6;        for(int i = 0;i<p-1;i++){            if(cnt[i]==1&&cnt[i+1]==2){                sum *= 2;                sum%=mod;            }            if(cnt[i]==1&&cnt[i+1]==1){                sum*=2;                sum%=mod;            }            if(cnt[i]==2&&cnt[i+1]==1){                sum*=1;            }            if(cnt[i]==2&&cnt[i+1]==2){                sum*=3;                sum%=mod;            }        }        cout<<sum<<endl;   }}