日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NYOJ 300 hdu 2276 Kiki Little Kiki 2 (矩阵快速幂)

發布時間:2025/3/16 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NYOJ 300 hdu 2276 Kiki Little Kiki 2 (矩阵快速幂) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Kiki & Little Kiki 2

時間限制:5000?ms ?|? 內存限制:65535?KB 難度:4 描述
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)
輸入
The input contains no more than 1000 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.
輸出
For each data set, output all lights' state at m seconds in one line. It only contains character '0' and '1.
樣例輸入
1 0101111 10 100000001
樣例輸出
1111000 001000010

題意:給出一些燈的初始狀態(用0、1表示),對這些燈進行m次變換;若當前燈的前一盞燈的狀態為1,則調整當前燈的狀態,0變為1,1變為0;否則不變。第1盞燈的前一盞燈是最后一盞燈。問最后每盞燈的狀態。

分析:通過模擬可以發現,假設有n盞燈,第i盞燈的狀態為f[i],則f[i] = (f[i] + f[i-1])%2;又因為這些燈形成了環,則f[i] = (f[i] + f[(n+i-2)%n+1])%2. 這樣初始狀態形成一個1*n的矩陣,然后構造出如下n*n的矩陣:

1 ?1 ?0…… 0 ? 0

0 ?1 ?1…… 0 ? 0

……………………

1 ?0 ?0…… 0 ? 1

每次乘以這個矩陣得出的結果就是下一個狀態。

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 105; char state[N]; struct Matrix {Matrix() {}void Init(int n) {row = n; col = n;memset(mat, 0, sizeof(mat));for(int i = 0; i < n; i++)mat[i][i] = 1;}void Init(int m, int n) {row = m; col = n;memset(mat, 0, sizeof(mat));}int row, col;int mat[N][N];const Matrix &Pow(int n); };const Matrix &operator *(const Matrix &A, const Matrix &B) {Matrix res;res.Init(A.row, B.col);for(int i = 0; i < A.row; i++) {for(int j = 0; j < A.col; j++) {if(A.mat[i][j]) {for(int k = 0; k < B.col; k++) {if(B.mat[j][k])res.mat[i][k] = res.mat[i][k] ^ (A.mat[i][j] & B.mat[j][k]);}}}}return res; }const Matrix &Matrix::Pow(int n) {Matrix tmp, pre;tmp = *this;pre.Init(this->row);while(n) {if(n&1) pre = tmp * pre;tmp = tmp * tmp;n >>= 1;}return pre; }int main() {int m;Matrix A, B, ans;while(~scanf("%d", &m)) {scanf("%s",state);int len = strlen(state);A.Init(1, len);for(int i = 0; i < len; i++)A.mat[0][i] = state[i] - '0';B.Init(len);for(int i = 0; i < len; i++) {B.mat[i][i] = 1;B.mat[i][(i+1)%len] = 1;}ans = A * B.Pow(m);for(int i = 0; i < len; i++)printf("%d", ans.mat[0][i]);printf("\n");}return 0; }


總結

以上是生活随笔為你收集整理的NYOJ 300 hdu 2276 Kiki Little Kiki 2 (矩阵快速幂)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。