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\n",getans(n,m));
50     }
51     return 0;
52 }

 

转载于:https://www.cnblogs.com/qldabiaoge/p/8727707.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值