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 | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
| - | p2, p3, p5 and p7 will each be between 0 and 100, inclusive. | ||||||||||||
| - | S will be between 1 and 2,500, inclusive. | ||||||||||||
Examples | |||||||||||||
| 0) | |||||||||||||
| |||||||||||||
| 1) | |||||||||||||
| |||||||||||||
| 2) | |||||||||||||
| |||||||||||||
| 3) | |||||||||||||
| |||||||||||||
| 4) | |||||||||||||
| |||||||||||||
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.
之后便可以统计,详见:点击打开链接
【代码】
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const int bb=500500573,N=2505;
long long G[N],F[N],L[N],M[N];
class ProductAndSum {
public:
int getSum(int, int, int, int, int);
};
void getl()
{
int i,j;
long long k,t;
for (i=1;i<=2500;i++)
{
t=1;k=i;
for (j=bb-2;j;j/=2)
{
if (j&1) t=(t*k)%bb;
k=(k*k)%bb;
}
L[i]=t;
}
}
void getm()
{
int i;
M[1]=1;
for (i=2;i<=2500;i++)
{
M[i]=M[i-1]*10+1;
M[i]%=bb;
}
}
void getk()
{
int i,j;
long long k,t;
G[0]=1;
for (i=1;i<=2500;i++)
{
G[i]=(G[i-1]*i)%bb;
k=G[i];t=1;
for (j=bb-2;j;j/=2)
{
if (j&1) t=(t*k)%bb;
k=(k*k)%bb;
}
F[i]=t;
}
}
int ProductAndSum::getSum(int p2, int p3, int p5, int p7, int s)
{
getl();
getm();
getk();
int i;
long long d[10];
long long k,ll,res,ans=0;
for (d[4]=0;d[4]<=p2/2;d[4]++)
for (d[6]=0;d[6]<=min(p2,p3);d[6]++)
for (d[8]=0;d[8]<=p2/3;d[8]++)
for (d[9]=0;d[9]<=p3/2;d[9]++)
{
d[2]=p2-2*d[4]-d[6]-3*d[8];
d[3]=p3-2*d[9]-d[6];
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];
if (d[1]<0 || d[2]<0 || d[3]<0) continue;
d[5]=p5;d[7]=p7;
for (i=1,ll=0;i<=9;i++) ll+=d[i];
for (k=G[ll],i=1;i<=9;i++)
{
if (d[i]==0) continue;
k*=F[d[i]];
k%=bb;
}
for (res=0,i=1;i<=9;i++)
res+=d[i]*i;
res=res*k%bb*L[ll]%bb*M[ll]%bb;
ans=(ans+res)%bb;
}
return ans;
}
int main()
{
ProductAndSum a;
cout << a.getSum(5,5,5,5,100);
return 0;
}

该博客主要介绍了TopCoder SRM500比赛中一个关于整数约束的问题。博主通过设立变量di表示每个数字i的出现次数,并通过公式计算总和s和特定位数之和p2、p3。提出通过枚举d6、d8、d9、d4的值来简化计算,从而解决该问题,总计需要枚举8000000次。提供了问题解析和代码实现链接。
533

被折叠的 条评论
为什么被折叠?



