打表:记录所有输入所对应的输出;避免输入数据重复超时

https://acm.hdu.edu.cn/showproblem.php?pid=2553
#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <queue>
#include <vector>
#include <iterator>
#include <map>
#include <bitset>
#include <stack>
#include <deque>
#include <cmath>
#include <iomanip>
#include <forward_list>
#include <set>
#include <cmath>
#include <cctype>
#include <sstream>
#include <climits>
#include <list>
#include <typeinfo>
//INT_MAX 32 bit int
//LLONG_MAX 64 bit int
//LONG_MAX 64 bit int

using namespace std;

//#pragma comment(linker, "/STACK:3073741824")
#define memmin(a) memset(a,0,sizeof(a))
#define memmax(a) memset(a,0x3f,sizeof(a));
#define spr(n) fixed << setprecision(n)
#define endll '\n'
#define ifor(i, l, r) for (long long (i) = (l); (i) <= (r); ++(i))
#define rfor(i, r, l) for (long long (i) = (r); (i) >= (l); --(i))
#define fori(i,l,r) for(auto i=p;i!=p1;++i)
#define rfori(i,rl,rr) for(auto i=rl;i!=rr;++i)
#define coute cout<<'\n'
typedef long long ll;
typedef pair<int, int> P;
typedef unsigned long long ull;

template <class T, class Container = vector<T>,
          class Compare = less<typename Container::value_type> >
using priqueue = priority_queue<T,Container,Compare>;

#define start1
#ifndef start1
const int IINF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1.0e-9;
const long long MOD = 1e9 + 7;
const int MAX1 = 1e5 + 100;
const int MAX2 = 1e7 + 100;

#endif

#define start
#ifndef start2

//__int128读取
void  read(ll& w) {
	w=0;
	ll f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch <= '9' && ch >= '0') {
		w = w * 10 + ch - '0';
		ch = getchar();
	}
}

void print128(ll x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9)print128(x / 10);
	putchar(x % 10 + '0');
}

#endif

#define start3
#ifndef start3
template<typename container>
void _print(container &a2) {
	for (auto i = a2.begin(); i != a2.end(); i++) {
		if (i == a2.begin()) cout << *i;
		else
			cout << " " << *i;
	}
}

template<typename T1>
auto _print(T1 a1, ll l, ll r) ->void {
	for (ll i = l; i <= r; ++i) {
		if (i == l) cout << a1[i];
		else cout << " " << a1[i];
	}
}
#endif

ll n;
int cnt{};
bitset<11> lie;
bitset<100> z;
bitset<100>f;
void dfs(int deep) {
	if(deep==n) {
		++cnt;
		return ;
	}
	ifor(j,0,n-1) {
		if((!lie[j])&&(!z[deep-j+n-1])&&(!f[deep+j])) {
			lie.set(j,1);
			z.set(deep-j+n-1,1);
			f.set(deep+j,1);
			dfs(deep+1);
			lie.set(j,0);
			z.set(deep-j+n-1,0);
			f.set(deep+j,0);
		}
	}

}
int main(int argv,char** argc) {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int a[12];
	ifor(i,1,10) {
		n=i;
		dfs(0);
		a[i]=cnt;
		cnt=0;
		lie.reset();
		z.reset();
		f.reset();
	}
	read(n);
	while(n) {
		cout<<a[n]<<endl;
		read(n);
	}
	return 0;
}

全部评论

相关推荐

冲芭芭拉鸭:你这图还挺新,偷了。
投递美团等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务