• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
村雨sup
自己选的路,跪着也要走完 XD
博客园    首页    新随笔    联系   管理    订阅  订阅
数论练习(1)——取余运算(快速幂)

1497 取余运算

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 
 
题目描述 Description

输入b,p,k的值,编程计算bp mod k的值。其中的b,p,k*k为长整型数(2^31范围内)。

输入描述 Input Description

b p k 

输出描述 Output Description

输出b^p mod k=?

=左右没有空格

样例输入 Sample Input

2  10  9

样例输出 Sample Output

2^10 mod 9=7

数据范围及提示 Data Size & Hint
 
#include<bits/stdc++.h>
using namespace std;
typedef long long  ll;

const int inf = 0x3f3f3f3f;
const int maxn = 40000 + 20;
const int moder = 1e9 + 7;
const int K = 256;

ll mod_pow(ll x,ll n,ll mod)
{
    ll res = 1;
    while(n > 0)
    {
        if(n & 1) res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    ll b,p,k;
    scanf("%lld%lld%lld",&b,&p,&k);
    cout << b << "^" << p << " " << "mod " << k <<"=" <<mod_pow(b,p,k);
    return 0;
}

 

posted on 2018-02-01 23:22  村雨sup  阅读(167)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3