DSA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.8 KiB

  1. /*
  2. #pragma GCC target ("avx2")
  3. #pragma GCC optimize ("Ofast")
  4. #pragma GCC optimize ("unroll-loops")
  5. */
  6. #include "bits/stdc++.h"
  7. #define iosb ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
  8. #define BIT(x) __builtin_popcount(x)
  9. #define all(x) x.begin() , x.end()
  10. #define F first
  11. #define S second
  12. #define pb push_back
  13. using namespace std ;
  14. typedef unsigned long long UL ;
  15. typedef long long L ;
  16. typedef string T ;
  17. typedef int I ;
  18. const I MaxN = 1e5+1 ;
  19. const I MOD = 1e9+7 ;
  20. const I inf = 2e9+7 ;
  21. const L INF = 2e18+7 ;
  22. L ans[3001], anss, ansss, f[3001], pascal[3001][3001] ;
  23. L binpow (L a, L n) {
  24. if (n == 0)
  25. return 1;
  26. if (n % 2 == 1)
  27. return (binpow (a, n-1) * a)%MOD;
  28. else {
  29. L b = binpow (a, n/2);
  30. return (b * b) % MOD;
  31. }
  32. }
  33. L cnk(L n, L k)
  34. {
  35. return (f[n] * binpow(f[k]*f[n-k]%MOD,MOD-2)) % MOD ;
  36. }
  37. int32_t main()
  38. {
  39. //freopen(".in" , "r" , stdin) ;
  40. //freopen(".out" , "w" , stdout) ;
  41. pascal[1][1] = 1 ;
  42. for(int i = 1 ; i <= 3000 ; ++i)
  43. {
  44. for(int j = 1 ; j <= i ; ++j)
  45. {
  46. if(j == 1 || j == i)
  47. pascal[i][j] = 1 ;
  48. else
  49. pascal[i][j] = pascal[i-1][j-1]+pascal[i-1][j], pascal[i][j] %= MOD ;
  50. }
  51. }
  52. f[0] = 1 ;
  53. for(L i = 1 ; i <= 3000 ; ++i)
  54. f[i] = i * f[i-1], f[i] %= MOD ;
  55. string s ;
  56. L c ;
  57. cin >> s >> c ;
  58. int cnt = 0 ;
  59. for(int i = 0 ; i < s.size() ; ++i)
  60. if(s[i] == '1')
  61. cnt ++ ;
  62. for(L i = 1, j = cnt-1; i <= s.size() ; i++)
  63. {
  64. ans[i] = c ;
  65. for(L j = 1 ; j < i ; ++j)
  66. {
  67. ans[i]+=binpow(c, j)*pascal[i][j+1];
  68. ans[i]%=MOD;
  69. }
  70. if(s[s.size()-i] == '1')
  71. anss+=ans[i]*binpow(c, j--), anss %= MOD ;
  72. }
  73. cout << anss+1 ;
  74. }