zoukankan      html  css  js  c++  java
  • hdu 5187(高精度快速幂)

    zhx's contest

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1867    Accepted Submission(s): 596


    Problem Description
    As one of the most powerful brushes, zhx is required to give his juniors n problems.
    zhx thinks the ith problem's difficulty is i. He wants to arrange these problems in a beautiful way.
    zhx defines a sequence {ai} beautiful if there is an i that matches two rules below:
    1: a1..ai are monotone decreasing or monotone increasing.
    2: ai..an are monotone decreasing or monotone increasing.
    He wants you to tell him that how many permutations of problems are there if the sequence of the problems' difficulty is beautiful.
    zhx knows that the answer may be very huge, and you only need to tell him the answer module p.
     
    Input
    Multiply test cases(less than 1000). Seek EOF as the end of the file.
    For each case, there are two integers n and p separated by a space in a line. (1n,p1018)
     
    Output
    For each test case, output a single line indicating the answer.
     
    Sample Input
    2 233 3 5
     
    Sample Output
    2 1
    Hint
    In the first case, both sequence {1, 2} and {2, 1} are legal. In the second case, sequence {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} are legal, so the answer is 6 mod 5 = 1
     
    Source
     
    题意:由 1-n n个数组成的序列,有多少种组合方式满足 a[1] - a[i] 单调递增/单调递减, a[i] - a[n] 单调递增/单调递减.
    题解:如果想要a[1] - a[i] 单调递增,a[i] - a[n]单调递减,那么就只能将最大的n放到第 i 位置,那么当n的左边的排列确定了,那么其右边的排列也确定了,所以总共有C(0,n-1)+C(2,n-1)+...+C(n-2,n-1)+C(n-1,n-1) = 2^(n-1)种放法,反之就是将最小的1放到第i位置满足a[1]-a[i]单调递减,a[i]-a[n]单调递增了,情况数一样,但是由于单调递增和单调递减算了两次,所以要减2.答案为 2^n-2 ,由于n太大,所以才用了高精度快速幂(快速幂+快速乘法).
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    typedef long long LL;
    LL n,m;
    
    LL modular_multi(LL a, LL b, LL c)  /// a * b % c
    {
        LL res, temp;
        res = 0, temp = a % c;
        while (b)
        {
            if (b & 1)
            {
                res += temp;
                if (res >= c)
                {
                    res -= c;
                }
            }
            temp <<= 1;
            if (temp >= c)
            {
                temp -= c;
            }
            b >>= 1;
        }
        return res;
    }
    LL modular_exp(LL a, LL b, LL c)
    {
        LL res, temp;
        res = 1 % c, temp = a % c;
        while (b)
        {
            if (b & 1)
            {
                res = modular_multi(res, temp, c);
            }
            temp = modular_multi(temp, temp, c);
            b >>= 1;
        }
        return res;
    }
    int main()
    {
        while(scanf("%lld%lld",&n,&m)!=EOF)
        {
            LL ans = modular_exp(2,n,m);
            printf("%lld
    ",(ans-2+m)%m);
        }
        return 0;
    }
  • 相关阅读:
    squid反向代理
    LVM逻辑卷管理
    磁盘分区
    磁盘阵列
    apache基本配置
    LNMP简要配置
    高性能Web服务器Nginx
    samba文件共享服务配置(multiuser机制)二 (共两节)
    samba文件共享服务配置一(共2节)
    linux 批量修改文件名 文件名只保留部分,去掉部分
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5700817.html
Copyright © 2011-2022 走看看