Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
- #define RFOR(i,b,a) for (int i = (b)-1; i >= (a); --i)
- #define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
- #define FILL(a,value) memset(a, value, sizeof(a))
- #define SZ(a) (int)a.size()
- #define ALL(a) a.begin(), a.end()
- #define PB push_back
- #define MP make_pair
- typedef long long LL;
- typedef vector<int> VI;
- typedef pair<int, int> PII;
- const double PI = acos(-1.0);
- const int INF = 1000 * 1000 * 1000 + 7;
- const LL LINF = INF * (LL) INF;
- const int MAX = 1010;
- int D[2][MAX * 2];
- int* Prime = D[0] + MAX;
- int* Beautiful = D[1] + MAX;
- void init()
- {
- FOR (i, 2, MAX)
- {
- bool ok = true;
- FOR (j, 2, MAX)
- {
- if (j * j > i) break;
- if (i % j == 0)
- {
- ok = false;
- break;
- }
- }
- if (ok) Prime[i] = true;
- else Prime[i] = false;
- }
- FOR (i, 0, MAX - 2)
- {
- VI d;
- int x = i;
- while(x)
- {
- d.PB(x % 10);
- x /= 10;
- }
- bool ok = false;
- FOR (mask, 0, 1<<SZ(d))
- {
- int sum = 0;
- FOR (j, 0, SZ(d))
- {
- if (mask & (1<<j)) sum ^= d[j];
- else sum ^= (9 - d[j]);
- }
- if (sum == 0)
- {
- ok = true;
- break;
- }
- }
- if (ok) Beautiful[i] = Beautiful[-i] = true;
- }
- }
- bool F(string& t, int x)
- {
- if (t == "prime?")
- {
- if (x <= 1) return false;
- return Prime[x];
- }
- if (t == "composite?")
- {
- if (x <= 1) return false;
- return !Prime[x];
- }
- if (t == "even?")
- {
- return x % 2 == 0;
- }
- if (t == "odd?")
- {
- return x % 2 != 0;
- }
- if (t == "positive?")
- {
- return x > 0;
- }
- if (t == "negative?")
- {
- return x < 0;
- }
- if (t == "beautiful?")
- {
- return Beautiful[x];
- }
- throw -1;
- }
- bool F(string& t, const string& ans, int x)
- {
- bool res = F(t, x);
- if (ans == "YES") return res;
- return !res;
- }
- bool F(string& q, string& t, string& ans, int x, int y)
- {
- if (q == "Is the first number")
- {
- return F(t, ans, x);
- }
- if (q == "Is the second number")
- {
- return F(t, ans, y);
- }
- if (q == "Are both the numbers")
- {
- if (ans == "YES") return F(t, ans, x) && F(t, ans, y);
- return F(t, "NO", x) || F(t, "NO", y);
- }
- if (q == "Is either of the numbers")
- {
- if (ans == "YES") return F(t, ans, x) || F(t, ans, y);
- return F(t, "NO", x) && F(t, "NO", y);
- }
- if (q == "Is the product of the two numbers")
- {
- return F(t, ans, x * y);
- }
- if (q == "Is the sum of the two numbers")
- {
- return F(t, ans, x + y);
- }
- if (q == "Is the difference of the two numbers")
- {
- return F(t, ans, abs(x - y));
- }
- throw -1;
- }
- pair<string, string> split(string& s)
- {
- string a, b;
- string t;
- stringstream ss;
- ss<<s;
- while(ss >> t)
- {
- if (a != "") a += " ";
- if (b != "") a += b;
- b = t;
- }
- return MP(a, b);
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- //ios::sync_with_stdio(false); cin.tie(0);
- init();
- int tt;
- cin>>tt;
- vector<PII> all;
- vector<PII> nxt;
- int test = 0;
- for (;tt;tt--)
- {
- test++;
- int n;
- cin>>n;
- all.clear();
- FOR (i, -100, 101)
- {
- FOR (j, -100, 101)
- {
- all.PB(MP(i, j));
- }
- }
- string s;
- getline(cin, s);
- printf("Case #%d:\n", test);
- FOR (i, 0, n)
- {
- getline(cin, s);
- string t;
- getline(cin, t);
- pair<string, string> a = split(s);
- s = a.first;
- string q = a.second;
- nxt.clear();
- FOR (i, 0, SZ(all))
- {
- if (F(s, q, t, all[i].first, all[i].second)) nxt.PB(all[i]);
- }
- swap(all, nxt);
- printf("%d\n", SZ(all));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement