Problem Description
Dr. Skywind is drawing a picture, using his favorite six colors, namely red, orange, yellow, green, blue, and violet.
The paper has N grids in a line. Each time he will fill a grid with one of the six colors. All grids needs to be filled. To make his drawing more beautiful, Skywind decided to draw symmetrically. Moreover, as he hates sorting, Skywind will never come up with the situation where all colors are in their original order. So he won't draw red-orange-yellow-green-blue-violet in a continuous way. And to make his drawing clearer, he won't paint the same color in adjacent grids.
Given N, you are asked to calculate the number of ways that Skywind can complete his drawing. As the answer might be very large, just output the number MOD 112233.
The paper has N grids in a line. Each time he will fill a grid with one of the six colors. All grids needs to be filled. To make his drawing more beautiful, Skywind decided to draw symmetrically. Moreover, as he hates sorting, Skywind will never come up with the situation where all colors are in their original order. So he won't draw red-orange-yellow-green-blue-violet in a continuous way. And to make his drawing clearer, he won't paint the same color in adjacent grids.
Given N, you are asked to calculate the number of ways that Skywind can complete his drawing. As the answer might be very large, just output the number MOD 112233.
Input
There are multiple test cases ended with an EOF. Each test case will be a line containing one positive integer N. (N <= 10^9)
Output
For each test case, output the answer MOD 112233 in a single line.
Sample Input
2 5
Sample Output
0 150
Source
Recommend
chenyongfu
不会写AC自动机过这题表示鸭梨极大。
手动构造DFA,图就不画了,我构造的有17个状态,要画死人的……
17个状态分别是:1,2,3,4,5,6,12,123,1234,12345,123456,65,654,6543,65432,654321,p
其中p表示有连续两个数一样的。
ps:后来想一下,其中三个状态是木有必要的,即终止状态:123456,654321,p
这些状态之间的转移就显而易见了。
ps:代码中那个很霸气的矩阵行顺序是按照上面列举的顺序。
之后很脑残的在草稿纸上手算转移矩阵,代入就可以了。。。
附代码,大牛勿喷……
#include <stdio.h>
typedef struct
{
__int64 m[17][17];
}Matrix;
#define MOD 112233
Matrix a,b,c;
int bz[17][17]={0,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,0,
0,0,1,1,1,1,0,1,1,1,0,1,1,0,0,0,0,
1,1,0,1,1,1,0,0,1,1,0,1,0,0,1,0,0,
1,1,1,0,1,1,1,0,0,1,0,0,0,1,1,0,0,
1,1,1,1,0,0,1,1,0,0,0,0,1,1,1,0,0,
1,1,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,6,0,0,0,0,0,0,
0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,6,0,
1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,6};
Matrix Mul(Matrix p,Matrix q,int qq)
{
int i,j,k;
for (i=0;i<17;i++)
{
for (j=0;j<qq;j++)
{
c.m[i][j]=0;
for (k=0;k<17;k++)
{
c.m[i][j]+=p.m[i][k]*q.m[k][j];
}
c.m[i][j]%=MOD;
}
}
return c;
}
int main()
{
int i,j,n;
__int64 ret;
while(scanf("%d",&n)!=EOF)
{
if (n%2==0)
{
printf("0\n");
continue;
}
if (n==1)
{
printf("6\n");
continue;
}
for (i=0;i<17;i++)
{
for (j=0;j<17;j++)
{
a.m[i][j]=bz[i][j];
}
}
for (i=0;i<17;i++)
{
if (i<6) b.m[i][0]=1;
else b.m[i][0]=0;
}
n=n/2-1;
while(n!=0)
{
if (n & 1)
{
b=Mul(a,b,1);
}
n>>=1;
a=Mul(a,a,17);
}
ret=0;
for (i=0;i<16;i++)
{
if (i<9) ret=(ret+b.m[i][0]*5)%MOD;
else if (i==9) ret=(ret+b.m[i][0]*4)%MOD;
else if (i<14 && i!=10) ret=(ret+b.m[i][0]*5)%MOD;
else if (i==14) ret=(ret+b.m[i][0]*4)%MOD;
}
printf("%I64d\n",ret);
}
return 0;
}
本文探讨了一种使用AC自动机和DFA构造来解决特定数学问题的方法,涉及状态转移矩阵构建和模运算的应用。
259

被折叠的 条评论
为什么被折叠?



