zoukankan      html  css  js  c++  java
  • Exponial~(欧拉函数)~(发呆题)

    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 computesexponial(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

    Hint

    Source

    NCPC 2016

    这题题意很容易懂  但是数学不好,只能看看。

    在没做这题之前我都不知道有欧拉函数这个东西

    AB mod C=AB mod φ(C)+φ(C) mod C(B>φ(C))

    φ(C)表示小于等于C和C互质的数目。

    此处只是提供一个模板,等我对欧拉函数了解后,

    我会写一篇详细的关于欧拉函数的详解。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<queue>
     7 using namespace std;
     8 const int maxn =1e5+10;
     9 typedef long long ll;
    10 ll n,m,ans;
    11 ll  euler(ll n)
    12 {
    13     ll res=n,a=n;
    14     for (ll i=2 ;i*i <=a ;i++  ){
    15         if (a%i==0) {
    16             res=res/i*(i-1);
    17             while(a%i==0) a/=i;
    18         }
    19     }
    20     if (a>1) res=res/a*(a-1);
    21     return res;
    22 }
    23 ll modexp(ll a,ll b,ll c)
    24 {
    25     ll res=1;
    26     while(b){
    27         if (b&1) res=res*a%c;
    28         a=a*a%c;
    29         b=b>>1;
    30     }
    31     return res;
    32 }
    33 ll getans(ll n,ll m )
    34 {
    35     if (m==1) return 0;
    36     if (n==1) return 1;
    37     else if (n==2) return 2%m;
    38     else if (n==3) return 9%m;
    39     else if (n==4) return modexp(4,9,m);
    40     else {
    41         ll phi=euler(m);
    42         ll z=getans(n-1,phi);
    43         ans=modexp(n,phi+z,m);
    44     }
    45     return ans;
    46 }
    47 int main() {
    48     while(scanf("%lld%lld",&n,&m)!=EOF){
    49         printf("%lld
    ",getans(n,m));
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    一个貌似比较吊的递归转换为loop--总算成功了.
    为何反转迭代顺序就不会栈溢出了?
    将树形递归转换为loop
    递归和迭代之间的转换简单例子
    非线性递归函数转化为迭代函数举例
    将尾递归函数转换为迭代函数的利器
    转:LINUX/UNIX下的回车换行与WINDOWS下的区别
    property干嘛的
    eval和列表解析的一处陷阱
    剑指offer——16二进制中1的个数
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8727707.html
Copyright © 2011-2022 走看看