AtCoder题解——Beginner Contest 178——D - Redistribution

题目相关

题目链接

AtCoder Beginner Contest 178 D 题,https://atcoder.jp/contests/abc178/tasks/abc178_d

Problem Statement

Given is an integer S. Find how many sequences there are whose terms are all integers greater than or equal to 3, and whose sum is equal to S. The answer can be very large, so output it modulo 10^9+7.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.

Samples1

Sample Input 1

7

Sample Output 1

3

Explaination

3 sequences satisfy the condition: {3,4}, {4,3} and {7}.

Samples2

Sample Input 2

2

Sample Output 2

0

Explaination

There are no sequences that satisfy the condition.

Samples3

Sample Input 3

1729

Sample Output 3

294867501

Constraints

  • 1 ≤ S ≤ 2000
  • All values in input are integers.

题解报告

题目翻译

给定一个整数 S,请找出有多少个序列满足以下条件:

1、序列中所有数字都大于等于 3;

2、序列的总和为 S。

题目分析

这个题目特别的郁闷,不知道什么情况,竞赛的时候,脑子一根筋。题目一看完,我的反应就是 DFS。

DFS

采用 DFS 的思路,也就是我们从 DFS(0) 开始,参数 0 表示当前序列的总和为 0。这样,我们可以直接套用 DFS 模板来完成代码。

但是一般我们 DFS 都是使用递归来实现,而本题的 N 最大值为 2000,递归的层次比较多,肯定会导致 TLE。下面是当时的 DFS 代码。

//https://atcoder.jp/contests/abc178/tasks/abc178_d
#include <bits/stdc++.h>
using namespace std;

const int MO=1e9+7;
int s;
long long ans=0;
vector<int> seq;

void dfs(int sum) {
    if (s==sum) {
        ans=(ans+1)%MO;
#if 0
        cout<<"{";
        for (int i=0; i<seq.size(); i++) {
            cout<<seq[i]<<",";
        }
        cout<<"}\n";
#endif
        return;
    } else if (sum>s) {
        return;
    }
    for (int i=3; i<=s-sum; i++) {
        //seq.push_back(i);
        dfs(sum+i);
        //seq.pop_back();
    }
}

int main() {
    cin>>s;

    dfs(0);
    cout<<ans<<"\n";

    return 0;
}

时间复杂度为 O(N)。

等有空将递归实现的 DFS 改成用栈实现,应该可以解决 TLE 问题。

递推

本题完全可以使用递推的思路来解决,而不需要使用 DFS。

我们用 ans[i] 表示使用 3,4,...,i 这些数字而且总和为 i 的序列总数。那么根据递推关系我们可以推到出以下的递推公式:

ans[i]=ans[i-3]+ans[i-4]+...+ans[0]\\ ans[0]=1

注意 ans[0] 的值,我们要注意空集也是一个答案。

这样代码就可以变为如下。

//https://atcoder.jp/contests/abc178/tasks/abc178_d
#include <bits/stdc++.h>

using namespace std;

const int MO=1e9+7;
const int MAXN=2e3+2;
long long ans[MAXN];

int main() {
    int s;
    cin>>s;

    //初始化ans
    ans[0]=1;

    for (int i=3; i<=s; i++) {
        for (int j=3; j<=i; j++) {
            ans[i]=(ans[i]+ans[i-j])%MO;
        }
    }

    cout<<ans[s]<<"\n";

    return 0;
}

时间复杂度为 O(N^2)。

改进

上面的递推,时间复杂度是 O(N^2),如果数据 N 进一步变大,将出现 TLE。我们继续研究一下递推公式。

ans[i]=ans[i-3]+ans[i-4]+...+ans[0]

我们写出 i-1 的递推公式,ans[i-1]=ans[i-4]+ans[i-5]+...+ans[0],我们可以将 ans[i-1] 带入到 ans[i] 的递推式中,可以得出优化后的递推式为:ans[i]=ans[i-3]+ans[i-1],这样我们可以将 O(N^2) 的复杂度,进一步优化为 O(N)。

//https://atcoder.jp/contests/abc178/tasks/abc178_d
#include <bits/stdc++.h>

using namespace std;

const int MO=1e9+7;
const int MAXN=2e3+2;
long long ans[MAXN];

int main() {
    int s;
    cin>>s;

    //初始化ans
    ans[0]=1;

    for (int i=3; i<=s; i++) {
        ans[i]=(ans[i-1]+ans[i-3])%MO;
    }

    cout<<ans[s]<<"\n";

    return 0;
}

猜你喜欢

转载自blog.csdn.net/justidle/article/details/108579301
今日推荐