zoukankan      html  css  js  c++  java
  • XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】

    1615: 刘备闯三国之三顾茅庐(三)

    Time Limit: 1000 MS  Memory Limit: 128 MB
    Submit: 45  Solved: 8
    [Submit][Status][Web Board]

    Description

            刘备(161年-223年6月10日),字玄德,东汉末年幽州涿郡涿县,西汉中山靖王刘胜的后代。刘备一生极具传奇色彩,早年颠沛流离、备尝艰辛最终却凭借自己的谋略终成一方霸主。那么在那个风云激荡的年代,刘备又是如何从一个卖草鞋的小人物一步一步成为蜀汉的开国皇帝呢?让我们一起拨开历史的迷雾,还原一个真实的刘备。

          公元207年冬至,当时驻军新野的刘备在徐庶的建议下,到南阳卧龙岗拜访诸葛亮。这是刘备第三次拜访诸葛亮的故事。据三国演义记载,此次刘备终于拜访到了诸葛亮,诸葛亮献上草庐对策,为诸葛亮描述了一个良好的战略远景。然而据我翻阅古籍发现:诸葛亮本人其实精通数论,他终于发现前面的题目太简单了,于是给刘备甩下了这么一道题,这是他最新的研究成果:

            题目意思很简单:

            已知:f(1)=1; f(k)=k^f(k-1),求f(n)%m。

            刘备为人深谋远虑,但对此类问题只能急得干瞪眼,请出诸葛亮事关大业,所以聪明的你自告奋勇,抄起纸笔开始计算了。

    Input

     有多组数据,需处理到文件结束(EOF):

    每组数据包含两个整数n,m(1<=n,m<=10^9)

    Output

    对于每一组数据:

           输出一个整数,f(n)%m的值

    Sample Input

    2 42
    5 123456789
    94 265

    Sample Output

    2
    16317634
    39

    HINT

     

    Source

    [Submit][Status][Web Board]

    题目链接:

      http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1615

    题目大意:

      f(1)=1; f(k)=k^f(k-1),求f(n)%m。

    题目思路:

      【欧拉函数+快速幂+欧拉定理】

      定理:a^b mod c = a^(b%phi[c]+phi[c]) mod c,其中要满足b >= phi[c]。(phi为欧拉函数)

      由题目可以知道这题f(n)=n^(n-1)^(n-2)^...^2^1。由于是指数级的,f(5)就已经超出longlong范围。所以试用上面的定理优化。

      由于指数只有在4^3^2^1或更小的情况下才有可能<phi[c],所以特殊处理这四个值,其余可以递归求解。

      需要log求解欧拉函数,log求快速幂,递归调用求解函数直至取模的数=1或指数下降到可以直接求的范围。

     1 /****************************************************
     2      
     3     Author : Coolxxx
     4     Copyright 2017 by Coolxxx. All rights reserved.
     5     BLOG : http://blog.csdn.net/u010568270
     6      
     7 ****************************************************/
     8 #include<bits/stdc++.h>
     9 #pragma comment(linker,"/STACK:1024000000,1024000000")
    10 #define abs(a) ((a)>0?(a):(-(a)))
    11 #define lowbit(a) (a&(-a))
    12 #define sqr(a) ((a)*(a))
    13 #define mem(a,b) memset(a,b,sizeof(a))
    14 const double EPS=1e-8;
    15 const int J=10;
    16 const int MOD=100000007;
    17 const int MAX=0x7f7f7f7f;
    18 const double PI=3.14159265358979323;
    19 const int N=1004;
    20 const int M=1004;
    21 using namespace std;
    22 typedef long long LL;
    23 double anss;
    24 LL aans;
    25 int cas,cass;
    26 LL n,m,lll,ans;
    27 LL euler(LL x)
    28 {
    29     LL res=x;
    30     int i;
    31     for(i=2;1LL*i*i<=x;i++)
    32     {
    33         if(x%i==0)
    34         {
    35             res=res/i*(i-1);
    36             while(x%i==0)x/=i;
    37         }
    38     }
    39     if(x>1)res=res/x*(x-1);
    40     return res;
    41 }
    42 LL mi(LL x,LL y,LL mod)
    43 {
    44     LL s=1;
    45     while(y)
    46     {
    47         if(y&1)s=(s*x)%mod;
    48         y/=2;
    49         x=(x*x)%mod;
    50     }
    51     return s;
    52 }
    53 LL cal(LL a,LL c)
    54 {
    55     LL e=euler(c);
    56     if(a==3 && e>9)return 9%c;
    57     if(a==2 && e>2)return 2%c;
    58     if(a==1)return 1;
    59     if(c==1)return 0;
    60     LL b=cal(a-1,e);
    61     return (1LL*mi(a,b,c)*mi(a,e,c))%c;
    62 }
    63 int main()
    64 {
    65     #ifndef ONLINE_JUDGE
    66     freopen("1.txt","r",stdin);
    67 //  freopen("2.txt","w",stdout);
    68     #endif
    69     int i,j,k;
    70     int x,y,z;
    71 //  for(scanf("%d",&cass);cass;cass--)
    72 //  for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    73 //  while(~scanf("%s",s))
    74     while(~scanf("%lld",&n))
    75     {
    76         scanf("%lld",&m);
    77         printf("%lld
    ",cal(n,m));
    78     }
    79     return 0;
    80 }
    81 /*
    82 //
    83  
    84 //
    85 */
    View Code
  • 相关阅读:
    SQL查询语句中,any和all有什么区别?
    $(function(){...});
    在ASP.NET中TextBox和TextBoxFor的区别以及ValidationMessageFor的作用以及EditorFor等的作用和用法什么?
    Brt课程设计day3
    Brt课程设计day2
    day1
    .net 高级写法总结
    可能是目前最完整的前端框架 Vue.js 全面介绍
    redis live 如何安装
    百万级PHP网站架构工具箱
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/6754509.html
Copyright © 2011-2022 走看看