• uva 10692 高次幂取模


    Huge Mod

    Input: standard input

    Output: standard output

    Time Limit: 1 second

    2^3^4^5 mod 10 = ?The operator for exponentiation is different from the addition, subtraction, multiplication or division operators in the sense that the default associativity for exponentiation goes right to left instead of left to right. So unless we mess it up by placing parenthesis, 2^3^2 should mean 2^(3^2)=2^9=512 not (2^3)^2=8^2=64. This leads to the obvious fact that if we take the levels of exponents higher (i.e., 2^3^4^5^3), the numbers can become quite big. But let's not make life miserable. We being the good guys would force the ultimate value to be no more than 10000.

    Given a1, a2, a3, ... , aN and m(=10000) you only need to compute a1^a2^a3^...^aN mod m.

     

    Input

    There can be multiple (not more than 100) test cases. Each test case will be presented in a single line. The first line of each test case would contain the value for M(2<=M<=10000). The next number of that line would be N(1<=N<=10). Then N numbers - the values for a1, a2, a3, ... , aNwould follow. You can safely assume that 1<=ai<=1000. The end of input is marked by a line containing a single hash ('#') mark.

     

     

    Output

    For each of the test cases, print the test case number followed by the value of a1^a2^a3^...^aNmod m on one line. The sample output shows the exact format for printing the test case number.

     

     

    Sample Input

    Sample Output

    10 4 2 3 4 5
    100 2 5 2
    53 3 2 3 2
    #
    
    Case #1: 2
    Case #2: 25
    Case #3: 35
    

     题目大意:求一个数((((a^b)^c)^d)^e)..... Mod m的值

    幂太huge了,上界是1000^1000^1000^1000^1000^1000^1000^1000^1000,暴力快速幂模肯定行不通,因为幂是多少都难的计算。有公式a^x=a^(x%phi(c)+phi(c)) (mod c),所以可以用递归方法求解。

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    int phi[10010];
    int f[20],n;
    string m;
    
    void init()
    {
        int i;
        for(i=2;i<=10000;i++) phi[i]=0;
        phi[1]=1;
        for(i=2;i<=10000;i++) 
            if(!phi[i])
           for(int j=i;j<=10000;j+=i)
           {
               if(!phi[j]) phi[j]=j;
               phi[j]=phi[j]/i*(i-1);
           }
    }
    
    int montgomery(int a,int b,int c)
    {
        int t=1;
        while(b)
        {
            if(b%2)
                t=t*a%c;
            b/=2;
            a=a*a%c;
        }
        return t;
    }
    
    int dfs(int now,int mod)
    {
        if(now==n-1)
        {
            return f[now]%mod;
        }
        int t=dfs(now+1,phi[mod]);
        int ans=montgomery(f[now],t+phi[mod],mod);
        return ans;
    }
    int main()
    {
        init();
        int    i,ret,kase=1;
        while(cin>>m,m!="#")
        {
            ret=0;
            for(i=0;i<m.size();i++)
            ret=ret*10+m[i]-'0';
            cin>>n;
            for(int i=0;i<n;i++)
                scanf("%d",f+i);
            cout<<"Case #"<<kase++<<": ";
            printf("%d
    ",dfs(0,ret));
        }
        return 0;
    }
  • 相关阅读:
    Mvc+三层(批量添加、删除、修改)
    js中判断复选款是否选中
    EF的优缺点
    Git tricks: Unstaging files
    Using Git Submodules
    English Learning
    wix xslt for adding node
    The breakpoint will not currently be hit. No symbols have been loaded for this document."
    Use XSLT in wix
    mfc110ud.dll not found
  • 原文地址:https://www.cnblogs.com/xiong-/p/3221354.html
Copyright © 2020-2023  润新知