zoukankan      html  css  js  c++  java
  • hdu2886 Lou 1 Zhuang 数学/快速幂

    All members of Hulafly love playing the famous network game called 'Lou 1 Zhuang' so much that Super Da Lord got mad with them. So he thought over and over and finally came up with a good idea to stop them. He consulted an ACM master and asked for the hardest problem ever in the world. Hulafly must solve this problem in order to get permission of playing 'Lou 1 Zhuang'. They, however, are so eager to play 'Lou 1 zhuang' and have no interest in this problem at all, so they turn to you for help. You can do this only if you swear that you will never tell Da Lord that you have helped them. 

    'Lou 1 Zhuang' is a game with only one but a very tall buiding in it and everybody runs a shop in this buiding ---- either a cake shop or a barbershop, each one occupies a whole floor. (Log in at www.xiaonei.com and check details at http://apps.xiaonei.com/soletower/tower.php?origin=2807) Super Da Lord asks that if the buiding is N stories high, how can we devided the floors(or the shops) and multiply the number of floors(or shops) in each part to make the product P as large as possible. Neverthless, Super Da Lord thought that it's still too hard for them, so he gave them some hints, he said:"Go and find the solutoin in 'Lou 1 Zhuang'!" 

    Given the buidings' height N, you are to find the largest P.

     

    题意:给出一个数N,将它拆成若干个数的和,并将这些数相乘,问乘积最大是多少,答案mod2009

    尽量拆成最多的3,进行相乘,结果最大,但是注意,若拆出若干个3,余数是1,则将1加到一个3中,作为4求乘积。

     1 #include<stdio.h>
     2 #define mod 2009
     3 int QuickPow(int a,long long n){
     4     int temp=a,ans=1;
     5     while(n){
     6         if(n&1)ans=temp*ans%mod;
     7         temp=temp*temp%mod;
     8         n>>=1;
     9     }
    10     return ans;
    11 }
    12 
    13 int main(){
    14     long long n;
    15     while(scanf("%I64d",&n)!=EOF){
    16         if(n==1)printf("1
    ");
    17         else if(n==2)printf("2
    ");
    18         else {
    19             int ans=1;
    20             long long p=n/3;
    21             if (n%3==1){
    22                 ans=4*QuickPow(3,p-1)%mod;
    23             }
    24             else if(n%3==2){
    25                 ans=2*QuickPow(3,p)%mod;
    26             }
    27             else if(n%3==0){
    28                 ans=1*QuickPow(3,p)%mod;
    29             }
    30             printf("%d
    ",ans);
    31         }
    32     }
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    css字体属性相关。
    子级用css float浮动 而父级div没高度不能自适应高度
    转载:基于Redis实现分布式锁
    LeetCode(53):最大子序和
    LeetCode(52):N皇后 II
    LeetCode(51):N皇后
    LeetCode(50):Pow(x, n)
    LeetCode(49): 字母异位词分组
    LeetCode(48):旋转图像
    LeetCode(47):全排列 II
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592536.html
Copyright © 2011-2022 走看看