fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define lln long long int
  4.  
  5. lln a,b,ans;
  6. lln potions[1000001];
  7.  
  8. int main()
  9. {
  10. int i;
  11. string visit;
  12. scanf("%lld %lld",&a,&b);
  13. cin>>visit;
  14. lln length=visit.length();
  15. potions[0]=a;
  16. if(visit[0]=='1') ans=(ans+potions[0]%b)%b;
  17. for(i=1; i<length; ++i){
  18. lln temp = potions[i-1];
  19. potions[i]= (temp%b * temp%b)%b;
  20. if(visit[i]=='1') ans=(ans+potions[i]%b)%b;
  21. }
  22. printf("%lld\n",ans%b);
  23.  
  24. return 0;
  25. }
Runtime error #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
Standard output is empty