#include<bits/stdc++.h>
#define mxn 20
using namespace std;
typedef long long ll;
ll n,arr[50],b,k;
string s;
ll dp[25][1<<17];
ll Set(ll N,ll pos){return N=N | (1<<pos);}
ll reset(ll N,ll pos){return N= N & ~(1<<pos);}
bool check(ll N,ll pos){return (bool)(N & (1<<pos));}
ll solve(ll value,ll mask)
{
if(mask==(1<<n)-1)
{
if(value==0)
return 1;
else
return 0;
}
if(dp[value][mask]!=-1)
{
return dp[value][mask];
}
ll ans=0;
for(ll i=0;i<n;i++)
{
if(check(mask,i)==0)
{
ans+=solve((value*b+s[i])%k,Set(mask,i));
}
}
return dp[value][mask]=ans;
}
int main()
{
ll i,j,l,m,test;
cin>>test;
for(l=1;l<=test;l++)
{
cin>>b>>k;
cin>>s;
memset(dp,-1,sizeof(dp));
n=s.size();
for(i=0;i<n;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
s[i]=s[i]-'0';
}
else
{
s[i]=s[i]-'A'+10;
}
}
cout<<"Case "<<l<<": "<<solve(0,0)<<endl;
}
return 0;
}
#define mxn 20
using namespace std;
typedef long long ll;
ll n,arr[50],b,k;
string s;
ll dp[25][1<<17];
ll Set(ll N,ll pos){return N=N | (1<<pos);}
ll reset(ll N,ll pos){return N= N & ~(1<<pos);}
bool check(ll N,ll pos){return (bool)(N & (1<<pos));}
ll solve(ll value,ll mask)
{
if(mask==(1<<n)-1)
{
if(value==0)
return 1;
else
return 0;
}
if(dp[value][mask]!=-1)
{
return dp[value][mask];
}
ll ans=0;
for(ll i=0;i<n;i++)
{
if(check(mask,i)==0)
{
ans+=solve((value*b+s[i])%k,Set(mask,i));
}
}
return dp[value][mask]=ans;
}
int main()
{
ll i,j,l,m,test;
cin>>test;
for(l=1;l<=test;l++)
{
cin>>b>>k;
cin>>s;
memset(dp,-1,sizeof(dp));
n=s.size();
for(i=0;i<n;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
s[i]=s[i]-'0';
}
else
{
s[i]=s[i]-'A'+10;
}
}
cout<<"Case "<<l<<": "<<solve(0,0)<<endl;
}
return 0;
}
No comments:
Post a Comment