zoukankan      html  css  js  c++  java
  • hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]

    传送门

    zhx's contest

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


    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
     

     

    Recommend
    hujie   |   We have carefully selected several similar problems for you:  5189 5184 5181 5180 5177 
     
     
    哎,醉死了,2个坑点,相乘会爆long long 要用快速幂,n=1时要特判p=1;
    其他转官方题解了:http://bestcoder.hdu.edu.cn/
    1002 zhx and contest
    如果n=
    
    ,答案是
    
    ,否则答案是
    
    。
    证明:i  
    
    肯定是最小的或者最大的。考虑另外的数,如果它们的位置定了的话,那么整个序列是唯一的。
    那么i  
    
    是最小或者最大分别有n1  
    
    种情况,而整个序列单调增或者单调减的情况被算了2次,所以要减2。
    要注意的一点是因为p>31  
    
    ,所以要用快速乘法。用法与快速幂相同。如果直接乘会超过long long范围,从而wa掉。
    
    13127194 2015-03-14 22:34:13 Accepted 5187 109MS 1664K 1179 B G++ czy
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <stack>
     4 #include <vector>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <map>
     8 #include <string>
     9 
    10 #define ll long long
    11 int const N = 205;
    12 int const M = 205;
    13 int const inf = 1000000000;
    14 ll const mod = 1000000007;
    15 
    16 using namespace std;
    17 
    18 ll n,p;
    19 ll ans;
    20 
    21 ll quickmul(ll x,ll m)
    22 {
    23     ll re=0;
    24     while(m){
    25         if(m&1){
    26             re=(re+x)%p;
    27         }
    28         m/=2;
    29         x=(x+x)%p;
    30     }
    31     return re;
    32 }
    33 
    34 ll quickpow(ll x,ll m)
    35 {
    36     ll re=1;
    37     while(m)
    38     {
    39         if(m&1){
    40             re=quickmul(re,x);
    41         }
    42         m/=2;
    43         x=quickmul(x,x);
    44     }
    45     return re;
    46 }
    47 
    48 void ini()
    49 {
    50 
    51 }
    52 
    53 void solve()
    54 {
    55     if(n==1){
    56         if(p!=1)
    57             ans=1;
    58         else
    59             ans=0;
    60         return;
    61     }
    62     else{
    63         ans=quickpow(2LL,n)-2LL;
    64         ans=(ans+p)%p;
    65     }
    66 }
    67 
    68 void out()
    69 {
    70     printf("%I64d
    ",ans);
    71 }
    72 
    73 int main()
    74 {
    75     //freopen("data.in","r",stdin);
    76     //scanf("%d",&T);
    77     //for(cnt=1;cnt<=T;cnt++)
    78     while(scanf("%I64d%I64d",&n,&p)!=EOF)
    79     {
    80         ini();
    81         solve();
    82         out();
    83     }
    84 }

    再贴一发Java的程序:

    13131089 2015-03-15 10:47:30 Accepted 5187 421MS 9640K 858 B Java czy
     1 //import java.io.*;
     2 import java.util.*;
     3 import java.math.*;
     4 
     5 public class Main {
     6     static BigInteger quickpow (BigInteger x, long m, BigInteger p )
     7     {
     8         BigInteger re = BigInteger.ONE;
     9         while(m >= 1)
    10         {
    11             if(m % 2 == 1){
    12                 re = re.multiply(x).mod(p);
    13             }
    14             x = x.multiply(x).mod(p);
    15             m = m / 2;
    16         }
    17         return re;
    18     }
    19 
    20     public static void main(String[] args){
    21         Scanner in = new Scanner(System.in);
    22         long n;
    23         BigInteger p;
    24         BigInteger ans;
    25         while(in.hasNext())
    26         {
    27             n = in.nextLong();
    28             p = in.nextBigInteger();
    29             if(n == 1){
    30                 if(p.equals(BigInteger.ONE)){
    31                     ans = BigInteger.ZERO;
    32                 }
    33                 else{
    34                     ans = BigInteger.ONE;
    35                 }
    36             }
    37             else{
    38                 ans = quickpow(BigInteger.valueOf(2),n,p).subtract(BigInteger.valueOf(2));
    39                 ans = ans.add(p).mod(p);
    40             }
    41             System.out.println(ans);
    42         }
    43     }
    44 }
  • 相关阅读:
    巧用SQL生成SQL语句
    update,delete与INNER JOIN 以及删除重复数据
    sql判断各种类型的东西存在与否(参考)
    附加数据库报错823
    hibernate配置文件hibernate.cfg.xml的详细解释
    向数据库插入图片以及从数据库中读取图片并显示到jsp(数据库中存储的图片字段类型为Blob或image)
    Hibernate中hibernateTemplate()方法总结
    spring MVC之构造ModelAndView对象
    手机网站初步布局,如何建立你自己的手机网站?
    (MoMoCMS教程2)创建页面——主菜单
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4338457.html
Copyright © 2011-2022 走看看