• POJ2720 Last Digits


    嘟嘟嘟

    一道题又写了近两个点……
    这道题直接暴力快速幂肯定会爆(别想高精),所以还是要用一点数学知识的~
    有一个东西叫欧拉降幂公式,就是:
         (x ^ y equiv x ^ {y mod varphi(p) + varphi(p)} (mod p))
    然后对于那些爆了的答案,就可以用这个公式递归求解。
    然而这样还是会(TLE),因此采用打表法:对于已经算过的答案(f_{b, i}),将这个数记录下来,从而减少运算复杂度。

    #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 = 105;
    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 b, n, m;  //f[m] = b ^ f[m - 1]
    ll f[maxn][maxn], a[10];
    
    ll check(ll b, ll y)
    {
      if(y == -1) return -1;
      ll ret = 1;
      for(int i = 1; i <= y; ++i)
        {
          ret *= b;
          if(ret > (ll)1e14) return -1;
        }
      return ret;
    }
    void init()
    {
      a[0] = 1;
      for(int i = 1; i <= 7; ++i) a[i] = a[i - 1] * 10;
      Mem(f, -1);
      for(int i = 0; i < maxn; ++i) f[0][i] = 0, f[1][i] = 1;
      for(int i = 2; i < maxn; ++i)
        {
          f[i][0] = 1;
          for(int j = 1; j < maxn; ++j)
    	{
    	  f[i][j] = check(i, f[i][j - 1]);
    	  if(f[i][j] == -1) break;
    	}
        }
    }
    
    ll quickpow(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;
      return ret;
    }
    
    ll phi(int n)
    {
      ll ret = n;
      for(int i = 2; i * i <= n; ++i)
        {
          if(n % i == 0)
    	{
    	  ret = ret / i * (i - 1);
    	  while(n % i == 0) n /= i;
    	}
        }
      if(n > 1) ret = ret / n * (n - 1);
      return ret;
    }
    ll calc(int b, int m, int mod)
    {
      if(mod == 1) return 0;
      if(!m) return 1;
      if(f[b][m] != -1) return f[b][m] % mod;
      int g = phi(mod);
      return quickpow(b, calc(b, m - 1, g) + g, mod);
    }
    
    void print(ll x, int n)
    {
      if(!n) return;
      print(x / 10, n - 1);
      putchar(x % 10 + '0');
    }
    
    int main()
    {
      init();
      while(scanf("%d", &b) && b)
        {
          m = read(); n = read();
          if(f[b][m] == -1) f[b][m] = calc(b, m, a[7]);
          print(f[b][m], n), enter;
        }
      return 0;
    }
    
  • 相关阅读:
    进程间通信-管道,消息,共享内存,信号量,信号,scoket
    高并发架构相关概念
    在WSL2中 安装 .NET Core SDK 进行Linux 开发工作
    OKR专题
    logback是怎么知道java代码的行数的?
    IDEA plugins
    [LeetCode] 956. Tallest Billboard 最高的广告牌
    上周热点回顾(8.10-8.16)团队
    【故障公告】腾讯云短信服务故障造成无法发送手机短信团队
    HBase~hbase-shaded-client解决包冲突问题
  • 原文地址:https://www.cnblogs.com/mrclr/p/9965063.html
Copyright © 2020-2023  润新知