Advertisement
a53

Caps

a53
Mar 14th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. int bits(int64_t N) {
  10. int have = 0;
  11. while (N > 0) {
  12. ++have;
  13. N -= (N & -N);
  14. }
  15. return have;
  16. }
  17.  
  18. int map(char c) {
  19. if (c >= 'A' && c <= 'Z')
  20. return c - 'A';
  21. return c - 'a' + 26;
  22. }
  23.  
  24. char rev(char c) {
  25. return c ^ 'A' ^ 'a';
  26. }
  27.  
  28. int main () {
  29. ifstream cin("caps.in");
  30. ofstream cout("caps.out");
  31.  
  32. int K, Q; assert(cin >> K >> Q);
  33. assert(1 <= K && K <= 100 * 1000);
  34. assert(1 <= Q && Q <= 50 * 1000);
  35.  
  36. string S; cin >> S;
  37. assert(int(S.size()) == K);
  38. for (auto &c : S)
  39. assert((c >= 'a' && c <= 'z') || (c >= 'A' || c <= 'Z'));
  40.  
  41. int total_count[52];
  42. fill(total_count, total_count + 52, 0);
  43. for (auto &c : S)
  44. total_count[map(c)]++;
  45.  
  46. vector<int> positions[52];
  47. for (int i = 0; i < int(S.size()); ++i)
  48. positions[map(S[i])].push_back(i);
  49.  
  50. for (int i = 0; i < Q; ++i) {
  51. int64_t N; assert(cin >> N);
  52. assert(1 <= N && N <= 1000LL * 1000 * 1000 * 1000 * 1000 * 1000);
  53. --N;
  54.  
  55. int64_t part = N / K;
  56. int index = N % K;
  57. char answer;
  58. if (bits(part) % 2)
  59. answer = rev(S[index]);
  60. else
  61. answer = S[index];
  62.  
  63. int64_t many = 0;
  64. int64_t normal = part / 2, reversed = part / 2;
  65. if (part % 2) {
  66. if (bits(part - 1) % 2)
  67. ++reversed;
  68. else
  69. ++normal;
  70. }
  71. many += normal * total_count[map(answer)];
  72. many += reversed * total_count[map(rev(answer))];
  73. char search_for = S[index];
  74. many += upper_bound(positions[map(search_for)].begin(), positions[map(search_for)].end(), index) - positions[map(search_for)].begin();
  75. cout << answer << " " << many << "\n";
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement