zoukankan      html  css  js  c++  java
  • 华华教月月做数学(快速幂)

    链接:https://ac.nowcoder.com/acm/problem/23046
    来源:牛客网

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 32768K,其他语言65536K
    64bit IO Format: %lld

    题目描述

    找到了心仪的小姐姐月月后,华华很高兴的和她聊着天。然而月月的作业很多,不能继续陪华华聊天了。华华为了尽快和月月继续聊天,就提出帮她做一部分作业。
    月月的其中一项作业是:给定正整数A、B、P,求ABmod  P的值。华华觉得这实在是毫无意义,所以决定写一个程序来做。但是华华并不会写程序,所以这个任务就交给你了。
    因为月月的作业很多,所以有T组询问。

    输入描述:

    第一行一个正整数T表示测试数据组数。
    接下来T行,每行三个正整数A、B、P,含义如上文。

    输出描述:

    输出T行,每行一个非负整数表示答案。

    示例1

    输入

    2
    2 5 10
    57284938291657 827493857294857 384729583748273

    输出

    2
    18924650048745

    备注:

    1T1031≤A,B,P≤1018

    快速幂模板题

    但是P的值很大,可以达到1018,所以,我们不能直接打快速幂,要用__int128储存中间计算结果,避免爆LL

     1 #include <bits/stdc++.h>
     2 typedef long long LL;
     3 #define pb push_back
     4 #define mst(a) memset(a,0,sizeof(a))
     5 const int INF = 0x3f3f3f3f;
     6 const double eps = 1e-8;
     7 const int mod = 1e9+7;
     8 const int maxn = 1e5+10;
     9 using namespace std;
    10 
    11 __int128 fpow(__int128 a, __int128 b,__int128 mo)
    12 {
    13     if (a == 0) return 0;
    14     __int128 ans = 1;
    15     for (; b; b >>= 1, a = (a % mo * a% mo) % mo)
    16         if (b & 1) ans = (ans % mo * a % mo) % mo;
    17     return ans;
    18 }
    19 
    20 int main()
    21 {
    22     #ifdef DEBUG
    23     freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout);
    24     #endif
    25     
    26     int T;
    27     scanf("%d",&T);
    28     while(T--)
    29     {
    30         LL a,b,p;
    31         scanf("%lld %lld %lld",&a,&b,&p);
    32         printf("%lld
    ",(LL)fpow((__int128)a,(__int128)b,(__int128)p));
    33     }
    34     
    35     return 0;
    36 }

    Python做法:

    1 T = int(input())
    2 for _ in range(T):
    3     a,b,c = map(int,input().split())
    4     print(pow(a,b,c))

     

    -

  • 相关阅读:
    bootstrap
    jQuery-介绍 加载 选择器 样式操作 属性操作 绑定click事件
    javascript -- BOM,DOM
    前端 -- javas-基本语法/引用等
    fatal: remote origin already exists.报错
    git
    npm install 时总是报phantomjs-prebuilt@2.1.14安装失败
    JavaScript中设置元素class,添加/删除元素class的方法
    单选按钮点击相关文字选中
    JS/Jquery实现select选中option触发事件
  • 原文地址:https://www.cnblogs.com/jiamian/p/13221494.html
Copyright © 2011-2022 走看看