令
fi,j=(i在j进制下回文)?j:1
求
∑Ri=L∑rj=lf(i,j)
1<=L,R<=1e18,2<=j<=36
数位dp,
由于j进制的长度为l的回文串长度能直接算出来
(j−1)j⌊l+12⌋
,所以不用在数位dp时考虑前导0
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<vector>
#include<set>
#include<string>
#include<queue>
#include<complex>
#include<stack>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a+b);}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
bool check();
int T;
#define MAXN (50)
struct FastIO {
static const int S = 5 << 20; //MB
int wpos; char wbuf[S];
FastIO() : wpos(0) {}
inline int xchar() {
static char buf[S];
static int len = 0, pos = 0;
if(pos == len)
pos = 0, len = fread(buf, 1, S, stdin);
if(pos == len) return -1;
return buf[pos ++];
}
inline int xuint() {
int c = xchar(), x = 0;
while(~c && c <= 32) c = xchar();
for(; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
return x;
}
inline int xint() {
int s = 1, c = xchar(), x = 0;
while(c <= 32) c = xchar();
if(c == '-') s = -1, c = xchar();
for(; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
return x * s;
}
inline void xstring(char* s) {
int c = xchar();
while(c <= 32) c = xchar();
for(; c > 32; c = xchar()) * s++ = c;
*s = 0;
}
inline void wchar(int x) {
if(wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
wbuf[wpos ++] = x;
}
inline void wint(ll x) {
if(x < 0) wchar('-'), x = -x;
char s[24];
int n = 0;
while(x || !n) s[n ++] = '0' + x % 10, x /= 10;
while(n--) wchar(s[n]);
}
inline void wstring(const char* s) {
while(*s) wchar(*s++);
}
~FastIO() {
if(wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
}
} io;
int a[MAXN];
ll f[MAXN][MAXN][2];
int n,k;
//first i,j tian j not qiandao0, y is==n
ll calc() {
ll ans=0;
Rep(i,n+1) Fork(j,i,i) Rep(y,2) f[i][j][y]=0;
f[0][0][1]=1;
Rep(i,n) {
int j=i; Rep(y,2) {
if (f[i][j][y]) {
Rep(t,k) {
int nj=(!t&&!j)?(0):(j+1);
int ny=(y&&t==a[i+1]);
if (y&&t>a[i+1]) continue;
if (j>=n-i) continue;
f[i+1][nj][ny]+=f[i][j][y];
}
}
}
}
For(i,n) {
int j=i;
Rep(y,2) {
if (j>=n-i) {
ans+=f[i][j][y];
}
}
}
if (!check()) ans--;
For(i,n-1) {
int len=(i-1)/2;
ll p=k-1;
For(i,len) p*=k;
ans+=p;
}
return ans;
}
bool check() {
Fork(i,(n+1)/2+1,n) {
if (a[i]>a[n-i+1]) return 1;
else if (a[i]<a[n-i+1]) return 0;
}
return 1;
}
int make_k(ll n,ll k){
int len=0;
while(n) a[++len]=n%k,n/=k;
For(i,len/2) swap(a[i],a[len-i+1]);
if (!len) a[len=1]=0;
return len;
}
int main()
{
// freopen("G.in","r",stdin);
// freopen("G.out","w",stdout);
int T=io.xuint();
For(kcase,T) {
ll ANS=0,L=io.xint()-1,R=io.xint(),l=io.xint(),r=io.xint();
for(k=l;k<=r;k++) {
n=make_k(R,k);
ll p=calc();
n=make_k(L,k);
ll q=calc();
ANS+=R-L+(k-1)*(p-q);
}
io.wstring("Case #");
io.wint(kcase);
io.wstring(": ");
io.wint(ANS);
io.wchar('\n');
}
return 0;
}