HDU - 2276 Kiki & Little Kiki 2 (矩阵快速幂)
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)
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.
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
思路:
想到矩阵快速幂是关键.
其实这个公式我推的时候也没有想到,但实在是不应该.
s[i]=(s[i]+s[i-1])%2;
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #define fuck(x) cout<<#x<<" = "<<x<<endl; #define debug(a, x) cout<<#a<<"["<<x<<"] = "<<a[x]<<endl; #define ls (t<<1) #define rs ((t<<1)|1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 108; const int maxm = 100086; const int inf = 0x3f3f3f3f; const ll Inf = 999999999999999999; const int mod = 2; const double eps = 1e-6; const double pi = acos(-1); struct Matrix{ int mp[108][108]; }; Matrix mul(Matrix a,Matrix b,int n){ Matrix ans; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ ans.mp[i][j]=0; for(int k=1;k<=n;k++){ ans.mp[i][j]+=a.mp[i][k]*b.mp[k][j]; } ans.mp[i][j]%=mod; } } return ans; } Matrix q_pow(Matrix a,int b,int n){ Matrix ans; memset(ans.mp,0,sizeof(ans.mp)); for(int i=1;i<=n;i++){ ans.mp[i][i]=1; } while (b){ if(b&1){ ans=mul(ans,a,n); } b>>=1; a=mul(a,a,n); } return ans; } int num[maxn]; char s[maxn]; int main() { int n,m; while (scanf("%d",&m)!=EOF){ scanf("%s",s+1); n=strlen(s+1); for(int i=1;i<=n;i++){ num[i]=s[i]-'0'; } Matrix tmp; memset(tmp.mp,0,sizeof(tmp.mp)); tmp.mp[1][1]=tmp.mp[1][n]=1; for(int i=2;i<=n;i++){ tmp.mp[i][i]=tmp.mp[i][i-1]=1; } tmp=q_pow(tmp,m,n); for(int i=1;i<=n;i++){ int ans=0; for(int j=1;j<=n;j++){ ans+=tmp.mp[i][j]*num[j]; ans%=mod; } printf("%d",ans); } printf("\n"); } return 0; }
如需转载,请注明出处
如有侵权,联系删除
2290713181@qq.com
如有侵权,联系删除
2290713181@qq.com