-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
TerribleMathematics.cpp
76 lines (70 loc) · 1.88 KB
/
TerribleMathematics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma Gpref optimize("O3")
//#pragma Gpref optimize("Ofast")
//#pragma Gpref target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma Gpref optimize("unroll-loops")
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define TC int t; cin >> t; while(t--)
#define rep(i,j,k) for(int i=j; i<k; i++)
#define rep_(i,j,k) for(int i=j; i>=k; --i)
#define pb push_back
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define F first
#define S second
using namespace std;
const int MOD = (int)1e9+7;
const int MX = 105;
const int N=105;
typedef pair<int, int> prs;
int mat[MX][MX];
int cccccc=INT_MIN;
int bitstodec(string n,int m)
{
string num = n;
int dec_value = 0;
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '1')
dec_value += base;
base = base * 2;
base%=m; dec_value%=m;
}
return dec_value%m;
}
int32_t main(){
//freopen("ic1.txt", "r", stdin);
//freopen("oc1.txt", "w", stdout);
IOS;
TC{
int n,k,len,c=0,y,z;
cin >> n >> k;
string s;cin >> s;
len=s.length();
rep(i,0,len){
if(s[i] == '_')c++;
}
int x=pow(2,c);int gg=x;
char bits[gg][c];
rep(i,0,c){
x/=2;y=x,z=x;
rep(j,0,gg){
if(y!=0 && z!=0){bits[j][i]='0';y--;}
else {bits[j][i]='1';z--;}
if(y==0&&z==0){y=x;z=x;}
}
}
int cnt=0;
rep(i,0,gg){
string ss="";int hh=0;
rep(j,0,n){
if(s[j]=='_')ss+=(bits[i][hh++]);else ss+=s[j];
}
if(bitstodec(ss,k)%k==0)cnt++;
}cout<<cnt<<endl;
rep(i,0,gg)fill_n(bits[i],0,c);
}
return 0;
}