fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. ll H,A,t;
  5. //using vector instead of set gives TLE
  6. set<string> v;
  7. void dfs(string s)
  8. {
  9. v.insert(s);
  10. string to_check=s;
  11. for(int i=0;i<to_check.size();i++)
  12. {
  13. to_check[(i+H)%to_check.size()]=s[i];
  14. }
  15. if(v.find(to_check)==v.end())
  16. dfs(to_check);
  17. //Now we have check for every possible string applying shift operations first
  18. //Now try for every possible string using add operation first
  19. to_check=s;
  20. for(int i=1;i<to_check.size();i+=2)
  21. {
  22. to_check[i]='0'+((s[i]-'0')+A)%10;
  23. }
  24. if(v.find(to_check)==v.end())
  25. dfs(to_check);
  26.  
  27. }
  28. int main()
  29. {
  30. ios_base::sync_with_stdio(false);
  31.  
  32. cin>>t;
  33. while(t--)
  34. {
  35. string n;
  36. v.clear();
  37. cin>>n>>A>>H;
  38. dfs(n);
  39. cout<<(*v.begin())<<endl;
  40. }
  41. }
  42.  
Success #stdin #stdout 0s 15376KB
stdin
2
31
4 1
160
9 2
stdout
11
000