fork(1) download
  1. //...START BY DOING WHAT IS NECESSARY, THEN WHAT IS POSSIBLE AND SUDDENLY YOU ARE DOING THE IMPOSSIBLE...
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define FAST_FURIER ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  5. #define pb push_back
  6. #define rep(i,a,N) for(ll i=a;i<N;i++)
  7. typedef long long ll;
  8. #define endl '\n'
  9. bool comp(int x,int y)
  10. {
  11. return x > y;
  12. }
  13.  
  14.  
  15. /*..............................code starts here........................*/
  16. vector<vector<ll> > vec;
  17.  
  18. int rem(int remain,int index,int pro,int count,int n){
  19. if(index >= vec[count].size())
  20. return remain;
  21. int a = rem((pro*vec[count][index])%n, index+1, (pro*vec[count][index])%n,count,n);
  22. int b = rem(vec[count][index]%n, index+1, (vec[count][index])%n,count,n);
  23. return max(remain,max(a,b));
  24. }
  25. int main() {
  26. FAST_FURIER;
  27. int t=1;
  28. cin >> t;
  29. ll m,n,k;
  30. string a,b;
  31. while(t--)
  32. {
  33. cin >> n >> m;
  34. ll position[n+1],M[m+1],N[n+1];
  35.  
  36. rep(i,1,n+1) cin >> position[i];
  37.  
  38. rep(i,1,n+1) cin >> N[i];
  39.  
  40. rep(i,1,m+1) cin >> M[i];
  41.  
  42. vec.resize(m+1);
  43. rep(i,1,n+1){
  44. if(N[i]%M[position[i]] != 0)
  45. vec[position[i]].pb(N[i]%M[position[i]]);
  46. }
  47.  
  48. rep(i,1,m+1){
  49. if(vec[i].size() == 0)
  50. cout << 0 << " ";
  51. else
  52. cout << rem(vec[i][0]%M[i], 1, vec[i][0], i, M[i]) << " ";
  53. }
  54. cout << endl;
  55. vec.clear();
  56. }
  57. }
  58.  
Runtime error #stdin #stdout 0s 4144KB
stdin
Standard input is empty
stdout
Standard output is empty