60分求助全是TLE和RE
  • 板块P1120 小木棍
  • 楼主wcyQwQ
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/2/24 21:36
  • 上次更新2023/10/28 07:49:01
查看原帖
60分求助全是TLE和RE
587248
wcyQwQ楼主2022/2/24 21:36
#include <bits/stdc++.h>
#define rint register int

using namespace std;

const int N = 70;
int a[N];
bool st[N];
int length;
int n, sum;

inline int read()
{
    int x = 0, y = 1; char c = getchar();
    while (!isdigit(c)) c = getchar();
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
    return x * y;
}

inline bool cmp(int i, int j)
{
    return a[i] > a[j];
}

inline bool dfs(int u, int s, int start)
{
    if (u * length == sum) return true;
    if (s == length) return dfs(u + 1, 0, 1);
    for (rint i = start; i <= n; i++)
    {
        if (st[i]) continue;
        if (s + a[i] > length) continue;
        st[i] = true;
        if (dfs(u, s + a[i], i + 1)) return true;
        st[i] = false;

        if (!s) return false;
        if (s + a[i] == length) return false;

        int j = i;
        while (j <= n && a[i] == a[j]) j++;
        
        i = j - 1;
    }
    return false;
}

int main()
{
    n = read();
    for (rint i = 1; i <= n; i++) a[i] = read(), sum += a[i];
    sort(a + 1, a + 1 + n, cmp);
    length = 1;
    while (true)
    {
        if (sum % length == 0 && dfs(0, 0, 1))
        {
            printf("%d\n", length);
            break;
        }
        length++;
    }
    return 0;
}
2022/2/24 21:36
加载中...