Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
  5. #define RFOR(i,b,a) for (int i = (b)-1; i >= (a); --i)
  6. #define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
  7. #define FILL(a,value) memset(a, value, sizeof(a))
  8.  
  9. #define SZ(a) (int)a.size()
  10. #define ALL(a) a.begin(), a.end()
  11. #define PB push_back
  12. #define MP make_pair
  13.  
  14. typedef long long LL;
  15. typedef vector<int> VI;
  16. typedef pair<int, int> PII;
  17.  
  18. const double PI = acos(-1.0);
  19. const int INF = 1000 * 1000 * 1000 + 7;
  20. const LL LINF = INF * (LL) INF;
  21.  
  22. const int MAX = 1010;
  23.  
  24. int D[2][MAX * 2];
  25.  
  26. int* Prime = D[0] + MAX;
  27. int* Beautiful = D[1] + MAX;
  28.  
  29. void init()
  30. {
  31.     FOR (i, 2, MAX)
  32.     {
  33.         bool ok = true;
  34.         FOR (j, 2, MAX)
  35.         {
  36.             if (j * j > i) break;
  37.  
  38.             if (i % j == 0)
  39.             {
  40.                 ok = false;
  41.                 break;
  42.             }
  43.         }
  44.  
  45.         if (ok) Prime[i] = true;
  46.         else Prime[i] = false;
  47.     }
  48.  
  49.     FOR (i, 0, MAX - 2)
  50.     {
  51.         VI d;
  52.         int x = i;
  53.         while(x)
  54.         {
  55.             d.PB(x % 10);
  56.             x /= 10;
  57.         }
  58.  
  59.         bool ok = false;
  60.  
  61.         FOR (mask, 0, 1<<SZ(d))
  62.         {
  63.             int sum = 0;
  64.             FOR (j, 0, SZ(d))
  65.             {
  66.                 if (mask & (1<<j)) sum ^= d[j];
  67.                 else sum ^= (9 - d[j]);
  68.             }
  69.  
  70.             if (sum == 0)
  71.             {
  72.                 ok = true;
  73.                 break;
  74.             }
  75.         }
  76.  
  77.         if (ok) Beautiful[i] = Beautiful[-i] = true;
  78.     }
  79. }
  80.  
  81. bool F(string& t, int x)
  82. {
  83.     if (t == "prime?")
  84.     {
  85.         if (x <= 1) return false;
  86.         return Prime[x];
  87.     }
  88.  
  89.     if (t == "composite?")
  90.     {
  91.         if (x <= 1) return false;
  92.         return !Prime[x];
  93.     }
  94.  
  95.     if (t == "even?")
  96.     {
  97.         return x % 2 == 0;
  98.     }
  99.  
  100.     if (t == "odd?")
  101.     {
  102.         return x % 2 != 0;
  103.     }
  104.  
  105.     if (t == "positive?")
  106.     {
  107.         return x > 0;
  108.     }
  109.  
  110.     if (t == "negative?")
  111.     {
  112.         return x < 0;
  113.     }
  114.  
  115.     if (t == "beautiful?")
  116.     {
  117.         return Beautiful[x];
  118.     }
  119.  
  120.     throw -1;
  121. }
  122.  
  123. bool F(string& t, const string& ans, int x)
  124. {
  125.     bool res = F(t, x);
  126.     if (ans == "YES") return res;
  127.     return !res;
  128. }
  129.  
  130. bool F(string& q, string& t, string& ans, int x, int y)
  131. {
  132.     if (q == "Is the first number")
  133.     {
  134.         return F(t, ans, x);
  135.     }
  136.  
  137.     if (q == "Is the second number")
  138.     {
  139.         return F(t, ans, y);
  140.     }
  141.  
  142.     if (q == "Are both the numbers")
  143.     {
  144.         if (ans == "YES") return F(t, ans, x) && F(t, ans, y);
  145.         return F(t, "NO", x) || F(t, "NO", y);
  146.     }
  147.  
  148.     if (q == "Is either of the numbers")
  149.     {
  150.         if (ans == "YES") return F(t, ans, x) || F(t, ans, y);
  151.         return F(t, "NO", x) && F(t, "NO", y);
  152.     }
  153.  
  154.     if (q == "Is the product of the two numbers")
  155.     {
  156.         return F(t, ans, x * y);
  157.     }
  158.  
  159.     if (q == "Is the sum of the two numbers")
  160.     {
  161.         return F(t, ans, x + y);
  162.     }
  163.  
  164.     if (q == "Is the difference of the two numbers")
  165.     {
  166.         return F(t, ans, abs(x - y));
  167.     }
  168.  
  169.     throw -1;
  170. }
  171.  
  172. pair<string, string> split(string& s)
  173. {
  174.     string a, b;
  175.     string t;
  176.     stringstream ss;
  177.     ss<<s;
  178.     while(ss >> t)
  179.     {
  180.         if (a != "") a += " ";
  181.         if (b != "") a += b;
  182.         b = t;
  183.     }
  184.  
  185.     return MP(a, b);
  186. }
  187.  
  188. int main()
  189. {
  190.     //freopen("in.txt", "r", stdin);
  191.     //ios::sync_with_stdio(false); cin.tie(0);
  192.  
  193.     init();
  194.     int tt;
  195.     cin>>tt;
  196.     vector<PII> all;
  197.     vector<PII> nxt;
  198.     int test = 0;
  199.     for (;tt;tt--)
  200.     {
  201.         test++;
  202.         int n;
  203.         cin>>n;
  204.  
  205.         all.clear();
  206.  
  207.         FOR (i, -100, 101)
  208.         {
  209.             FOR (j, -100, 101)
  210.             {
  211.                 all.PB(MP(i, j));
  212.             }
  213.         }
  214.  
  215.         string s;
  216.         getline(cin, s);
  217.  
  218.         printf("Case #%d:\n", test);
  219.         FOR (i, 0, n)
  220.         {
  221.             getline(cin, s);
  222.             string t;
  223.             getline(cin, t);
  224.  
  225.             pair<string, string> a = split(s);
  226.  
  227.             s = a.first;
  228.             string q = a.second;
  229.  
  230.  
  231.             nxt.clear();
  232.             FOR (i, 0, SZ(all))
  233.             {
  234.                 if (F(s, q, t, all[i].first, all[i].second)) nxt.PB(all[i]);
  235.             }
  236.  
  237.             swap(all, nxt);
  238.  
  239.             printf("%d\n", SZ(all));
  240.         }
  241.     }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement