zoukankan      html  css  js  c++  java
  • HDU-6608 Fansblog 数论 ,威尔逊定理,快速乘

    第一次使用快速乘,原来是这样的。

    注意Put快速输出要改一下类型,(ll),注意快速输出后面要手打换行。

    注意熟悉快速幂配合快速乘的模板。

    #pragma warning(disable:4996)
    
    #include<iostream>
    #include<algorithm>
    #include<bitset>
    #include<tuple>
    #include<unordered_map>
    #include<fstream>
    #include<iomanip>
    #include<string>
    #include<cmath>
    #include<cstring>
    #include<vector>
    #include<map>
    #include<set>
    #include<list>
    #include<queue>
    #include<stack>
    #include<sstream>
    #include<cstdio>
    #include<ctime>
    #include<cstdlib>
    #define pb push_back
    #define INF 0x3f3f3f3f
    #define inf 0x7FFFFFFF
    #define moD 1000000003
    #define pii pair<ll,ll>
    #define eps 1e-8
    #define equals(a,b) (fabs(a-b)<eps)
    #define bug puts("bug")
    #define re  register
    #define fi first
    #define se second
    typedef  long long ll;
    typedef unsigned long long ull;
    const ll MOD = 1e6 + 7;
    const int maxn = 2e4 +5;
    const double Inf = 10000.0;
    const double PI = acos(-1.0);
    using namespace std;
    
    ll mul(ll a, ll b, ll m) {
        ll res = 0;
        while (b) {
            if (b & 1) res = (res + a) % m;
            a = (a + a) % m;
            b >>= 1;
        }
        return res % m;
    }
    
    ll quickPower(ll a, ll b, ll m) {
        ll base = a;
        ll ans = 1ll;
        while (b) {
            if (b & 1) ans = mul(ans, base , m);
            base = mul(base, base , m);
            b >>= 1;
        }
        return ans;
    }
    
    
    int readint() {
        int x = 0, f = 1; char ch = getchar();
        while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
        while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
        return x * f;
    }
    
    ll readll() {
        ll x = 0, f = 1; char ch = getchar();
        while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
        while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
        return x * f;
    }
    
    void Put(ll x)  {
        if (x > 9) Put(x / 10);
        putchar(x % 10 + '0');
    }
    
    bool is_prime(ll tmp) {
        if (tmp == 1ll) return false;
        for (ll i = 2; i * i <= tmp; i++) {
            if (tmp % i == 0) return false;
        }
        return true;
    }
    
    
    int main() {
        int T;
        ll p;
        T = readint();
        while (T--) {
            p = readll();
            ll tmp = p - 1;
            ll res = p - 1;
            while (!is_prime(tmp)) {
                res =mul(res, quickPower(tmp, p - 2, p),p);
                res %= p;
                tmp--;
            }
            Put(res);
            puts("");
        }
    }
  • 相关阅读:
    求原根
    koa2-router中间件来请求数据获取
    koa2 快速开始
    如何修改host
    bzoj 2480——扩展BSGS
    bzoj 4128: Matrix ——BSGS&&矩阵快速幂&&哈希
    Ubuntu 16.04LTS 安装和配置Bochs
    2019ICPC徐州网络赛 A.Who is better?——斐波那契博弈&&扩展中国剩余定理
    求十亿内所有质数的和
    MYSQL的随机查询的实现方法
  • 原文地址:https://www.cnblogs.com/hznumqf/p/13395611.html
Copyright © 2011-2022 走看看