hdu 5068 Harry And Math Teacher(BestCoder Round #14)
来源:互联网 发布:淘宝有多少分可以扣 编辑:程序博客网 时间:2024/06/08 09:04
Harry And Math Teacher
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 76 Accepted Submission(s): 17
Problem Description
As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm.
In Hogwarts, there is a tall castle in which all teachers live. The castle is rather special. In every floor there are two doors, behind each of them there existing two stairs to the next floor’s two doors. And if you are at the i-th floor’s j-th door , then you can just go to the next floor from this door. However, something even more interesting (or we can say "magic") can happen to the stairs: sometimes they break into pieces (making it impossible to go to the next floor), and sometimes the fragments can joint together and become the whole stair again. Now suppose Harry is in the a-th floor (you know, Harry is the hero, so he lives in the teachers’ building somehow), and his math teacher b-th floor. Sometimes the math teacher will call Harry to his room. Facing these magic stairs, Harry gets puzzled how he can go to see the math teacher. Can you help Harry figure out how many ways exactly he can choose without going backwards? You can assume that the change of the stairs will not happen when Harry is on his way. Harry can begin at any doors in floor a and he can end at any doors in floor b. And as Harry want to arrive as soon as possible, so he can not go back to the past. And at the beginning all the stairs are intact. And the answer may be too large, you should output the answer mod 1000000007.
In Hogwarts, there is a tall castle in which all teachers live. The castle is rather special. In every floor there are two doors, behind each of them there existing two stairs to the next floor’s two doors. And if you are at the i-th floor’s j-th door , then you can just go to the next floor from this door. However, something even more interesting (or we can say "magic") can happen to the stairs: sometimes they break into pieces (making it impossible to go to the next floor), and sometimes the fragments can joint together and become the whole stair again. Now suppose Harry is in the a-th floor (you know, Harry is the hero, so he lives in the teachers’ building somehow), and his math teacher b-th floor. Sometimes the math teacher will call Harry to his room. Facing these magic stairs, Harry gets puzzled how he can go to see the math teacher. Can you help Harry figure out how many ways exactly he can choose without going backwards? You can assume that the change of the stairs will not happen when Harry is on his way. Harry can begin at any doors in floor a and he can end at any doors in floor b. And as Harry want to arrive as soon as possible, so he can not go back to the past. And at the beginning all the stairs are intact. And the answer may be too large, you should output the answer mod 1000000007.
Input
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n andm(2≤n≤50000,1≤m≤50000) in the first line, indicate the number of the castle’s layers and the number of queries. And the following m lines, each line contains three or four integers. If the first integer op equals 0, there are two integers a and b (1≤a<b≤n ) follow, indicate the position of Harry and math teacher. Otherwise, there are three integersx,y,z(1≤x<n,1≤y,z≤2) follow, it means that the stair between the xth floor’s yth door and the (x+1)th floor’s z-th door changes its state(if it is intact, then it breaks else it joints).
For each test case, there are two integers n and
Output
For each query, if op equals 0, you should output one line that contains an integer indicates the number of ways fromath floor to the bth floor.
Sample Input
3 10 1 33 21 2 1 10 1 3
Sample Output
86
Source
BestCoder Round #14
题意:N层楼梯,每层有2个门,上下两层门四条路互通。M种操作,op0: a b 询问从a楼到b楼有多少种走法。 op2:x y z x楼的y门和(x+1)楼的z门之间的楼梯状态改变(可走 /不可走)。
分析:
我们可以把第i层跟第i+1层之间楼梯的通断性构造成一个2*2的通断性矩阵,1表示通,0表示不通。那么从第a层到第b层,就是将a到b-1的通断性矩阵连乘起来,然后将得到的答案矩阵上的每个元素加起来即为方案数。想到矩阵的乘法是满足结合律的,那么我们可以用线段树来维护矩阵的乘积。每次我们只会修改某一个楼梯的通断性,所以就只是简单的线段树单点更新,成段求乘积而已。整体复杂度2∗2∗2∗nlogn
#include <cmath>#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>using namespace std;typedef long long LL;const LL MOD = 1000000007LL;#define N 50100LL a[N<<2][2][2];void mul(LL x[2][2], LL y[2][2]){ LL z[2][2] = {0,0,0,0}; for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) z[i][j] = (z[i][j]+x[i][k]*y[k][j])%MOD; memcpy(x,z,sizeof(z));}void build(int l, int r, int rt){ if(l==r) { a[rt][0][0] = a[rt][0][1] = a[rt][1][0] = a[rt][1][1] = 1; return ; } int m = l+r>>1; build(l,m,rt<<1); build(m+1,r,rt<<1|1); for(int i=0;i<2;i++) for(int j = 0;j<2;j++) a[rt][i][j] = a[rt<<1][i][j]; mul(a[rt],a[rt<<1|1]);}void query(LL s[2][2], int L, int R, int l, int r, int rt){ if(L<=l && R>=r) { mul(s,a[rt]); return; } int m = l+r >>1; if(L<=m) query(s,L,R,l,m,rt<<1); if(R>m) query(s,L,R,m+1,r,rt<<1|1);}void update(int floor, int x, int y, int l, int r, int rt){ if(l==r) { a[rt][x][y]^=1; return; } int m = l + r >>1; if(floor<=m) update(floor,x,y,l,m,rt<<1); else update(floor,x,y,m+1,r,rt<<1|1); for(int i=0;i<2;i++) for(int j = 0;j<2;j++) a[rt][i][j] = a[rt<<1][i][j]; mul(a[rt],a[rt<<1|1]);}int main(){ int i,j,k,m,n; while(scanf("%d %d",&n,&m)==2) { int op,l,r,x,y,z; build(1,n,1); while(m--) { scanf("%d",&op); if(op==0) { scanf("%d%d",&l,&r); l++; LL s[2][2] = { 1,0,0,1}; query(s,l,r,1,n,1); LL ans = (s[0][0]+s[0][1]+s[1][0]+s[1][1])%MOD; printf("%I64d\n",ans); } else { scanf("%d%d%d",&x,&y,&z); update(x+1,--y,--z,1,n,1); } } } return 0;}
0 0
- hdu 5068 Harry And Math Teacher(BestCoder Round #14)
- BestCoder Round #14 03 Harry And Math Teacher (线段树)
- hdu 5066 Harry And Physical Teacher(Bestcoder Round #14)
- HDU 5068 Harry And Math Teacher
- hdu-5068 Harry And Math Teacher
- Bestcoder Round 14# 1001 Harry And Physical Teacher
- BestCoder Round #14 A题 Harry And Physical Teacher
- HDU 5069 Harry And Biological Teacher AC自动机 + 线段树优化 (BestCoder Round 14 D)
- HDU 5066 Harry And Physical Teacher(物理 动量守恒 动能守恒)——BestCoder Round #14
- HDU 5068 Harry And Math Teacher (矩阵处理,线段树)
- hdu 5068 Harry And Math Teacher 线段树
- HDU-5068-Harry And Math Teacher(线段树)
- 【HDU5068】Harry And Math Teacher
- hdu 5067 Harry And Dig Machine(BestCoder Round #14)
- HDU 5068 Harry And Math Teacher 线段树维护矩阵乘积
- hdu 5068 Harry And Math Teacher ( 线段树+矩阵乘法 )
- 【线段树】 HDOJ 5068 Harry And Math Teacher
- [BestCoder] Round #25 1001 - Harry and Magical Computer | HDU 5154
- 题目1108:堆栈的使用 题目28
- QCamera的使用方法,包含截图并且保存图片等简单功能
- /usr/bin/ld: error: cannot find -lGL
- UVA - 10245 The Closest Pair Problem
- XML
- hdu 5068 Harry And Math Teacher(BestCoder Round #14)
- SPOJ_AMR10I_Dividing Stones(DFS)
- visual Assist X 免费下载网址
- 飞鸽传书源码分析二消息机制
- codecomb 2098【stone】
- C++ const
- Ubuntu13.04(64bit)下用Wine安装百度云、360云、微云
- 对 熊大快跑Ver2.0.4 和支付的分析
- PAT(A) 1019