najimCseJu

dp sucks

Jul 15th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. #include<algorithm>
  2. #include<bitset>
  3. #include<cctype>
  4. #include<math.h>
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8. #include<iostream>
  9. #include<iterator>
  10. #include<map>
  11. #include<queue>
  12. #include<set>
  13. #include<sstream>
  14. #include<stack>
  15. #include<strstream>
  16. #include<string>
  17. #include<utility>
  18. #include<vector>
  19. #include <cstdarg>
  20. using namespace std;
  21. /***************************************************************************************************************************************/
  22. typedef long long int LLI;
  23. typedef unsigned long long int ULLI;
  24. #define MP(X,Y) make_pair(X,Y)
  25.  
  26. #define REP(i,n) for(int i=0;i<n;i++)
  27. #define FOR(i,a,b) for(int i=a;i<=b;i++)
  28.  
  29. #define fill(a,v) memset(a,v,sizeof(a))
  30. #define fillx(a,v,n) memset(a,v,n*sizeof(a[0]))
  31. #define DEBUG(x) cout << #x << ": " << x << endl;
  32. #define all(x) (x).begin(),(x).end()
  33. #define SORT(x) sort(all(x))
  34. #define VI vector<int>
  35. #define VS vector<string>
  36. #define PB push_back
  37. #define REV(a) reverse(all(a))
  38. typedef pair<int, int>PII;
  39.  
  40. template<class T> inline T MIN_3(T a, T b, T c)
  41. {
  42.     return min(min(a, b), c);
  43. }
  44.  
  45.  
  46. template<class T> inline T MAX_3(T a, T b, T c)
  47. {
  48.     return max(max(a, b), c);
  49. }
  50.  
  51. #define BRPS(n,bit) bitset<bit>(n)
  52. #define LB(A, x) (lower_bound(all(A), x) - A.begin())//exactly where it starts
  53. #define UB(A, x) (upper_bound(all(A), x) - A.begin())
  54. #define UNQ(x) SORT(x),(x).erase(unique(all(x)),x.end())
  55.  
  56. template<class T> inline T BIGMOD(T n, T m, T mod)
  57. {
  58.     LLI ans = 1;
  59.     LLI k = n;
  60.     while(m)
  61.     {
  62.         if(m & 1)
  63.         {
  64.             ans *= k;
  65.             if(ans>mod) ans %= mod;
  66.         }
  67.         k *= k;
  68.         if(k>mod) k %= mod;
  69.         m >>= 1;
  70.     }
  71.     return ans;
  72. }
  73. #define eps 1e-11
  74. template<class T> string toString(T n)
  75. {
  76.     ostringstream ost;
  77.     ost << n;
  78.     ost.flush();
  79.     return ost.str();
  80. }
  81. template<class T> string toBinary(T n)
  82. {
  83.     string ret="";
  84.     while(n)
  85.     {
  86.         if(n%2==1)ret+='1';
  87.         else ret+='0';
  88.         n>>=1;
  89.     }
  90.     reverse(ret.begin(),ret.end());
  91.     return ret;
  92. }
  93. void combination(int n,vector< vector<int> > &ret)
  94. {
  95.     ret.resize(n+1, vector<int>(n+1, 0));
  96.     for(int i=1; i<=n; i++)
  97.     {
  98.         ret[i][0]=ret[i][i]=1;
  99.         for(int j=1; j<i; j++)
  100.         {
  101.             ret[i][j]=ret[i-1][j]+ret[i-1][j-1];
  102.         }
  103.     }
  104. }
  105. int toInt(string s)
  106. {
  107.     int r = 0;
  108.     istringstream sin(s);
  109.     sin >> r;
  110.     return r;
  111. }
  112. LLI toLInt(string s)
  113. {
  114.     LLI r = 0;
  115.     istringstream sin(s);
  116.     sin >> r;
  117.     return r;
  118. }
  119. double toDouble(string s)
  120. {
  121.     double r = 0;
  122.     istringstream sin(s);
  123.     sin >> r;
  124.     return r;
  125. }
  126. vector<string> parse(string temp)
  127. {
  128.     vector<string> ans;
  129.     ans.clear();
  130.     string s;
  131.     istringstream iss(temp);
  132.     while (iss >> s)ans.PB(s);
  133.     return ans;
  134. }
  135. template<class T> inline T gcd(T a, T b)
  136. {
  137.     if (a < 0)return gcd(-a, b);
  138.     if (b < 0)return gcd(a, -b);
  139.     return (b == 0) ? a : gcd(b, a % b);
  140. }
  141. template<class T> inline T lcm(T a, T b)
  142. {
  143.     if (a < 0)return lcm(-a, b);
  144.     if (b < 0)return lcm(a, -b);
  145.     return a*(b / gcd(a, b));
  146. }
  147. template<class T> inline T power(T b, T p)
  148. {
  149.     if (p < 0)return -1;
  150.     if (b <= 0)return -2;
  151.     if (!p)return 1;
  152.     return b*power(b, p - 1);
  153. }
  154.  
  155. template<class T> inline void asd(T &ret)
  156. {
  157.     char r;
  158.     bool start=false,neg=false;
  159.     ret=0;
  160.     while(true)
  161.     {
  162.         r=getchar();
  163.         if((r-'0'<0 || r-'0'>9) && r!='-' && !start)
  164.         {
  165.             continue;
  166.         }
  167.         if((r-'0'<0 || r-'0'>9) && r!='-' && start)
  168.         {
  169.             break;
  170.         }
  171.         if(start)ret*=10;
  172.         start=true;
  173.         if(r=='-')neg=true;
  174.         else ret+=r-'0';
  175.     }
  176.  
  177.     if(neg)
  178.         ret*=-1;
  179.     return ;
  180. }
  181.  
  182. template<class T> inline void asdasd(T x,char y)
  183. {
  184.     // 0- print+newline, 1 - print ' ',2 - nothing;
  185.     if (x < 0)
  186.     {
  187.         putchar('-');
  188.         x = -x;
  189.     }
  190.     char buf[21], *p = buf;
  191.     do
  192.     {
  193.         *p++ = '0' + x % 10;
  194.         x /= 10;
  195.     }
  196.     while (x);
  197.     do
  198.     {
  199.         putchar(*--p);
  200.     }
  201.     while (p > buf);
  202.     if(y==0)putchar('\n');
  203.     else if(y==1)putchar(' ');
  204. }
  205. #define filein(x) freopen(x,"r",stdin)
  206. #define fileout(x) freopen(x,"w",stdout)
  207. #define fst first
  208. #define snd second
  209. //istringstream(temp) >> data >> value >> stamp;
  210. //mod1 = 1000000007, mod2 = 1000000009;
  211. //.016-.040-.900-2.48
  212. /***************************************************************************************************************************************/
  213. #define Zero 120001
  214. int a[7];
  215. bool dp[7][60001];
  216. short vis[7][60001];
  217. int csum[7];
  218. int cur;
  219. int sum=0;
  220. int mid;
  221.  
  222. int restSum(int pos)
  223. {
  224.     return csum[6]-csum[pos];
  225. }
  226. bool rec(int pos,int d)
  227. {
  228.     if(pos==7)return d==mid;
  229.     bool &ret=dp[pos][d];
  230.     if(vis[pos][d]==cur)return dp[pos][d];
  231.     ret=false;
  232.     int ad;
  233.     int lb,ub;
  234.     lb=(a[pos]-(d/pos))>>1;
  235.     lb--;
  236.     ub=(a[pos]+((sum-d)/pos))>>1;
  237.     ub++;
  238.     lb=max(0,lb);
  239.     ub=min(ub,a[pos]);
  240.     for(int i=lb;i<=ub;i++)
  241.     {
  242.         ad=i<<1;
  243.         ad-=a[pos];
  244.         ad*=(pos);
  245.         if(d+ad>=0 && d+ad<=sum)ret|=rec(pos+1,ad + d);
  246.     }
  247.     dp[pos][d]=ret;
  248.     vis[pos][d]=cur;
  249.     return ret;
  250. }
  251. int main()
  252. {
  253.     //freopen("in.txt","r",stdin);
  254.     int ks=0;
  255.     cur=2;
  256.     while(true)
  257.     {
  258.         sum=0;
  259.         FOR(i,1,6)
  260.         {
  261.             asd(a[i]);
  262.  
  263.         }
  264.         int threshold=100;
  265.         FOR(i,1,6)
  266.         {
  267.             if(a[i]>=threshold)
  268.             {
  269.                 a[i]=threshold+((a[i]&1)*19);
  270.             }
  271.             sum+=(i*a[i]);
  272.             csum[i]=sum;
  273.         }
  274.         if(sum==0)break;
  275.         mid=(sum+1)>>1;
  276.         //cout << sum << " " << mid << endl;
  277.         if(sum&1)
  278.         {
  279.             printf("Collection #%d:\nCan't be divided.\n",++ks);
  280.         }
  281.         else if(!rec(1,mid))
  282.         {
  283.             printf("Collection #%d:\nCan't be divided.\n",++ks);
  284.         }
  285.         else
  286.         {
  287.             printf("Collection #%d:\nCan be divided.\n",++ks);
  288.         }
  289.         puts("");
  290.         cur++;
  291.     }
  292.     return 0;
  293. }
  294.  
  295. /*
  296. 3334 3333 3333 3333 3333 3333
  297. */
Advertisement
Add Comment
Please, Sign In to add comment