zoukankan      html  css  js  c++  java
  • Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021

    Description

    Everybody loves big numbers (if you do not, you might want to stop reading at this point). There are many ways of constructing really big numbers known to humankind, for instance:

    • Exponentiation: 422016=4242...42����������������2016 times422016=42⋅42⋅...⋅42⏟2016 times.
    • Factorials: 2016!=2016 ⋅ 2015 ⋅ ... ⋅ 2 ⋅ 1.

    Illustration of exponial(3) (not to scale), Picture by C.M. de Talleyrand-Périgord via Wikimedia Commons

    In this problem we look at their lesser-known love-child the exponial, which is an operation defined for all positive integers n​ as
    exponial(n)=n(n − 1)(n − 2)21
    For example, exponial(1)=1 and exponial(5)=54321 ≈ 6.206 ⋅ 10183230 which is already pretty big. Note that exponentiation is right-associative: abc = a(bc).

    Since the exponials are really big, they can be a bit unwieldy to work with. Therefore we would like you to write a program which computes exponial(n) mod m (the remainder of exponial(n) when dividing by m).

    Input

    There will be several test cases. For the each case, the input consists of two integers n (1 ≤ n ≤ 109) and m (1 ≤ m ≤ 109).

    Output

    Output a single integer, the value of exponial(n) mod m.

    Sample Input

    2 42
    5 123456789
    94 265

    Sample Output

    
    
    
    2
    16317634
    39

    思路:本题是一道经典的指数循环定理简记e(n)=exponial(n)e(n)=exponial(n),利用欧拉定理进行降幂即可,不过要注意会爆int。指数循环公式为指数循环公式为A^B = A^(B %  φ(C) +  φ(C)) % C,其中 φ(C)为1~C-1中与C互质的数的个数。

     

    
    


    代码如下:

     
     1 #include <cstdio>
     2 #include <cstring>
     3 
     4 typedef long long ll;
     5 int n, m;
     6 
     7 ll euler(int n) {
     8     ll ans = n;
     9     for(int i = 2; i * i <= n; i++) {
    10         if(n % i == 0) {
    11             ans = ans / i * (i - 1);
    12             while(n % i == 0) n /= i;
    13         }
    14     }
    15     if(n > 1) ans = ans / n * (n - 1);
    16     return ans;
    17 }
    18 
    19 ll ModPow(int x, int p, ll mod) {
    20     ll rec = 1;
    21     while(p > 0) {
    22         if(p & 1) rec = (ll)rec * x % mod;
    23         x = (ll)x * x % mod;
    24         p >>= 1;
    25     }
    26     return rec;
    27 }
    28 
    29 ll slove(int n, ll m) {
    30     if(m == 1) return 0;
    31     if(n == 1) return 1 % m;
    32     if(n == 2) return 2 % m;
    33     if(n == 3) return 9 % m;
    34     if(n == 4) return (1 << 18) % m;
    35     return (ll)ModPow(n, euler(m), m) * ModPow(n, slove(n - 1, euler(m)), m) % m;
    36 }
    37 
    38 int main() {
    39     while(~scanf("%d%d", &n, &m)) {
    40         printf("%lld
    ",slove(n, m));
    41     }
    42     return 0;
    43 }

    有不懂的请私聊我QQ(右侧公告里有QQ号)或在下方回复
  • 相关阅读:
    Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
    3ds max学习笔记(九)-- 实例操作(路径阵列)
    3ds max学习笔记(八)-- 实例操作(直行楼梯)
    3ds max学习笔记(七)-- 实例操作(桌子)
    3ds max学习笔记(六)-- 基本操作(建模前奏)
    UE4入门(二)建立和打开项目
    3ds max学习笔记(五)--操作工具
    3ds max 学习笔记(四)--创建物体
    3ds max学习笔记(一)--选择物体
    欧拉回路输出(DFS,不用回溯!)Watchcow POJ 2230
  • 原文地址:https://www.cnblogs.com/Dillonh/p/9059842.html
Copyright © 2011-2022 走看看