题目描述

Farmer John's N cows (1≤N≤100) are standing in a line. The ith cow from the left has label i, for each 1≤i≤N.
Farmer John has come up with a new morning exercise routine for the cows. He tells them to repeat the following two-step process exactly K (1≤K≤109) times:
1.The sequence of cows currently in positions A1…A2 from the left reverse their order (1≤A1<A2≤N).
2.Then, the sequence of cows currently in positions B1…B2 from the left reverse their order (1≤B1<B2≤N).
After the cows have repeated this process exactly K times, please output the label of the ith cow from the left for each 1≤i≤N.

输入

The first line of input contains N and K. The second line contains A1 and A2, and the third contains B1 and B2.

输出

On the ith line of output, print the label of the ith cow from the left at the end of the exercise routine.

样例输入

7 2
2 5
3 7

样例输出

1
2
4
3
5
7
6

提示

Initially, the order of the cows is [1,2,3,4,5,6,7] from left to right. After the first step of the process, the order is [1,5,4,3,2,6,7]. After the second step of the process, the order is [1,5,7,6,2,3,4]. Repeating both steps a second time yields the output of the sample.


题目大意:给出一个1~n的数列,初始时为1,2,3...n-1,n,接下来进行 k 次操作,每次操作有两个步骤:

  1. 将[ l , r ]内的数字翻转
  2. 将[ ll , rr ]内的数字翻转

问操作 k 次后的数列变成了什么样子

题目分析:因为 k 高达1e9,所以暴力翻转肯定是不行的,所以自然而然想到了快速幂,因为 n 只有100,而且是对于数列进行的操作,所以不难想到矩阵快速幂,具体如下:

不难看出,只需要将单位矩阵按照特殊的区间限制稍微变换一下就好了,每次与初始矩阵相乘后,得到的结果都是相应区间转换完成后的结果,而题目中要求的是每次翻转两个区间,两个区间带来的影响是会有重叠部分不好处理,但用矩阵来处理的话,只需要将两个区间的转移矩阵单独预处理好后,然后相乘一下就好了

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=100;const int mat_size = 100;//矩阵大小,需要乘以2,为了&运算的时候需要二倍的矩阵大小struct Matrix
{unsigned long long a[mat_size][mat_size];int x, y;//长宽Matrix() //返回0矩阵{memset(a,0,sizeof(a));}Matrix(int x,int y)//返回0矩阵,并且x,y赋值{this->x = x;this->y = y;memset(a, 0,sizeof(a));}Matrix(int n)  //返回n*n的【单位矩阵】{this->x=n;this->y=n;memset(a,0,sizeof(a));for (int i = 0; i <n;++i)    a[i][i]=1;}Matrix operator * (const Matrix &B)//矩阵乘法{Matrix tmp;for (int i = 0; i < x; ++ i)for (int j = 0; j < B.y; ++ j){tmp.a[i][j] = 0;for (int k = 0; k < y; ++ k){tmp.a[i][j] = (tmp.a[i][j] + a[i][k] * B.a[k][j]);}}tmp.x = x;tmp.y=B.y;return tmp;}Matrix operator ^ (int b)//矩阵A的b次方{Matrix ret = Matrix(x);  Matrix A = *this;while( b )  {  if( b & 1 )   ret = ret * A ;  b >>= 1 ;  A = A * A ;  }  return ret ;  }Matrix operator & (int b)//A^0 + A^1+A^2+A^3+++A^n,其中A是矩阵。最后返回的就是一个矩阵{Matrix ret = *this;for (int i = ret.x; i < ret.x * 2; ++ i)   {ret.a[i-ret.x][i]= 1;ret.a[i][i] = 1;}ret.x <<= 1;ret.y <<= 1;//pg(ret);ret = ret^b;ret.x >>= 1;ret.y >>= 1;for (int i = 0; i < ret.x; ++ i)  for (int j = 0; j < ret.y; ++ j)ret.a[i][j] += ret.a[i][j + ret.x];return ret;}void pg(Matrix A){for (int i = 0; i <A.x; ++i){for (int j = 0; j < A.y;++j) cout<<A.a[i][j]<<" ";cout<<endl;}cout<<endl;}
};int main()
{
#ifndef ONLINE_JUDGE
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);int n,k;scanf("%d%d",&n,&k);int l,r,ll,rr;scanf("%d%d%d%d",&l,&r,&ll,&rr);l--,r--,ll--,rr--;Matrix a(n,n),b(n,n);for(int i=0;i<n;i++){a.a[i][i]=1;b.a[i][i]=1;}for(int i=l;i<=r;i++)a.a[i][i]=0;for(int i=l;i<=r;i++)a.a[i][r-(i-l)]=1;for(int i=ll;i<=rr;i++)b.a[i][i]=0;for(int i=ll;i<=rr;i++)b.a[i][rr-(i-ll)]=1;Matrix temp=a*b;Matrix ans(n,n);for(int i=0;i<n;i++)ans.a[0][i]=i+1;ans=ans*(temp^k);for(int i=0;i<n;i++)printf("%d\n",ans.a[0][i]);return 0;
}

