fork download
  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4. public class Main {
  5. public static void main(String[] args) throws Exception {
  6. InputReader in = new InputReader(System.in);
  7. OutputWriter out = new OutputWriter(System.out);
  8. Task Solver = new Task();
  9. int t = in.readInt();
  10. while(t-- > 0)
  11. Solver.solve(in,out);
  12. out.close();
  13. }
  14. }
  15. class Task{
  16. int n;
  17. String s;
  18. String S[] = new String[100];
  19. BigInteger zero = new BigInteger("0");
  20. BigInteger two = new BigInteger("2");
  21. public void solve(InputReader in, OutputWriter out) {
  22. n = in.readInt();
  23. String t = in.readString();
  24. m = new BigInteger(t);
  25. s = in.readString();
  26. S[0] = "1";
  27. for(int i = 1; i < 100; ++i){
  28. S[i] = S[i - 1] + "0";
  29. }
  30. out.printLine(recur(0,zero));
  31. }
  32. private long recur(int pos, BigInteger val) {
  33. if(pos >= s.length()) {
  34. if(val.mod(m).equals(zero)) {
  35. return 1;
  36. }
  37. else return 0;
  38. }
  39. long ret = 0;
  40. BigInteger new_val = val;
  41. if(s.charAt(s.length() - 1 - pos) != '_') {
  42. int num = s.charAt(s.length() - 1 - pos ) == '1' ? 1 : 0;
  43. if(num == 1) {
  44. BigInteger q = new BigInteger(S[pos],2);
  45. new_val = new_val.or(q);
  46. }
  47. ret += recur(pos + 1, new_val);
  48. }
  49. else {
  50. BigInteger q = new BigInteger(S[pos],2);
  51. new_val = new_val.or(q);
  52. ret += recur(pos + 1, new_val);
  53. ret += recur(pos + 1, val);
  54. }
  55. return ret;
  56. }
  57. }
  58. class InputReader {
  59. private InputStream stream;
  60. private byte[] buf = new byte[1024];
  61. private int curChar;
  62. private int numChars;
  63. private SpaceCharFilter filter;
  64. public InputReader(InputStream stream) {
  65. this.stream = stream;
  66. }
  67. public int read() {
  68. if (numChars == -1) {
  69. throw new InputMismatchException();
  70. }
  71. if (curChar >= numChars) {
  72. curChar = 0;
  73. try {
  74. numChars = stream.read(buf);
  75. } catch (IOException e) {
  76. throw new InputMismatchException();
  77. }
  78. if (numChars <= 0) {
  79. return -1;
  80. }
  81. }
  82. return buf[curChar++];
  83. }
  84. public int readInt() {
  85. int c = read();
  86. while (isSpaceChar(c)) {
  87. c = read();
  88. }
  89. int sgn = 1;
  90. if (c == '-') {
  91. sgn = -1;
  92. c = read();
  93. }
  94. int res = 0;
  95. do {
  96. if (c < '0' || c > '9') {
  97. throw new InputMismatchException();
  98. }
  99. res *= 10;
  100. res += c - '0';
  101. c = read();
  102. } while (!isSpaceChar(c));
  103. return res * sgn;
  104. }
  105. public String readString() {
  106. int c = read();
  107. while (isSpaceChar(c)) {
  108. c = read();
  109. }
  110. StringBuilder res = new StringBuilder();
  111. do {
  112. res.appendCodePoint(c);
  113. c = read();
  114. } while (!isSpaceChar(c));
  115. return res.toString();
  116. }
  117. public double readDouble() {
  118. int c = read();
  119. while (isSpaceChar(c)) {
  120. c = read();
  121. }
  122. int sgn = 1;
  123. if (c == '-') {
  124. sgn = -1;
  125. c = read();
  126. }
  127. double res = 0;
  128. while (!isSpaceChar(c) && c != '.') {
  129. if (c == 'e' || c == 'E') {
  130. return res * Math.pow(10, readInt());
  131. }
  132. if (c < '0' || c > '9') {
  133. throw new InputMismatchException();
  134. }
  135. res *= 10;
  136. res += c - '0';
  137. c = read();
  138. }
  139. if (c == '.') {
  140. c = read();
  141. double m = 1;
  142. while (!isSpaceChar(c)) {
  143. if (c == 'e' || c == 'E') {
  144. return res * Math.pow(10, readInt());
  145. }
  146. if (c < '0' || c > '9') {
  147. throw new InputMismatchException();
  148. }
  149. m /= 10;
  150. res += (c - '0') * m;
  151. c = read();
  152. }
  153. }
  154. return res * sgn;
  155. }
  156. public long readLong() {
  157. int c = read();
  158. while (isSpaceChar(c)) {
  159. c = read();
  160. }
  161. int sgn = 1;
  162. if (c == '-') {
  163. sgn = -1;
  164. c = read();
  165. }
  166. long res = 0;
  167. do {
  168. if (c < '0' || c > '9') {
  169. throw new InputMismatchException();
  170. }
  171. res *= 10;
  172. res += c - '0';
  173. c = read();
  174. } while (!isSpaceChar(c));
  175. return res * sgn;
  176. }
  177.  
  178. public boolean isSpaceChar(int c) {
  179. if (filter != null) {
  180. return filter.isSpaceChar(c);
  181. }
  182. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  183. }
  184. public String next() {
  185. return readString();
  186. }
  187.  
  188. public interface SpaceCharFilter {
  189. public boolean isSpaceChar(int ch);
  190. }
  191. }
  192. class OutputWriter {
  193. private final PrintWriter writer;
  194.  
  195. public OutputWriter(OutputStream outputStream) {
  196. writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  197. }
  198. public OutputWriter(Writer writer) {
  199. this.writer = new PrintWriter(writer);
  200. }
  201. public void print(Object... objects) {
  202. for (int i = 0; i < objects.length; i++) {
  203. if (i != 0) {
  204. writer.print(' ');
  205. }
  206. writer.print(objects[i]);
  207. }
  208. writer.flush();
  209. }
  210. public void printLine(Object... objects) {
  211. print(objects);
  212. writer.println();
  213. writer.flush();
  214. }
  215. public void close() {
  216. writer.close();
  217. }
  218. public void flush() {
  219. writer.flush();
  220. }
  221. }
Success #stdin #stdout 0.1s 36332KB
stdin
1
5
2
_____
stdout
16