您的位置:首页 > 其它

Kiki & Little Kiki 2(找规律+矩阵快速幂的应用)

2015-08-06 00:49 357 查看
Link:http://acm.hdu.edu.cn/showproblem.php?pid=2276


Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2209 Accepted Submission(s): 1134



Problem Description

There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and the left of light k (1< k<= n) is the light k-1.At time of 0, some of them turn on, and others turn off.

Change the state of light i (if it's on, turn off it; if it is not on, turn on it) at t+1 second (t >= 0), if the left of light i is on !!! Given the initiation state, please find all lights’ state after M second. (2<= n <=
100, 1<= M<= 10^8)



Input

The input contains one or more data sets. The first line of each data set is an integer m indicate the time, the second line will be a string T, only contains '0' and '1' , and its length n will not exceed 100. It means all lights in the circle from 1 to n.

If the ith character of T is '1', it means the light i is on, otherwise the light is off.



Output

For each data set, output all lights' state at m seconds in one line. It only contains character '0' and '1.



Sample Input

1
0101111
10
100000001




Sample Output

1111000
001000010




Source

HDU 8th Programming Contest Site(1)



算法思想:

由题意,可得如下规律:

00 -----> ?0
10 -----> ?1

01 -----> ?1

11 -----> ?0

也就是说对于当前第i-1位、第i位字符,下一秒第i位字符的变化规律是:00->0 , 10->1 , 01->1 , 11->0 观察可发现下一秒第i位字符的结果是当前第i-1位、第i位字符进行异或运算后的结果。因此可以将原来长度为N的01字符串看成一个1xN的矩阵A,构造然后利用上面得出的元素之间的递推规律一个NxN的01方阵。比如N=7时,可构造矩阵B如下:

1 1 0 0 0 0 0

0 1 1 0 0 0 0

0 0 1 1 0 0 0

0 0 0 1 1 0 0

0 0 0 0 1 1 0

0 0 0 0 0 1 1

1 0 0 0 0 0 1

该矩阵构造原理如下:矩阵B中第i列中元素B[i][i]为1,其关联的是原字符串中的第i个字符,而第i列中另一个元素1关联的是原字符串中的第i-1个字符(题目中有说,第一个字符的前一个字符是第N个字符,所以第一列中另外一个元素1位于第N行,其他列中的两个1都是相邻行的)。

而异或运算如何通过矩阵乘法来实现呢?其实矩阵相乘时,就是把原来的数据与现在的数据相加了,我们只要将所得的数据模2即可。为什么这个可以替代异或呢?其实异或本来就是朴素的加法(二进制的),只不过1+1=10,我们取得是最低位罢了,所以可以直接模2就行了。

AC code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define LL long long 
#define MAXN 1000010
using namespace std;
const int INF=0x3f3f3f3f;
char s[111];
//----以下为矩阵快速幂模板-----// 
//const int mod=1000;//模3,故这里改为3即可 
int mod=2;
const int NUM=101;//定义矩阵能表示的最大维数 
int N;//N表示矩阵的维数,以下的矩阵加法、乘法、快速幂都是按N维矩阵运算的 
struct Mat{//矩阵的类
	int a[NUM][NUM];
	Mat(){memset(a,0,sizeof(a));}  
	void init()//将其初始化为单位矩阵  
	{
		memset(a,0,sizeof(a));
		for(int i=0;i<NUM;i++)
		{
			a[i][i]=1;
		}
	}
};
Mat A,B,mi,ans;
Mat add(Mat a,Mat b)//(a+b)%mod  矩阵加法  
{
	Mat ans;
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<N;j++)
		{
			ans.a[i][j]=(a.a[i][j]%mod)+(b.a[i][j]%mod);
			ans.a[i][j]%=mod;
		}
	}
	return ans;
}
Mat mul(Mat a,Mat b) //(a*b)%mod  矩阵乘法  
{
	Mat ans;
	for(int i=1;i<=N;i++)
	{
		for(int j=1;j<=N;j++)
		{
			ans.a[i][j]=0;
			for(int k=1;k<=N;k++)
			{
				ans.a[i][j]=(ans.a[i][j]+a.a[i][k]*b.a[k][j])%2;
			}
			//ans.a[i][j]%=mod;
		}
	}
	return ans;
}
Mat power(Mat a,int num)//(a^n)%mod  矩阵快速幂 
{
	Mat ans;
	ans.init();
	while(num)
	{
		if(num&1)
		{
			ans=mul(ans,a);
		}
		num>>=1;
		a=mul(a,a);
	}
	return ans;
}
Mat pow_sum(Mat a,int num)//(a+a^2+a^3....+a^n)%mod 矩阵的幂和
{
	int m;
	Mat ans,pre;
	if(num==1)
		return a;
	m=num/2;
	pre=pow_sum(a,m);
	ans=add(pre,mul(pre,power(a,m)));
	if(num&1)
		ans=add(ans,power(a,num));
	return ans;
}
void output(Mat a)//输出矩阵 
{
	for(int i=1;i<=N;i++)
	{
		for(int j=1;j<=N;j++)
		{
			printf("%d%c",a.a[i][j],j==N-1?'\n':' ');
		}
	}
}
//----以上为矩阵快速幂模板-----// 
int main()
{
	//freopen("D:\in.txt","r",stdin);
	int n,m,i,j;
	while(scanf("%d",&m)!=EOF)
	{
		scanf("%s",s);
		n=strlen(s);
		N=n;
		memset(A.a,0,sizeof(A.a));
		memset(B.a,0,sizeof(B.a));
		for(i=1;i<=N;i++)
		{
			A.a[1][i]=s[i-1]-'0';
		}
		for(i=1;i<=N-1;i++)
		{
			B.a[i][i]=B.a[i][i+1]=1;
		}
		B.a
[1]=B.a

=1;
		B=power(B,m);
		for(int i=1;i<=1;i++)
		{
			for(int j=1;j<=N;j++)
			{
				ans.a[i][j]=0;
				for(int k=1;k<=N;k++)
				{
					ans.a[i][j]=(ans.a[i][j]+A.a[i][k]*B.a[k][j])%2;
				}
				//ans.a[i][j]%=mod;
			}
		}
		for(i=1;i<=N;i++)
			printf("%d",ans.a[1][i]);
		printf("\n");
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: