zoukankan      html  css  js  c++  java
  • luogu P5151 HKE与他的小朋友

    嘟嘟嘟


    看到(i)变成了(A_i),我突然想起了置换这个东西。于是马上到网上学了一遍轮换乘法。
    手模后发现轮换乘法满足结合律,但不满足交换律。
    于是就可以快速幂啦。
    需要注意的是每一次相乘是(O(n))的,因此总复杂度为(O(n log n))
    代码一看就懂

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e5 + 5;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int n, k, a[maxn];
    
    int tp[maxn], ret[maxn];
    In void mul(int* ret, int* a)
    {
      for(int i = 1; i <= n; ++i) tp[i] = ret[a[i]];
      for(int i = 1; i <= n; ++i) ret[i] = tp[i];
    }
    In void quickpow(int* a, int b)
    {
      for(int i = 1; i <= n; ++i) ret[i] = i;
      for(; b; b >>= 1, mul(a, a))
        if(b & 1) mul(ret, a);
    }
    
    int main()
    {
      n = read(); k = read();
      for(int i = 1; i <= n; ++i) a[i] = read();
      quickpow(a, k);
      for(int i = 1; i <= n; ++i) tp[ret[i]] = i;
      for(int i = 1; i <= n; ++i) write(tp[i]), space; enter;
      return 0;
    }
    
  • 相关阅读:
    SOLO: 按位置分割对象
    支付宝架构
    h264和h265多维度区别
    机器学习图解
    机器视觉系统性能
    APA自动泊车系统
    智能驾驶测距估计
    结构感知图像修复:ICCV2019论文解析
    Lambda表达式
    转:利用 T-sql 的从句 for xml path('') 实现多行合并到一行, 并带有分隔符
  • 原文地址:https://www.cnblogs.com/mrclr/p/10178849.html
Copyright © 2011-2022 走看看