啥都不会啊!怎么办啊!

Fitz

慢慢来生活总会好起来的!!!

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 }

 

posted @ 2018-04-06 14:39  Fitz~  阅读(369)  评论(0编辑  收藏  举报