fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int k;
  7.  
  8. long long pot(const long long & a, long long n)
  9. {
  10. if (n == 1)
  11. return a;
  12. if (n % 2)
  13. {
  14. long long s = pot(a, n - 1);
  15. return (a * s) % k;
  16. }
  17. else
  18. {
  19. long long s = pot(a, n / 2);
  20. return (s * s) % k;
  21. }
  22. }
  23.  
  24. int main()
  25. {
  26. std::ios_base::sync_with_stdio(0);
  27.  
  28. int n;
  29. string str;
  30. long long licznik = 0;
  31.  
  32. cin >> n >> k;
  33.  
  34. cin >> str;
  35.  
  36. for (int i = str.size() - 2; i > 0; --i) // inicjacja
  37. if (str[i] == '1')
  38. ++licznik;
  39.  
  40. if (licznik)
  41. cout << pot(2LL, licznik) << endl;
  42. else
  43. cout << 1 << endl;
  44. }
Success #stdin #stdout 0s 16064KB
stdin
7 23
1101001
stdout
4