topcoder SRM500 div1 Level3

本文深入探讨了算法与数据结构的核心概念及其在实际应用中的高效解决方案,涵盖了排序算法、动态规划、哈希算法等经典算法,以及二叉树、队列、栈等基本数据结构的应用案例。

Problem Statement

 NOTE: This problem statement contains superscripts that may not display properly if viewed outside of the applet.

A positive integer is called nice if the sum of its digits is equal to S and the product of its digits is equal to 2p2* 3p3 * 5p5 * 7p7. Return the sum of all nice integers, modulo 500,500,573.

Definition

 
Class:ProductAndSum
Method:getSum
Parameters:int, int, int, int, int
Returns:int
Method signature:int getSum(int p2, int p3, int p5, int p7, int S)
(be sure your method is public)
 
 

Constraints

-p2p3p5 and p7 will each be between 0 and 100, inclusive.
-S will be between 1 and 2,500, inclusive.

Examples

0) 
 
2
0
0
0
4
Returns: 26
There are two nice integers: 22 and 4. Their sum is 26.
1) 
 
0
0
0
0
10
Returns: 110109965
A single nice integer is 1,111,111,111.
2) 
 
2
0
0
0
5
Returns: 610
41 + 14 + 221 + 212 + 122 = 610.
3) 
 
1
1
1
1
10
Returns: 0
There are no nice integers in this case.
4) 
 
5
5
5
5
100
Returns: 61610122

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     


【题解】

设di为i出现的次数(1<=i<=9)

则有

s=1*d1+2*d2+...+9*d9;

p2=d2+2*d4+3*d8+d6;

p3=d3+d6+2*d9;

如果枚举d6,d8,d9,d4,则只需枚举8000000次即可,并求得d1,d2,d3.

之后便可以统计,详见:点击打开链接

【代码】

  1. #include <vector>  
  2. #include <list>  
  3. #include <map>  
  4. #include <set>  
  5. #include <deque>  
  6. #include <stack>  
  7. #include <bitset>  
  8. #include <algorithm>  
  9. #include <functional>  
  10. #include <numeric>  
  11. #include <utility>  
  12. #include <sstream>  
  13. #include <iostream>  
  14. #include <iomanip>  
  15. #include <cstdio>  
  16. #include <cmath>  
  17. #include <cstdlib>  
  18. #include <ctime>  
  19.   
  20. using namespace std;  
  21.   
  22. const int bb=500500573,N=2505;  
  23. long long G[N],F[N],L[N],M[N];  
  24.   
  25. class ProductAndSum {  
  26. public:  
  27.     int getSum(intintintintint);  
  28. };  
  29.   
  30. void getl()  
  31. {  
  32.     int i,j;  
  33.     long long k,t;  
  34.     for (i=1;i<=2500;i++)  
  35.     {  
  36.         t=1;k=i;  
  37.         for (j=bb-2;j;j/=2)  
  38.         {  
  39.             if (j&1) t=(t*k)%bb;  
  40.             k=(k*k)%bb;  
  41.         }  
  42.         L[i]=t;  
  43.     }  
  44. }  
  45.   
  46. void getm()  
  47. {  
  48.     int i;  
  49.     M[1]=1;  
  50.     for (i=2;i<=2500;i++)  
  51.     {  
  52.         M[i]=M[i-1]*10+1;  
  53.         M[i]%=bb;  
  54.     }  
  55. }  
  56.   
  57. void getk()  
  58. {  
  59.     int i,j;  
  60.     long long k,t;  
  61.     G[0]=1;  
  62.     for (i=1;i<=2500;i++)  
  63.     {  
  64.         G[i]=(G[i-1]*i)%bb;  
  65.         k=G[i];t=1;  
  66.         for (j=bb-2;j;j/=2)  
  67.         {  
  68.             if (j&1) t=(t*k)%bb;  
  69.             k=(k*k)%bb;  
  70.         }  
  71.         F[i]=t;  
  72.     }  
  73. }  
  74. int ProductAndSum::getSum(int p2, int p3, int p5, int p7, int s)  
  75. {  
  76.     getl();  
  77.     getm();  
  78.     getk();  
  79.     int i;  
  80.     long long d[10];  
  81.     long long k,ll,res,ans=0;  
  82.     for (d[4]=0;d[4]<=p2/2;d[4]++)  
  83.         for (d[6]=0;d[6]<=min(p2,p3);d[6]++)  
  84.             for (d[8]=0;d[8]<=p2/3;d[8]++)  
  85.                 for (d[9]=0;d[9]<=p3/2;d[9]++)  
  86.                 {  
  87.                     d[2]=p2-2*d[4]-d[6]-3*d[8];  
  88.                     d[3]=p3-2*d[9]-d[6];  
  89.                     d[1]=s-d[4]*4-d[6]*6-d[8]*8-d[9]*9-5*p5-7*p7-2*d[2]-3*d[3];  
  90.                     if (d[1]<0 || d[2]<0 || d[3]<0) continue;  
  91.                     d[5]=p5;d[7]=p7;  
  92.                     for (i=1,ll=0;i<=9;i++) ll+=d[i];  
  93.                     for (k=G[ll],i=1;i<=9;i++)  
  94.                     {  
  95.                         if (d[i]==0) continue;  
  96.                         k*=F[d[i]];  
  97.                         k%=bb;  
  98.                     }  
  99.                     for (res=0,i=1;i<=9;i++)  
  100.                         res+=d[i]*i;  
  101.                     res=res*k%bb*L[ll]%bb*M[ll]%bb;  
  102.                     ans=(ans+res)%bb;  
  103.                 }  
  104.     return ans;  
  105. }  
  106.   
  107. int main()  
  108. {  
  109.     ProductAndSum a;  
  110.     cout << a.getSum(5,5,5,5,100);  
  111.     return 0;  
  112. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值