#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i,a,n) for (int i=a;i=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define ACCU accumulate #define TWO(x) (1<<(x)) #define TWOL(x) (1ll<<(x)) #define clr(a) memset(a,0,sizeof(a)) #define POSIN(x,y) (0<=(x)&&(x) VI; typedef vector VS; typedef vector VD; typedef long long ll; typedef long double LD; typedef pair PII; typedef pair PLL; typedef vector VL; typedef vector VPII; typedef complex CD; const int inf=0x20202020; const ll mod=1000000007; const double eps=1e-9; const double pi=3.1415926535897932384626; const int DX[]={1,0,-1,0},DY[]={0,1,0,-1}; ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=101000; int sigma[N]; int _,n,m,__; ll dp[N]; int main() { for (int j=1;j*(3*j-1)/2<=50100;j++) { int sgn=(j%2==0?1:-1); sigma[j*(3*j+1)/2]=sigma[j*(3*j-1)/2]=sgn; } for (scanf("%d",&_);_;_--) { scanf("%d%d",&n,&m); dp[0]=1; rep(i,1,n+1) { dp[i]=0; for (int k=1;k*k<=i;k++) dp[i]+=(k%2*2-1)*dp[i-k*k]; dp[i]=(dp[i]*2+sigma[i])%m; } if (dp[n]<0) dp[n]+=m; printf("Case #%d: %d\n",++__,(int)dp[n]); } }