topcoder SRM500 div1 Level3

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

-p2, p3, p5 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.

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

【代码】

#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值