您的位置:首页 > 其它

Sicily 1050 Numbers & Letters

2013-06-04 15:43 239 查看
注意减法和除法可交换,除法判断`div`0

//#define LOCAL
#define DEBUG

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#if __cplusplus > 201103L
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
#define INF 0x3f3f3f3f
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
#define debug(...) printf( __VA_ARGS__)
template<class T> void DISP(const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP(#x " to " #n, x, n)
#else
#define debug(...)
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define car first
#define cdr second
#define PB push_back
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int _end_ = (b), i = (a); i <= _end_; ++i)
#define ROF(i, a, b) for (int _end_ = (b), i = (a); i >= _end_; --i)

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;

int64 fpm(int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin(T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax(T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
template <typename T> T gcd(T x, T y) {for (T t; x; t = x, x = y % x, y = t); return y; }

template<class edge> struct Graph
{
vector<vector<edge> > adj;
Graph(int n) {adj.clear (); adj.resize (n + 5);}
Graph() {adj.clear (); }
void resize(int n) {adj.resize (n + 5); }
void add(int s, edge e){adj[s].push_back (e);}
void del(int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
vector<edge>& operator [](int t) {return adj[t];}
};

const int maxn = 6;
const int MOD = int(1e9) + 7;
const double EPS = 1E-9;
const double  PI = acos(-1.0); //M_PI;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int ans;
int num[maxn], target;
void dfs(int s[], int len);
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
#endif
int nn;
cin>>nn;
while(nn--) {
for (int i=1; i<=5; i++) {
cin>>num[i];
}
cin>>target;
ans = -1 * INF;
for (int i=1; i<=5; i++) {
if (ans < num[i] && num[i] <= target) {
ans = num[i];
}
}
dfs(num, 5);
cout<<ans<<endl;
}
}
void dfs(int s[], int len) {
if (ans == target) return;
if (len == 1) {
return;
}
for (int i=1; i<=len; i++) {
for (int j=i+1; j<=len; j++) {
int t[6], cnt=0;
for (int k=1; k<=len; k++) {
if (k!=i && k!=j) {
t[++cnt] =s[k];
}
}
int tmp = s[i] *s[j];
if (tmp > ans && tmp <= target) ans = tmp;
t[++cnt] = tmp;
dfs(t, cnt);
tmp = s[i] + s[j];
if (tmp > ans && tmp <= target) ans = tmp;
t[cnt] = tmp;
dfs(t, cnt);
// - (1)
tmp = s[i] - s[j];
if (tmp > ans && tmp <= target) ans = tmp;
t[cnt] = tmp;
dfs(t, cnt);
// `sub` (2)
tmp = s[j] - s[i];
if (tmp > ans && tmp <= target) ans = tmp;
t[cnt] = tmp;
dfs(t, cnt);
// `div`
int a = min(s[i],s[j]), b= max(s[i],s[j]);
if (a != 0 && b % a == 0) {
tmp = b / a;
if (tmp > ans && tmp <= target) ans = tmp;
t[cnt] = tmp;
dfs(t, cnt);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: