题目
题解&代码来源标程
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9+7;
const int prime = 13;
int n;
int digit[10][10];
int dp[10][10][14][1<<10];
void Add(int &a,int b){
a += b;
if(a >= MOD)a -= MOD;
}
int dfs(int row,int pos,int dig_sum,int limit){
if(row == n){
row = 0;
pos--;
dig_sum = 0;
if(pos == -1)return 1;
}
if(dp[row][pos][dig_sum][limit] != -1)
return dp[row][pos][dig_sum][limit];
int ans = 0;
int end = ((limit>>row)&1)?digit[row][pos]:prime-1;
for(int i = 0;i <= end;i++){
if(i + dig_sum >= prime)break;
Add(ans,dfs(row+1,pos,dig_sum+i,i == end? limit : (limit & (~(1<<row)))));
}
dp[row][pos][dig_sum][limit] = ans;
return ans;
}
int x[10];
int calc(){
memset(digit,0,sizeof(digit));
memset(dp,-1,sizeof(dp));
int cnt = 0;
for(int i = 0;i < n;i++){
int pos = 0;
do{
digit[i][pos++] = x[i]%prime;
x[i] /= prime;
}
while(x[i]);
cnt = max(cnt,pos);
}
return dfs(0,cnt-1,0,(1<<n)-1);
}
int main(){
int T;
int iCase = 0;
scanf("%d",&T);
while(T--){
iCase++;
scanf("%d",&n);
long long tot = 1;
for(int i = 0;i < n;i++){
scanf("%d",&x[i]);
tot = tot*(x[i]+1)%MOD;
}
tot -= calc();
tot = (tot%MOD+MOD)%MOD;
printf("Case #%d: %d\n",iCase,(int)tot);
}
return 0;
}