70pts,双剪枝,TLE#7,8,9,求助

P1092 [NOIP2004 提高组] 虫食算

@[yuzh2005](/user/367897) 我也TLE#7#8#9:[link](/record/120097036)&code: ```cpp # include <bits/stdc++.h> # define old_six \ ios::sync_with_stdio (0);\ \ cin.tie (0);\ \ cout.tie (0); # define ffor(i,name) \ for (auto i = name.begin (); i != name.end (); ++ i) # define reg register using namespace std; typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; int n, a[30], x, a1[30], a2[30], a3[30]; string s1, s2, s3; bool f, vis[30]; bool check () { for (reg int i = 0; i < n; ++ i) if (~ a[a1[i]] && ~ a[a2[i]] && ~ a[a3[i]] && (a[a1[i]] + a[a2[i]]) % n != a[a3[i]] && (a[a1[i]] + a[a2[i]] + 1) % n != a[a3[i]]) return 1; return 0; } void dfs (int step) { // cout << step << '\n'; if (step >= n) { // for (reg int i = 0; i < n; ++ i) cout << a[i] << ' '; f = 0; // cout << '\n'; for (reg int i = n; i --;) { x = a[a1[i]] + a[a2[i]] + f; if (x < n) f = 0; else x -= n, f = 1; if (x != a[a3[i]]) return ; } for (reg int i = 0; i < n; ++ i) cout << a[i] << ' '; exit (0); } if (check ()) return ; for (reg int i = 0; i < n; ++ i) if (! vis[i]) { a[step] = i; vis[i] = 1; dfs (step + 1); vis[i] = 0; a[step] = -1; } return ; } int main () { old_six cin >> n >> s1 >> s2 >> s3; for (reg int i = 0; i < n; ++ i) a1[i] = s1[i] - 'A', a2[i] = s2[i] - 'A', a3[i] = s3[i] - 'A', a[i] = -1; dfs (0); return 0; } ```
by Special_Tony @ 2023-08-10 16:56:01


|