Advertisement
a53

qmat

a53
Jul 3rd, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <fstream>
  2. #include <bitset>
  3. #include <algorithm>
  4. using namespace std;
  5. ifstream fin("qmat.in");
  6. ofstream fout("qmat.out");
  7. bitset<11>ex[11];
  8. bitset<11>b[11];
  9. struct pm
  10. {
  11. int s;
  12. short n,m;
  13. bitset<11>a[11];
  14. };
  15. pm mt[10001];
  16. int g,p,i,j,q,n,m,s,sol;
  17. short f;
  18. int qx(pm a, pm b)
  19. {
  20. return a.s<b.s;
  21. }
  22. int lwrbnd(int x)
  23. {
  24. int s=1,d=g,m;
  25. while(s<=d)
  26. {
  27. m=(s+d)/2;
  28. if(x<=mt[m].s)
  29. d=m-1;
  30. else s=m+1;
  31. }
  32. return s;
  33. }
  34. int eg(int n,int m,int p)
  35. {
  36. int i,j;
  37. for(i=1;i<=n;i++)
  38. for(j=1;j<=m;j++)
  39. if(b[i][j]!=mt[p].a[i][j])return 0;
  40. return 1;
  41. }
  42. int main()
  43. {
  44. fin>>g;
  45. for(p=1;p<=g;p++)
  46. {
  47. fin>>mt[p].n>>mt[p].m;
  48. ex[mt[p].n][mt[p].m]=1;
  49. for(i=1;i<=mt[p].n;i++)
  50. for(j=1;j<=mt[p].m;j++)
  51. {
  52. fin>>f;
  53. if(f==1)mt[p].a[i][j]=1;
  54. else mt[p].a[i][j]=0;
  55. mt[p].s+=f;
  56. }
  57. }
  58. sort(mt+1,mt+g+1,qx);
  59. for(fin>>q;q;q--)
  60. {
  61. fin>>n>>m;
  62. s=0;
  63. for(i=1;i<=n;i++)
  64. for(j=1;j<=m;j++)
  65. {
  66. fin>>f;
  67. if(f==1)b[i][j]=1;
  68. else b[i][j]=0;
  69. s+=f;
  70. }
  71. if(ex[n][m])
  72. for(p=lwrbnd(s);mt[p].s==s;p++)
  73. if(mt[p].n==n && mt[p].m==m)
  74. sol+=eg(n,m,p);
  75. }
  76. fout<<sol<<"\n";
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement