题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1230
dp(l,s,ss)表示长度为l的数各位和为s,各位平方和为ss的幸运数的个数。
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std; typedef long long LL;
const int maxn = ;
const int maxa = ;
const int maxm = ;
LL l, r;
LL dp[maxn][maxa][maxm];
int digit[maxn];
bool isprime[maxm]; void printlist() {
memset(isprime, true, sizeof(isprime));
isprime[] = isprime[] = false;
int pedge = int(sqrt(maxm));
for(int i = ; i <= pedge; i++) {
if(isprime[i]) {
int o = maxm / i;
for(int j = ; j <= o; j++) {
isprime[i*j] = false;
}
}
}
} LL dfs(int l, int s, int ss, bool flag) {
if(l == ) return isprime[s] && isprime[ss];
if(!flag && ~dp[l][s][ss]) return dp[l][s][ss];
int pos = flag ? digit[l] : ;
LL ret = ;
for(int i = ; i <= pos; i++) {
ret += dfs(l-, s+i, ss+i*i, flag&&(pos==i));
}
if(!flag) dp[l][s][ss] = ret;
return ret;
} LL f(LL x) {
if(x < ) return ;
int pos = ;
while(x) {
digit[++pos] = x % ;
x /= ;
}
return dfs(pos, , , true);
} int main() {
//freopen("in", "r", stdin);
printlist();
memset(dp, -, sizeof(dp));
int T;
scanf("%d", &T);
while(T--) {
scanf("%lld%lld",&l,&r);
printf("%lld\n", f(r)-f(l-));
}
return ;
}