September 3rd 模拟赛C T3 数字 Solution

空降题目处(外网)
点我点我点我

空降题目处(内网)
点我点我点我

Description

一个数字被称为好数字当他满足下列条件:
1. 它有2*n个数位,n是正整数(允许有前导0)
2. 构成它的每个数字都在给定的数字集合S中。
3. 它前n位之和与后n位之和相等或者它奇数位之和与偶数位之和相等
例如对于n=2,S={1,2},合法的好数字有
1111,1122,1212,1221,2112,2121,2211,2222这样8种。
已知n,求合法的好数字的个数mod 999983。

Input

第一行一个数n。
接下来一个长度不超过10的字符串,表示给定的数字集合。

Output

一行一个数字表示合法的好数字的个数mod 999983。

Solution

有某♂些♂人在读集合时用了什× EOF 函数,但是捏,像在OJ这种好♂地♂方,还是用字符串的好.




























正解来了……
叠屁大法好…
Fi,j 为前 i 位和为j的方案数.
利用Dynamic Programming求出 Fi,j
然后…




























懒得打了,看Code吧.

Code

C++

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define F(i,x,y) for(int i=x;i<=y;++i)
using namespace std;
const int M=999983;
int a[11];
long long f[1010][10010];
char ss[11];
int main()
{
    int n,ll=0;
    long long ans=0,ta=0,tb=0;
    scanf("%d\n",&n);
    scanf("%s",ss);
    F(i,0,strlen(ss)-1) a[++ll]=ss[i]-'0';
    sort(a+1,a+1+ll);
    F(i,1,ll)  f[1][a[i]]=1;
    f[0][0]=1;
    F(i,2,n)
    {
        F(j,i*a[1],i*a[ll])
        {
            F(k,1,ll) 
            {
                if(j-a[k]>=0) f[i][j]=(f[i][j]+f[i-1][j-a[k]])%M;
            }
        }
    }
    F(i,0,n*a[ll])
    {
        ans=ans+f[n][i]*f[n][i];
        ans%=M;
    }

    F(i,(n/2)*a[1],(n/2)*a[ll])
    {
        ta+=(f[n/2][i]*f[n/2][i])%M;
    }
    F(i,(n/2+n%2)*a[1],(n/2+n%2)*a[ll])
    {
        tb+=(f[n/2+n%2][i]*f[n/2+n%2][i])%M;
    }

    ans=(ans*2)%M; 
    ans=(ans-ta*tb)%M; 
    ans=(ans+ M*((ta*tb)/M)  )%M; 
    printf("%lld",ans);
    return 0;
}

Pascal

const
        maxn=999983;
var
        s:string;
        ch:char;
        f:Array[0..1000,0..9000] of int64;
        a:array[1..10] of longint;
        n,i,j,k,len:Longint;
        ans,sum,sum1,sum2:int64;
begin
        readln(n);
        readln(s);
        for i:=1 to length(s)-1 do
                for j:=i+1 to length(s) do
                        if s[i]>s[j] then
                        begin
                                ch:=s[i]; s[i]:=s[j]; s[j]:=ch;
                        end;
        for i:=1 to length(s) do
        begin
                inc(len);
                a[len]:=ord(s[i])-48;
        end;

        f[0,0]:=1;
        for i:=1 to len do
                f[1,a[i]]:=1;
        for i:=2 to n do
                for j:=i*a[1] to i*a[len] do
                        for k:=1 to len do
                                if j-a[k]>=0 then f[i,j]:=(f[i,j]+f[i-1,j-a[k]]) mod maxn;

        for i:=0 to n*a[len] do
                ans:=(ans+f[n,i]*f[n,i]) mod maxn;

        for i:=0 to (n+1) div 2*a[len] do
                sum1:=(sum1+sqr(f[(n+1) shr 1,i])) mod maxn;
        for i:=0 to n div 2*a[len] do
                sum2:=(sum2+sqr(f[n shr 1,i])) mod maxn;

        writeln((ans*2+maxn*maxn-sum1*sum2) mod maxn);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值