http://acm.hdu.edu.cn/showproblem.php?pid=1223
一般遇到这些题,我都是暴力输出前几项,找规律。未果。
然后输出n = 1时候,以A开始,有多少个答案,
n = 2的时候,A开始,B开始,有多少个答案。然后发现了规律。大数之
const int maxn = 50 + 20; struct node { int val; int id; bool operator < (const struct node & rhs) const { if (val != rhs.val) return val < rhs.val; else return id < rhs.id; } }a[maxn]; int n; int f[maxn]; int lena; map<string, bool>mp; int aa; void dfs(int cur) { if (cur == n + 1) { for (int i = 1; i <= n; ++i) { a[i].val = f[i]; a[i].id = i; } sort(a + 1, a + 1 + n); string s; s += ‘A‘ + a[1].id - 1; for (int i = 2; i <= n; ++i) { if (a[i].val == a[i - 1].val) { s += "="; } else s += "<"; s += ‘A‘ + a[i].id - 1; } if (mp[s]) return; if (s[0] == ‘C‘) { cout << s << endl; aa++; } mp[s] = true; return; } for (int i = 1; i <= n; ++i) { f[cur] = i; dfs(cur + 1); } } void work() { n = 6; dfs(1); cout << mp.size() << endl; cout << aa << endl; }
dfs打表
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> #include <iomanip> const int maxn = 500 + 20; void bigadd (char str1[],char str2[],char str3[]) { //str1 + str2 = str3 int len1=strlen(str1+1),len2=strlen(str2+1); char b[maxn]= {0}; int i=len1,j=len2; int h=1; while (i>=1 && j>=1) b[h++] = str1[i--]-‘0‘ + str2[j--]-‘0‘; while (i>=1) b[h++] = str1[i--]-‘0‘; while (j>=1) b[h++] = str2[j--]-‘0‘; for (int i=1; i<h; i++) { //h是理论越界的 if (b[i] >= 10) { b[i+1]++; b[i] -= 10; } } if (!b[h]) --h;//没有进位到越界位。 int t=h; for (int i=1; i<=h; i++) str3[t--]=b[i]+‘0‘; str3[h+1]=‘\0‘; //一定要手动添加结束符,不然会GG return ; } char a[maxn][maxn][maxn]; char last[maxn]; char t[maxn]; void init() { // a[1][1] = 1.0; strcpy(a[1][1] + 1, "1"); // long double last = 1.0; strcpy(last + 1, "1"); for (int j = 2; j <= 52; ++j) { // a[j][j] = last; strcpy(a[j][j] + 1, last + 1); // printf("%s\n", a[j][j] + 1); for (int i = j - 1; i >= 1; --i) { // a[i][j] = a[i + 1][j] + a[i][j - 1]; bigadd(a[i + 1][j], a[i][j - 1], a[i][j]); // printf("%s\n", a[i][j] + 1); } // last = 0; memset(t, 0, sizeof t); for (int k = j; k >= 1; --k) { // last += a[k][j]; bigadd(a[k][j], t, last); strcpy(t + 1, last + 1); } } } void work() { int n; scanf("%d", &n); printf("%s\n", a[n + 1][n + 1] + 1); } int main() { #ifdef local freopen("data.txt","r",stdin); #endif init(); int t; scanf("%d", &t); while (t--) work(); return 0; }
时间: 2024-10-24 10:35:32