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; }
#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]='