zoukankan      html  css  js  c++  java
  • CH 0101


    0101 a^b

    题目链接:传送门

    描述

    求 a 的 b 次方对 p 取模的值,其中 1≤a,b,p≤10^9 输入格式 三个用空格隔开的整数 a,b 和 p。

    输出格式

    一个整数,表示 a^b mod p 的值。

    样例输入

    2 3 9

    样例输出

    8

    题解:

    快速幂。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    ll a,b,mod;
    ll fpow(ll a,ll n)
    {
        ll res=1, base=a%mod;
        while(n)
        {
            if(n&1) res*=base, res%=mod;
            base*=base, base%=mod;
            n>>=1;
        }
        return res%mod;
    }
    int main()
    {
        cin>>a>>b>>mod;
        cout<<fpow(a,b)<<endl;
    }

    0102 64位整数乘法

    题目链接:传送门

    描述

    求 a 乘 b 对 p 取模的值,其中 1≤a,b,p≤10^18。

    输入格式

    第一行 a,第二行 b,第三行 p。

    输出格式

    一个整数,表示 a*b mod p 的值。

    样例输入
    2
    3
    9

    样例输出
    6

    题解:

    和快速幂是一样的思路。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    ll a,b,mod;
    ll fmul(ll a,ll n)
    {
        ll res=0, base=a%mod;
        while(n)
        {
            if(n&1) res+=base, res%=mod;
            base+=base, base%=mod;
            n>>=1;
        }
        return res%mod;
    }
    int main()
    {
        cin>>a>>b>>mod;
        cout<<fmul(a,b)<<endl;
    }
  • 相关阅读:
    GORM模型(Model)创建
    GORM高级查询
    LogAgent
    安装kafka
    go读取日志tailf
    bubble清单
    go操作kafka
    GORM模型删除
    Vue 父子组件间的传值
    列表和表格
  • 原文地址:https://www.cnblogs.com/dilthey/p/9958155.html
Copyright © 2011-2022 走看看