中石油训练赛 - Swapity Swap(矩阵快速幂)相关推荐

  1. 中石油训练赛 - Switches(高斯消元求逆矩阵+逆矩阵求线性方程组)

    题目大意:给出一个 n * n 的布尔矩阵表示开关与灯的关系,现在每个灯来说,是否存在一种开关的集合,使得恰好使得只有当前灯是打开状态,其余灯都是熄灭状态,分别输出方案 题目分析:将开关视为变元,将灯 ...

  2. 中石油训练赛 - Trading Cards(最大权闭合子图)

    题目大意:给出 n 个卡片,可以自由买卖,且价格都是相同的,再给出 m 个集合,如果已经得到了其中一个集合中的卡片,那么可以获得该集合的收益,问如何操作可以使得收益最大化 题目分析:最大权闭合子图的模 ...

  3. 中石油训练赛 - Bouldering(最短路+剪枝)

    题目大意:给出一个 n * m 的矩阵,矩阵中有些许数字,两个数字之间的距离如果小于 r 的话就是可达,到达一个数字后会消耗数字对应的体力值,问从最下面的数字到最上面的数字的最短路是多少,必须要保证体 ...

  4. 中石油训练赛 - Cafebazaar’s Chess Tournament(FFT)

    题目大意:给出 n 个队伍,每个队伍有两个属性分别记为 a 和 b ,如果队伍 i 和队伍 j 比赛: a[ i ] > a[ j ] && b[ i ] > b[ j ] ...

  5. 中石油训练赛 - Incremental Induction(贪心)

    题目链接:点击查看 题目大意:初始时有 n 个人,给出一个 n * n 的能力矩阵,如果 maze[ i ][ j ] = 1 的话说明 i 和 j 对战的话,i 能获胜,反之亦然,现在 n 个人都在 ...

  6. 中石油训练赛 - Molecules(高斯消元解方程)

    题目链接:点击查看 题目大意:笛卡尔坐标系上给出 n 个点,如果点为 ( -1 , -1 ) 说明该点的位置是不确定的,现在给出 m 个相连接的关系, 规定位置不确定的点会被周围相邻的点拉到平均位置上 ...

  7. 中石油训练赛 - Watch Later(状压dp)

    题目链接:点击查看 题目大意: 给出一个长度为 n 的字符串,字符串中共有 k 种不同的字符,现在问删除掉所有字符的最小操作数,对于每种字符需要确定一个先后顺序,每次需要删除掉当前所有的这种字符才能去 ...

  8. 中石油训练赛 - One-Way Conveyors(边双缩点+树上差分)

    题目链接:点击查看 题目大意:给出一张 n 个点 m 条边的无向图,现在需要将这张图转换为有向图,并且使得 k 个可达条件成立,输出一种构造方案 题目分析:如果在无向图中出现环的话,那么在转换为有向图 ...

  9. 中石油训练赛 - Block(二维前缀和+思维)

    题目描述 Alice得到了一张由n×m个黑白像素点组成的图片,她想要压缩这张图片.压缩图片的过程如下: 1.首先,选择一个正整数k(k>1),将图片划分成若干个k×k的小块.如果n,m不能被k整 ...

最新文章

  1. 本硕非科班,单模型获得亚军!
  2. Stixel_World(single)学习笔记
  3. linux与python客户端,《使用python进行unix和linux管理》§5网络 §5.1 网络客户端
  4. Android Studio常用Plugin及 手动安装 Plugins
  5. STL源码剖析—stl_config
  6. php远程连接403,php中出现“ HTTP 异常 403 - 禁止访问”解决方法 总结
  7. python招生海报_从原研哉的哲学中学习海报设计
  8. 虚虚实实,亦假亦真的 ValueTuple,绝对能眩晕你
  9. 程序写不好,总理当到老!
  10. 把VOC数据集转化成txt文件python
  11. mysql 查询大于某个时间_有关于mysql 时间作为条件查询,索引失效的问题。
  12. g++是什么_路由器信号分为2.4G和5G,有什么区别?怎么选?
  13. 小沙的步伐(枚举+暴力)
  14. 初学java socket编程实例代码讲解
  15. YOLO算法v1-v3原理通俗理解
  16. 多道程序 cpu利用率
  17. c语言试题1答案,c语言试题1有答案
  18. Linux打印服务-CUPS的安装、配置和使用
  19. java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager acti
  20. html 图片放大保证不失真,教你如何在保证图片不失真的情况下缩小图片大小

热门文章

  1. 单片机c语言 u16,单片机C语言的误用
  2. Docker Client(Docker 客户端)
  3. 自定义ChannelHandler 的添加过程
  4. ConcurrentHashMap的源码分析-put方法第三阶段
  5. HTTP通信协议的组成
  6. MyBatis开发步骤
  7. 分布式文件系统研究-什么是文件系统
  8. Hive的基本操作-排序
  9. MapReduce-流量统计求和-FlowBean和Mapper代码编写
  10. 包-封装模块、设置__init__和外界导入包