hdu 2276 矩阵 有点小发现,矩阵mod的使用太多易造成TLE
来源:互联网 发布:手机理财软件有哪些 编辑:程序博客网 时间:2024/04/29 07:15
Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1232 Accepted Submission(s): 645
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
1010111110100000001
Sample Output
1111000001000010
Source
HDU 8th Programming Contest Site(1)
Recommend
lcy
题意:
题目大意:有一圈的灯,其中0和1分别表示灯的暗亮,灯每秒会以一种规律改变状态,规则是
当前一盏灯亮时,就改变状态,否则就不改变状态。
思路: 很容易看出
可以用矩阵去运算,先要构造一个矩阵!
| 1 0 0 0 ....1 |
| 1 1 0 0.....0 |
| 0 1 1 0 ....0|
......
我们只要把这个矩阵^M再乘以初始状态的就可以了注意:
在代码中注意mod 的应用 如果没有用的mod用多了 就会造成超时 所以除了必须的那个mod 能省的mod还是要省去的 不然很超时
#include<stdio.h>#include<string.h>#define ll intstruct Mat{ int martix[102][102];};int N,n,d,mod=2;Mat temp,res,init,q;Mat Martix_Add(Mat a,Mat b) { int i,j; Mat c; for (i=0;i<N;i++) { for (j=0;j<N;j++) { c.martix[i][j]=(a.martix[i][j]+b.martix[i][j])%mod; } } return c; } Mat Martix_Mul(Mat a,Mat b) { int i,j,l; Mat c; for (i=0;i<N;i++) { for (j=0;j<N;j++) { c.martix[i][j]=0; for (l=0;l<N;l++) { c.martix[i][j]+=a.martix[i][l]*b.martix[l][j]; } c.martix[i][j]%=mod; } } return c; } Mat er_fun(Mat e,ll x) //求矩阵e^x { Mat tp; tp=e; e=res; //res是单位矩阵 while(x) { if(x&1) e=Martix_Mul(e,tp); tp=Martix_Mul(tp,tp); x>>=1; } return e; } int main(){ int n,g[102],i,j,k; char s[102]; memset(res.martix,0,sizeof(res.martix)); for(i=0;i<=100;i++) res.martix[i][i]=1; while(scanf("%d",&n)!=EOF) { scanf("%s",s); d=strlen(s); for(i=0;i<d;i++) g[i]=s[i]-'0'; N=d; memset(init.martix,0,sizeof(init.martix)); init.martix[0][0]=init.martix[0][d-1]=1; k=0; for(i=1;i<d;i++) { init.martix[i][k]=init.martix[i][k+1]=1; k++; } q=er_fun(init,n); for(i=0;i<d;i++) { int ans=0; for(j=0;j<d;j++) ans+=q.martix[i][j]*g[j]%mod; printf("%d",ans%2); } printf("\n"); } return 0;}
- hdu 2276 矩阵 有点小发现,矩阵mod的使用太多易造成TLE
- HDU 4920 Matrix multiplication(稀疏矩阵的乘法)不优化很容易TLE
- hdu 1506 最大矩阵面积 __int64 AC long long TLE
- 矩阵行列式mod M
- 缺陷注入-发现矩阵
- 重新发现矩阵
- 今天无意间发现的,有点小兴奋。while(next)
- 小奇的矩阵 DP
- 矩阵的基本使用
- 投影矩阵的使用
- 工作一年了,发现使用的、学习到的东西太多太多,需要整理整理了~
- hdu 2256 神奇的矩阵
- hdu 2254 矩阵的应用
- HDU 5414 矩阵的性质
- HDU 4920 Matrix multiplication(简单矩阵相乘+技巧减少Mod次数)
- HDU 2276 小小矩阵乘法
- hdu 2276(矩阵快速幂)
- hdu 2276(矩阵快速幂)
- solr管理配置,关于数据导入,集群复制及日志配置
- VC 多线程编程 blogdown整理
- 工作线程与消息循环 blogdown的整理
- vmware tools 安装详解
- datagrid使用心得(附大量代码)
- hdu 2276 矩阵 有点小发现,矩阵mod的使用太多易造成TLE
- TwoDimensionTest
- Android入门(4) 布局、JUnit、日志与数据存储访问方式
- windows7设置静态arp
- Verilog 存储器建模
- hdu 1255 覆盖的面积(扫描线)
- 由多个Object(以Teacher为例)对象所形成的数组可以使用Arrays.sort方法进行排序(编号由低到高排序)。
- windows线程同步的总结-------四种实现的方法blogdown整理
- dataset临时数据