C. Sasha and Array

题目大意&题目链接:

  http://codeforces.com/problemset/problem/718/C

  长度为n的正整数数列,有m次操作,$opt==1$时,对$[L,R]$全部加x,$opt==2$时,对$[L,R]$求$\sum_{i=L}^{R}Fibonacc(a_{i})$。

题解:

  线段树+矩阵快速幂。

  在每个线段树存一个转移矩阵,然后YY即可。

代码:

 

  1. #include<cstdio>
  2. #include<cstring>
  3. #define lc t<<1
  4. #define rc (t<<1)|1
  5. typedef long long ll;
  6. const int N=;
  7. const ll mod=(ll)1e9+;
  8. inline int read(){
  9. int x=,f=;char ch=getchar();
  10. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  11. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  12. return x*f;
  13. }
  14. struct Martrix{
  15. ll a[][];
  16. Martrix(){memset(a,,sizeof(a));}
  17. inline void e(){
  18. memset(a,,sizeof(a));
  19. for(int i=;i<;i++)
  20. a[i][i]=;
  21. }
  22. inline void fibe(){
  23. for(int i=;i<;i++)
  24. for(int j=;j<;j++)
  25. a[i][j]=;
  26. a[][]=;
  27. }
  28. friend Martrix operator *(Martrix x,Martrix y){
  29. Martrix z;
  30. for(int i=;i<;i++)
  31. for(int j=;j<;j++)
  32. for(int k=;k<;k++)
  33. z.a[i][j]=(z.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
  34. return z;
  35. }
  36. friend Martrix operator ^(Martrix a,int b){
  37. Martrix ans;ans.e();
  38. while(b){
  39. if(b&) ans=ans*a;
  40. b>>=;
  41. a=a*a;
  42. }return ans;
  43. }
  44. }fibe_1;
  45. struct Tree{
  46. Martrix x,lazy;
  47. }tree[N<<];
  48. const int root=;
  49. inline void update(int t){
  50. tree[t].x.a[][]=(tree[lc].x.a[][]+tree[rc].x.a[][])%mod;
  51. tree[t].x.a[][]=(tree[lc].x.a[][]+tree[rc].x.a[][])%mod;
  52. }
  53. inline void downdate(int t){
  54. tree[lc].x=tree[t].lazy*tree[lc].x;
  55. tree[rc].x=tree[t].lazy*tree[rc].x;
  56. tree[lc].lazy=tree[lc].lazy*tree[t].lazy;
  57. tree[rc].lazy=tree[rc].lazy*tree[t].lazy;
  58. tree[t].lazy.e();
  59. }
  60. inline void build(int l,int r,int t){
  61. tree[t].lazy.e();
  62. if(l==r){
  63. int num=read()-;
  64. tree[t].x.fibe();
  65. tree[t].x=(tree[t].x^num)*fibe_1;
  66. return ;
  67. }int mid=l+r>>;
  68. build(l,mid,t<<);build(mid+,r,(t<<)|);
  69. update(t);
  70. }
  71. inline void add(int l,int r,int L,int R,int t,Martrix x){
  72. if(l!=r)
  73. downdate(t);
  74. if(L<=l&&r<=R){
  75. tree[t].x=x*tree[t].x;
  76. tree[t].lazy=x;
  77. return ;
  78. }
  79. int mid=l+r>>;
  80. if(R>mid) add(mid+,r,L,R,rc,x);
  81. if(L<=mid) add(l,mid,L,R,lc,x);
  82. update(t);
  83. }
  84. inline ll sum(int l,int r,int L,int R,int t){
  85. if(l!=r)
  86. downdate(t);
  87. if(L<=l&&r<=R){
  88. return tree[t].x.a[][];
  89. }
  90. int mid=l+r>>;
  91. ll ans=;
  92. if(R>mid) ans+=sum(mid+,r,L,R,rc);
  93. if(L<=mid) ans+=sum(l,mid,L,R,lc);
  94. return ans%mod;
  95. }
  96. int n,m;
  97. int main(){
  98. n=read(),m=read();
  99. fibe_1.a[][]=;
  100. build(,n,);
  101. for(int i=,opt,l,r,x;i<=m;i++){
  102. opt=read();
  103. l=read(),r=read();
  104. if(opt==){
  105. x=read();
  106. Martrix now;now.fibe();
  107. now=now^x;
  108. add(,n,l,r,root,now);
  109. }
  110. else{
  111. printf("%lld\n",sum(,n,l,r,root));
  112. }
  113. }
  114. }

          D. Andrew and Chemistry

题目大意&链接:

  http://codeforces.com/problemset/problem/718/D

  (辣鸡有机化学)给一个无向图,每个节点只允许有至多4条边,且至少一条边,而且这个图是个树,现在新建一个节点,并与这个节点连条边,求可以构成多少个不一样的图(恩……就是是不是同构)。n<=100000

题解:

  写个靠谱的哈希……然后因为节点多,但是只会有最多4条边,设F[x][i]表示从第x个节点,走第i条边得到的hash值,如果已经得到F[x][i]就不要再dfs下去了。

  (什么你问我时间复杂度)

代码:

  

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<set>
  4. inline int read(){
  5. int s=;char ch=getchar();
  6. while(ch<''||ch>'') ch=getchar();
  7. while(ch>=''&&ch<='') s=s*+(ch^),ch=getchar();
  8. return s;
  9. }
  10. typedef unsigned long long ull;
  11. ull P[]={0u,76543u,233u,123457u,56741857u,1537427u};
  12. const int N=;
  13. ull f[N];
  14. ull g[N][];
  15. std::set<ull> st;
  16. int n;
  17. struct edges {
  18. int v;edges *last;
  19. }edge[N<<],*head[N];int cnt;
  20. inline void push(int u,int v){
  21. edge[++cnt]=(edges){v,head[u]};head[u]=edge+cnt;
  22. }
  23. int a[];
  24. inline bool cmp(int x,int y){
  25. return f[x]<f[y];
  26. }
  27. inline void dfs(int x,int fa){
  28. int num=;
  29. f[x]=1u;
  30. int k=;
  31. for(edges *i=head[x];i;i=i->last){
  32. k++;
  33. if(fa==i->v) continue;
  34. if(!g[x][k])
  35. dfs(i->v,x),g[x][k]=f[i->v];
  36. else
  37. f[i->v]=g[x][k];
  38. }
  39. for(edges *i=head[x];i;i=i->last)
  40. if(fa==i->v) continue;
  41. else a[++num]=i->v;
  42. std::sort(a+,a++num,cmp);
  43. for(int i=;i<=num;i++)
  44. f[x]=f[x]*P[]+f[a[i]]*P[i];
  45. }
  46. int red[N];
  47. int main(){
  48. n=read();
  49. for(int i=;i<n;i++){
  50. int u=read(),v=read();
  51. push(u,v);push(v,u);
  52. red[u]++,red[v]++;
  53. }
  54. //printf("s");
  55. for(int i=;i<=n;i++){//printf("i=%d\n",i);
  56. if(red[i]<){
  57. dfs(i,i);
  58. // printf("i=%d\n",i);
  59. st.insert(f[i]);
  60. }
  61. }
  62. printf("%d\n",st.size());
  63. }

 

【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry的更多相关文章

  1. 【题解】[CF718C Sasha and Array]

    [题解]CF718C Sasha and Array 对于我这种喜欢写结构体封装起来的选手这道题真是太对胃了\(hhh\) 一句话题解:直接开一颗线段树的矩阵然后暴力维护还要卡卡常数 我们来把\(2 ...

  2. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  3. Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵

    E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...

  4. 【一天一道LeetCode】#88. Merge Sorted Array

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  5. Codeforces Round #373 (Div. 2) E. Sasha and Array 矩阵快速幂+线段树

    E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  6. 【Codeforces Round #438 C】 Qualification Rounds

    [链接]h在这里写链接 [题意] 给你n个问题,每个人都知道一些问题. 然后让你选择一些问题,使得每个人知道的问题的数量,不超过这些问题的数量的一半. [题解] 想法题. 只要有两个问题. 这两个问题 ...

  7. 【Codeforces Round #438 B】Race Against Time

    [链接]h在这里写链接 [题意] 时针.分钟.秒针走不过去. 问你从t1时刻能不能走到t2时刻 [题解] 看看时针.分钟.秒针的影响就好. 看看是不是在整时的位置就好. 然后看看影响到x不能到y; 然 ...

  8. 【Codeforces Round #438 A】Bark to Unlock

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举它是在连接处,还是就是整个字符串就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc+ ...

  9. 【Codeforces 321E / BZOJ 5311】【DP凸优化】【单调队列】贞鱼

    目录 题意: 输入格式 输出格式 思路: DP凸优化的部分 单调队列转移的部分 坑点 代码 题意: 有n条超级大佬贞鱼站成一行,现在你需要使用恰好k辆车把它们全都运走.要求每辆车上的贞鱼在序列中都是连 ...

随机推荐

  1. Spring Boot 2.0系列文章(七):SpringApplication 深入探索

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...

  2. minimun depth of binary tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. 我的sql数据库存储过程分页- -

    以前用到数据库存储过程分页的时候都是用 not in 但是最近工作的时候,随着数据库记录的不断增大,发现not in的效率 真的不行 虽然都设置了索引,但是当记录达到10w的时候就发现不行了,都是需要 ...

  4. python---购物车

    购物车功能如下: 1. 输入收入多少,购买商品 2. 打印购物清单,根据清单选择商品: 3. 结算,打印购物清单及总金额 # -*- coding:utf-8 -*- # LC goods=[[1,' ...

  5. java文件传输之文件编码和File类的使用

    ---恢复内容开始--- 我们知道,在用户端和服务端之间存在一个数据传输的问题,例如下载个电影.上传个照片.发一条讯息.在这里我们 就说一下文件的传输. 1.文件编码 相信大家小时候玩过积木(没玩过也 ...

  6. node七-required、缓存

    学会查API,远比会几个API更重要. 核心模块意义 -如果只是在服务器运行javascript代码,并没有多大意义,因为无法实现任何功能>读写文件.访问网络 -Node的用处在于它本身还提供可 ...

  7. 使用on-my-zsh时,php 输出内容后面多个%号

    今天用php写个命令行的小工具时,突然发现在echo输出后,总是会多个%号,开始以为是代码的问题,然后新建了一个代码文件: <?php echo 'hello world'; 输出结果: hel ...

  8. java语言浅显理解

    从厉害的c语言.到经久不衰的java语言.到不太火的安卓和IOS,到当下流行的python,这些都是软件开发中的一员. 之前在传智播客上的免费视频资源上,听了老师对java语言的介绍,感觉挺好了.今天 ...

  9. 学unity3d需要什么基础

    学unity3d需要什么基础?在游戏业发展如火如荼的情境下,很多人开始转行投身于游戏程序开发,而unity3D游戏开发则是他们必须了解和会用的游戏开发工具.在学习之前又应该了解哪些内容呢? unity ...

  10. 并发库应用之九 & 到时计数器CountDownLatch应用

    申明:CountDownLatch好像倒计时计数器,调用CountDownLatch对象的countDown方法就将计数器减1,当到达0时,所有等待者就开始执行. java.util.concurre ...