fork download
  1. #include<stdio.h>
  2. #include<string>
  3. #include<vector>
  4. #include<iostream>
  5. typedef long long int lli;
  6.  
  7. using namespace std;
  8. const long long int K = 1000000007LL;
  9.  
  10. lli power(lli base, lli exponent)
  11. {
  12. lli result=1;
  13. while(exponent)
  14. {
  15. if(exponent & 1)
  16. result=(result*base)%K;
  17. exponent>>=1;
  18. base=(base*base)%K;
  19. }
  20. return result%K;
  21. }
  22. int main()
  23. {
  24. string n;
  25. cin>>n;
  26. lli k;
  27. cin>>k;
  28. vector<int> position;
  29. for(int i=0;i<n.length();i++)
  30. if(n[i]=='5' || n[i]=='0')
  31. position.push_back(i);
  32. lli m=0;
  33. for(int i=0;i<position.size();i++)
  34. m=(m+power(2,position[i]))%K;
  35. lli answer=0;
  36. lli l=n.length();
  37.  
  38. // the following is finding summation via loop
  39. for(int i=1;i<=k;i++)
  40. answer=(answer + (power(2,l*(k-i))*m)%K)%K;
  41. cout<<answer<<endl;
  42.  
  43. // the following finds the sum by using gp formula (1st_term*(ratio^no_of_terms-1)/(ratio-1))
  44. answer=1;
  45. answer=((power(power(2,l),k) - 1)/(power(2,l)-1))%K;
  46. answer=(answer*m)%K;
  47. cout<<answer<<endl;
  48. return 0;
  49. }
Time limit exceeded #stdin #stdout 5s 2900KB
stdin
Standard input is empty
stdout

Standard output is empty