• 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
  • 相关阅读:
    2017秋-软件工程第三次作业(3)
    第二周例行总结
    2017秋-软件工程第二次作业
    2017秋-软件工程第一次作业
    ORA-01502: 索引或这类索引的分区处于不可用状态
    Merge into使用详解
    SQL2008中Merge的用法
    system表空间用满解决
    Oracle:ORA-00604: 递归 SQL 级别 1 出现错误
    AIX系统上压缩与解压文件
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592536.html
Copyright © 2020-2023  润新知