Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <map>
- //#include <google/profiler.h>
- using namespace std;
- typedef unsigned long long ull;
- map<ull,int> mem;
- unsigned dat[16][16];
- unsigned sum,t;
- unsigned cs[16];
- ull ccs;
- inline ull code()
- {
- ull res=0;
- for(unsigned i=0;i<t;i++)
- res|=ull(cs[i])<<(4*i);
- return res;
- }
- int recurse()
- {
- ull ccs=code();
- map<ull,int>::iterator it;
- if((it=mem.find(ccs))!=mem.end())return it->second;
- int res=0;
- for(int i=t-1;i>=0;i--)
- if(cs[i]!=0)
- for(int j=i-1;j>=0;j--)
- if(cs[j]!=0)
- if(dat[i][cs[i]-1]+dat[j][cs[j]-1]==sum)
- {
- cs[i]--;cs[j]--;
- res=max(res,2+recurse());
- cs[i]++;cs[j]++;
- }
- mem.insert(make_pair(ccs,res));
- return res;
- }
- int main()
- {
- int n;scanf("%d%d",&n,&t);
- for(int i=0;i<t;i++)
- {
- int sz=4*n/t+(i<4*n%t?1:0);
- cs[i]=sz;
- for(int j=0;j<sz;j++)
- {
- char c;
- scanf("%d%c",&dat[i][j],&c);
- }
- }
- sum=n+1;
- printf("%d\n",recurse());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment