zoukankan      html  css  js  c++  java
  • Huge Mods UVA

    题意:

    输入正整数a1,a2,a3..an和模m,求a1^a2^...^an mod m

    解析:

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 100010, INF = 0x7fffffff;
    LL A[maxn], num[maxn];
    LL n;
    char str[maxn];
    LL qpow(LL a, LL b, LL m)
    {
        LL res = 1;
        while(b)
        {
            if(b & 1) res = res * a % m;
            a = a * a % m;
            b >>= 1;
        }
        return res;
    }
    
    void init()
    {
        for(int i=1; i<maxn; i++)
            A[i] = i;
        for(int i=2; i<maxn; i++)
            if(A[i] == i)
                for(int j=i; j<maxn; j+=i)
                    A[j] = A[j]/i*(i-1);
    }
    
    LL dfs(LL cnt, LL m)
    {
        if(cnt == n-1)
        {
            return num[cnt] % m;
        }
        LL phi = A[m];
        LL k = dfs(cnt+1, phi) + phi;  //因为在上一步的快速幂中已经%phi 所有这一步不用%phi
        return qpow(num[cnt], k, m);
    }
    
    int main()
    {
        init();
        int kase = 0;
        while(scanf("%s",str) && strcmp(str, "#"))
        {
            LL MOD;
            sscanf(str,"%lld", &MOD);
            cin>> n;
            for(int i=0; i<n; i++)
            {
                cin>> num[i];
            }
            printf("Case #%d: %lld
    ",++kase,dfs(0, MOD));
        }
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    scala泛函编程是怎样被选中的
    新一代编程:scala泛函编程技术-唠叨
    maven依赖本地非repository中的jar包【转】
    关于maven的profile
    intellij idea使用技巧
    springmvc的过滤器和拦截器
    spring bean的生命周期
    关于spring的bean
    关于递归
    tcp
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9326095.html
Copyright © 2011-2022 走看看