• [SDOI2011]计算器


    嘟嘟嘟

    黄题 + 绿题 + 蓝题 = 紫题……
    对于询问1,直接快速幂。
    对于询问2,(exgcd)
    对于询问3,(bsgs),但要特判一下(a mod c = 0)(b mod c eq 0)的时候应该无解。
    (bsgs)不懂的可以看我的这篇博客

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define rg register
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e5 + 5;
    const int base = 999979;
    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');
    }
    
    ll y, z, p;
    
    void work1(ll a, ll b, ll mod)
    {
      a %= mod; ll ret = 1;
      for(; b; b >>= 1, a = a * a % mod)
        if(b & 1) ret = ret * a % mod;
      write(ret), enter;
    }
    
    void exgcd(ll a, ll b, ll& d, ll& x, ll& y)
    {
      if(!b) d = a, x = 1, y = 0;
      else exgcd(b, a % b, d, y, x), y -= x * (a / b);
    }
    void work2(ll a, ll c, ll b)
    {
      ll x, y, d;
      exgcd(a, b, d, x, y);
      if(c % d) {puts("Orz, I cannot find x!"); return;}
      ll t = b / d;
      write((x * (c / d) % t + t) % t), enter;
    }
    
    struct Hash
    {
      int nxt; ll to; int w;
    }e[maxn];
    int head[base], hcnt = 0;
    int st[maxn], top = 0;
    void init()
    {
      hcnt = 0;
      while(top) head[st[top--]] = 0;
    }
    void insert(ll x, int y)
    {
      int h = x % base;
      if(!head[h]) st[++top] = h;
      e[++hcnt] = (Hash){head[h], x, y};
      head[h] = hcnt;
    }
    int query(ll x)
    {
      int h = x % base;
      for(int i = head[h]; i; i = e[i].nxt)
        if(e[i].to == x) return e[i].w;
      return -1;
    }
    
    int bsgs(ll a, ll b, ll c)
    {
      if(a % c == 0 && b % c) return -1;
      init();
      int s = sqrt(c);
      ll p = 1;
      for(int i = 0; i < s; ++i)
        {
          if(p == b) return i;
          insert(p * b % c, i);
          p = p * a % c;
        }
      ll q = p;
      for(int i = 1; i <= s; ++i)
        {
          int t = query(q);
          if(t != -1) return i * s - t;
          q = q * p % c;
        }
      return -1;
    }
    void work3(ll a, ll b, ll c)
    {
      b %= c;
      ll ans = bsgs(a, b, c);
      if(ans == -1) puts("Orz, I cannot find x!");
      else write(ans), enter;
    }
    
    int main()
    {
      int T = read(), K = read();
      while(T--)
        {
          y = read(); z = read(); p = read();
          if(K == 1) work1(y, z, p);
          else if(K == 2) work2(y, z, p);
          else work3(y, z, p);
        }
      return 0;
    }
    
  • 相关阅读:
    手撕RPC框架
    关于JVM的一些冷知识
    luoguP2627 修剪草坪
    [USACO16OPEN]248
    luoguP1171 售货员的难题
    luoguP2016 战略游戏
    luoguP2422 良好的感觉
    POJ1160 [IOI2000]Post Office
    luoguP2015 二叉苹果树
    BZOJ1756 小白逛公园
  • 原文地址:https://www.cnblogs.com/mrclr/p/9969918.html
Copyright © 2020-2023  润新知