• 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("");
        }
    }
  • 相关阅读:
    Notepad++64插件安装方法
    lucene、solr、nutch三者的关系
    更改MySQL数据库的编码为utf8mb4
    对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)
    登录页面的表单验证(登录+密码 )
    MSYS2 环境搭建,并整合Qt
    QAbstractItemView为截断的项显示ToolTip(使用事件过滤)
    TraceTool 跟踪工具的瑞士军刀(C++版使用)
    Indy9的TIdFTPServer封装类
    Delphi 7学习开发控件(继承TGraphicControl只画一条线)
  • 原文地址:https://www.cnblogs.com/hznumqf/p/13395611.html
Copyright © 2020-2023  润新知