hdu2886 Lou 1 Zhuang 数学/快速幂

本文介绍了一个有趣的游戏问题:如何将一个数字N拆分成若干个数的和,再将这些数相乘得到最大的乘积,并给出了具体的算法实现。通过尽可能多地拆分成数字3,可以达到最优解,特殊情况还需特殊处理。

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\n");
17         else if(n==2)printf("2\n");
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\n",ans);
31         }
32     }
33     return 0;
34 }
View Code

 

转载于:https://www.cnblogs.com/cenariusxz/p/6592536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值