给定一个01**字符串环**(2<=字符串长度<=100)然后进行m次的变换。
定义变换的规则为:如果当前位置i的左边是1(下标为0的左边是n-1),那么i就要改变状态0->1 , 1->0
比如当前的状态为100101那么一秒过后的状态为010111。
用公式表示变化状态其实就是:
换成位操作就是:
于是我们可以建立一个变化矩阵:
/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;
const int maxn = 105;
const int mod = 2;
lint n;
struct Matrix{
int n,m;
int a[maxn][maxn];
Matrix(int n , int m){
this->n = n;
this->m = m;
cls(a);
}
Matrix operator * (const Matrix &tmp){
Matrix res(n,tmp.m);
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j < tmp.m ; j++)
for(int k = 0 ; k < m ; k++)
res.a[i][j] = res.a[i][j] ^ (a[i][k] & tmp.a[k][j]);
return res;
}
};
void Matrix_print(Matrix x){
for(int i = 0 ; i < x.n ; i++){
for(int j = 0 ; j < x.m ; j++) cout << x.a[i][j] << ‘ ‘;
cout << endl;
}
cout << endl;
}
Matrix fast_pow(Matrix x , lint n){
Matrix res(x.n , x.m);
for(int i = 0 ; i < x.n ; i++) res.a[i][i] = 1;
while(n){
if(n&1)
res = res*x;
x = x*x;
n >>= 1;
}
return res;
}
void solve(string s , lint n){
int l = s.length();
Matrix fun(l,l);
Matrix base(l,1);
for(int i = 0 ; i < l - 1; i++) fun.a[i][i] = fun.a[i][i+1] = 1;
fun.a[l-1][0] = fun.a[l-1][l-1] = 1;
//Matrix_print(fun);
for(int i = 0 ; i < l; i++) base.a[i][0] = s[i] - ‘0‘;
fun = fast_pow(fun , n);
base = fun*base;
for(int i = 0 ; i < l ; i++) cout << base.a[(i - n%l + l)%l][0];
puts("");
}
int main(){
//freopen("input.txt","r",stdin);
string s;
lint n ;
while(cin >> n >> s){
solve(s,n);
}
return 0;
}
类似上一题,
n个小朋友围成一个环(2<=n<=100)然后进行m次的游戏。
一开始,第i个小朋友有
定义游戏的规则为:每一次游戏处于i位置的小朋友获得
求m次游戏后每个小朋友的苹果数量。
凑样例愣是凑了半小时不明所以然,所幸搜了题解后被告知题目有误。
变化矩阵如下:
/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;
const int maxn = 100 + 5;
lint n1 , m1 , L , R , M;
lint A[maxn];
struct Matrix{
int n , m ;
lint a[maxn][maxn];
Matrix( int n , int m ){
this->n = n ;
this->m = m ;
cls(a);
}
Matrix operator * ( const Matrix &tmp ){
Matrix res( n , tmp.m );
for( int i = 0 ; i < tmp.m ; i++ )
for( int j = 0 ; j < m ; j++ )
res.a[0][i] = ( res.a[0][i] + ( a[0][j] * tmp.a[j][i] ) % M ) % M;
for( int i = 1 ; i < n ; i++ )
for( int j = 0 ; j < m ; j++ )
res.a[i][j] = res.a[i - 1][( j - 1 + n ) % n];
return res;
}
};
void Matrix_input( Matrix x ){
for( int i = 0 ; i < x.n ; i++ ){
for( int j = 0 ; j < x.m ; j++ ){
cout << x.a[i][j] << ‘ ‘;
}
puts("");
}
puts("");
}
Matrix fast_pow( Matrix x , lint n ){
Matrix res( x.n , x.m );
for( int i = 0 ; i < x.n ; i++ ) res.a[i][i] = 1;
while( n ){
if( n & 1 )
res = res * x;
x = x * x ;
n >>= 1;
}
return res;
}
void input(){
cin >> n1 >> m1 >> L >> R >> M;
for( int i = 0 ; i < n1 ; i++ ){
scanf( "%I64d" , &A[i] );
}
}
void solve(){
if( m1 == 0 ) {
for( int i = 0 ; i < n1 ; i++ ){
if( i ) printf( " " );
printf( "%I64d" , A[i] % M);
}
puts("");
return;
}
Matrix fun( n1 , n1 );
for( int i = 0 ; i < n1 ; i++ ){
fun.a[i][( i + 1 ) % n1] = L;
fun.a[i][( i - 1 + n1 ) % n1] = R;
fun.a[i][i] = 1;
}
//Matrix_input( fun );
fun = fast_pow( fun , m1 );
//Matrix_input( fun );
for( int i = 0 ; i < n1 ; i++ ){
lint sum = 0 ;
for( int j = 0 ; j < n1 ; j++ )
sum = ( sum + fun.a[i][j] * A[j] % M ) % M;
if( i ) printf( " " );
printf( "%I64d" , sum );
}
puts("");
}
int main(){
//freopen("input.txt","r",stdin);
int t ; cin >> t;
while( t-- ){
input();
solve();
}
return 0;
}
版权声明:博主表示授权一切转载:)
HDU 2276 & FZU 1692 (循环同构优化+矩阵快速幂)
原文地址:http://blog.csdn.net/qq_15714857/article/details/47708157