Problem:
Digit
For a positive integer
a
, let
S(a)
be the sum of the digits in base
l
.
Also let
L(a)
be the minimum
k
such that
S^k(a)
is less than or equal to
l-1
.
Find the minimum
a
such that
L(a) = N
for a given
N
, and print
a
modulo
m
.
Input
The input contains several test cases, followed by a line containing "0 0 0".
Each test case is given by a line with three integers
N
,
m
,
l
(
0 \leq N \leq 10^5
,
1 \leq m \leq 10^9
,
2 \leq l \leq 10^9
).
Output
For each test case, print its case number and the minimum
a
modulo
m
as described above.
Sample Input
0 1000 10
1 1000 10
0 0 0
Output for the Sample Input
Case 1: 1
Case 2: 10

Submitted solutions:

code_snippet: // AOJ #2392 Digit
// 2025.4.6

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
    int n = 0; int c = gc();
    do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
    return n;
}

void Cout(int n) {
    char b[30];
    if (!n) pc('0');
    else {
        if (n < 0) pc('-'), n = -n;
        int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
        while (i--) pc(b[i]);
    }
}

void Couts(const string &s) { for (char c : s) pc(c); }

ll gcd(ll a, ll b) {
    ll r;
    while (b != 0) r = a % b, a = b, b = r;
    return a;
}

ll eulerPhi(ll N) {
    ll res = N;
    for (ll i = 2; i * i <= N; i++) {
        if (N % i == 0) {
            while (N % i == 0) N /= i;
            res -= res / i;
        }
    }
    if (N > 1) res -= res / N;
    return res;
}

__int128 modPow(__int128 base, ll exp, ll mod) {
    __int128 res = 1;
    base %= mod;
    while(exp > 0) {
        if(exp & 1) res = (res * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return res;
}

ll geomSum(ll a, ll n, ll mod) {
    ll newMod = mod * (a - 1);
    __int128 p = modPow(a, n, newMod);
    ll sum = (ll)((p - 1) / (a - 1));
    return sum % mod;
}

pair<ll, ll> splitMod(ll M, ll L) {
    ll cp = 1, np = 1;
    for (ll i = 2; i * i <= M; i++) {
        if (M % i == 0) {
            ll fac = 1;
            while (M % i == 0) {
                fac *= i;
                M /= i;
            }
            if (gcd(L - 1, i) == 1) cp *= fac;
            else np *= fac;
        }
    }
    if (M > 1) {
        if (gcd(L - 1, M) == 1) cp *= M;
        else np *= M;
    }
    return {cp, np};
}

struct State { ll mp, non; };

ll calc(ll N, ll L, ll M) {
    vector<State> st(N + 1);
    vector<ll> modVal(N + 1, 0);
    vector<ll> U(N + 1, 0);

    st[N].mp = M;
    st[N].non = 1;

    for (ll i = N; i >= 2; i--) {
        ll mp = st[i].mp, non = st[i].non;
        if (mp != 1) {
            auto sp = splitMod(mp, L);
            mp = sp.first;
            non = non * sp.second;
        }
        modVal[i] = mp * non;
        st[i - 1].mp = eulerPhi(mp);
        st[i - 1].non = non;
    }
    U[1] = 1;
    for (ll i = 2; i <= N; i++) {
        U[i] = (2 * geomSum(L, U[i - 1], modVal[i])) % modVal[i];
        if (U[i] == 0)
            U[i] = modVal[i];
    }
    return U[N];
}

int main(){
    ll N, M, L;
    int cn = 0;
    while (true) {
        int N = Cin(), M = Cin(), L = Cin();
        if (M == 0) break;
        ll ans;
        if (N == 0) ans = 1 % M;
        else if (N == 1) ans = L % M;
        else {
            ll t = calc(N, L, M);
            ans = (((L - 1) % M) * (t % M)) % M;
            ans = (ans + 1) % M;
        }
        Couts("Case "), Cout(++cn), Couts(": "), Cout(ans), pc('\n');
    }
    return 0;
}
accuracy: 1
time_ms: 750
memory_kb: 6124
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
using vvll=vector<vll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)

ll euler_phi(ll N){
    ll CN=N;
    vll P;
    for(ll i=2;i*i<=N;i++)if(N%i==0){
        P.push_back(i);
        while(N%i==0)N/=i;
    }

    if(N!=1)P.push_back(N);
    for(auto p:P){
        CN/=p;
        CN*=(p-1);
    }
    return CN;
}

using LL=__int128_t;
LL modpow(LL a,ll n,LL mod){
    if(n==0)return 1;
    else if(n%2==1){
        return (a*modpow(a,n-1,mod)%mod);
    }
    LL t=modpow(a,n/2,mod);
    return (t*t)%mod;
}

//(a^n-1)/(a-1) modulo mod
ll geom_sum(ll a,ll n,ll mod){
    if(n==0)return 0;
    return (ll(modpow(a,n,LL(mod)*LL(a-1))-1)/(a-1))%mod;
}

// Mを, Lと互いに素のパートとそれ以外に分ける
pair<ll,ll> split(ll M,ll L){
    ll res=1,fes=1;
    for(ll i=2;i*i<=M;i++)if(M%i==0){
        ll d=1;
        while(M%i==0){
            d*=i;
            M/=i;
        }
        (gcd(L-1,i)==1?res:fes)*=d;
    }
    (gcd(M,L-1)==1?res:fes)*=M;
    return {res,fes};
}

ll calcU(ll N,ll L,ll MP,ll ML=1){
    if(N==1)return 1;
    if(MP!=1){//互いに素パートが残っていたら分割
        pair<ll,ll> p=split(MP,L);
        MP=p.first;
        ML*=p.second;
    }
    ll D=calcU(N-1,L,euler_phi(MP),ML);
    D=geom_sum(L,D,MP*ML);
    D*=2;
    D%=(MP*ML);
    if(D==0)D=MP*ML;
    return D;
}

//M=ML*MP, MPとLは直交
ll calc(ll N,ll L,ll MP,ll ML=1){
    if(N==0)return 1%(ML*MP);
    ll u=calcU(N,L,MP,ML);
    // cout<<u<<" ";
    u*=(L-1);
    u++;
    return u%(MP*ML);
}

ll Case=0;
void solve(ll N,ll M,ll L){
    Case++;
    cout<<"Case "<<Case<<": ";
    cout<<calc(N,L,M,1)<<endl;
    return;
}

int main(){
    ll N,M,L;
    while(cin>>N>>M>>L,N+M+L!=0)solve(N,M,L);
}
accuracy: 1
time_ms: 770
memory_kb: 8128
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
using vvll=vector<vll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)

map<pair<ll,pair<ll,ll>>,vvll> MP;

ll euler_phi(ll N){
    ll CN=N;
    vll P;
    for(ll i=2;i*i<=N;i++){
        if(N%i==0){
            P.push_back(i);
            while(N%i==0)N/=i;
        }
    }
    if(N!=1)P.push_back(N);
    for(auto p:P){
        CN/=p;
        CN*=(p-1);
    }
    return CN;
}

vvll mul(vvll A,vvll B,ll mod){
    ll n=A.size();
    vvll res(n,vll(n,0));
    rep(i,n)rep(j,n){
        rep(k,n){
            res[i][j]+=A[i][k]*B[k][j]%mod;
        }
        res[i][j]%=mod;
    }
    return res;
}

//A^N
vvll matpow(vvll A,ll N,ll mod){
    ll n=A.size();
    ll a=A[0][0];
    vvll E(n,vll(n,0));
    rep(i,n)E[i][i]=1;
    ll pw=0;
    while(N>0){
        if(N%2){
            E=mul(E,A,mod);
        }
        N/=2;
        if(!MP.count({pw+1,{a,mod}})){
            A=mul(A,A,mod);
            MP[{pw+1,{a,mod}}]=A;
        }
        A=MP[{pw+1,{a,mod}}];
        pw++;
    }
    return E;
}

using LL=__int128_t;
LL modpow(LL a,ll n,LL mod){
    if(n==0)return 1;
    else if(n%2==1){
        return (a*modpow(a,n-1,mod)%mod);
    }
    LL t=modpow(a,n/2,mod);
    return (t*t)%mod;
}

ll geom_sumnaive(ll a,ll n,ll mod){
    ll res=0;
    ll d=1;
    for(ll j=0;j<n;j++){
        res+=d;
        d=(d*a)%mod;
    }
    return res%mod;
}

//1+a+a^2+...+a^{n-1} %mod
ll geom_sum(ll a,ll n,ll mod){
    if(n==0)return 0;
    vvll A={{a,1},{0,1}};
    vvll P=matpow(A,n,mod);
    return P[0][1];

}

ll geom_sum2(ll a,ll n,ll mod){
    if(n==0)return 0;
    return (ll(modpow(a,n,LL(mod)*LL(a-1))-1)/(a-1))%mod;
}

pair<ll,ll> split(ll M,ll L){
    ll res=1,fes=1;
    for(ll i=2;i*i<=M;i++){
        if(M%i==0){
            
            ll d=1;
            while(M%i==0){
                d*=i;
                M/=i;
            }
            (gcd(L-1,i)==1?res:fes)*=d;
        }
    }
    (gcd(M,L-1)==1?res:fes)*=M;
    return {res,fes};
}

ll calcU(ll N,ll L,ll MP,ll ML=1){
    if(N==1)return 1;
    if(MP!=1){
        pair<ll,ll> p=split(MP,L);
        // cout<<MP<<" "<<ML<<" "<<L<<" "<<p.first<<" "<<p.second*ML<<endl;
        MP=p.first;
        ML*=p.second;
    }
    ll D=calcU(N-1,L,euler_phi(MP),ML);
    D=geom_sum2(L,D,MP*ML);
    D*=2;
    D%=(MP*ML);
    if(D==0)D=MP*ML;
    return D;
}



//M=ML*MP, MPとLは直交
ll calc(ll N,ll L,ll MP,ll ML=1){
    if(N==0)return 1%(ML*MP);
    ll u=calcU(N,L,MP,ML);
    // cout<<u<<" ";
    u*=(L-1);
    u++;
    return u%(MP*ML);
}

ll Case=0;
void solve(ll N,ll M,ll L){
    Case++;
    cout<<"Case "<<Case<<": ";
    cout<<calc(N,L,M,1)<<endl;
    return;
}

int main(){
    ll N,M,L;
    while(cin>>N>>M>>L,N+M+L!=0)solve(N,M,L);
}
accuracy: 1
time_ms: 750
memory_kb: 8036
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

const long long TEISUU = 100LL;
long long N, M, L;
long long dp1[100009]; // (Answer[i]-1) mod p
long long dp2[100009][69]; // (Answer[i]-1)/(L-1) mod euler(p)
vector<long long> List;
long long Modulo_Precount[69][39];
long long Concat_Precount1[69][39];
long long Concat_Precount2[69][39];

void mod_henkan(long long &a, long long m) {
	if (a >= TEISUU) {
		long long eval = ((a - TEISUU) / m) * m;
		a -= eval;
	}
}

void Initialize() {
	for (int i = 0; i < (int)List.size(); i++) {
		Modulo_Precount[i][0] = L % List[i];
		for (int j = 1; j < 34; j++) {
			Modulo_Precount[i][j] = Modulo_Precount[i][j - 1] * Modulo_Precount[i][j - 1];
			Modulo_Precount[i][j] %= List[i];
		}
	}
	for (int i = 0; i < (int)List.size(); i++) {
		Concat_Precount1[i][0] = L;
		Concat_Precount2[i][0] = 1;
		for (int j = 1; j < 34; j++) {
			Concat_Precount2[i][j] = Concat_Precount1[i][j - 1] * Concat_Precount2[i][j - 1] + Concat_Precount2[i][j - 1];
			Concat_Precount1[i][j] = Concat_Precount1[i][j - 1] * Concat_Precount1[i][j - 1];
			mod_henkan(Concat_Precount2[i][j], List[i]);
			mod_henkan(Concat_Precount1[i][j], List[i]);
		}
	}
}

// a^b mod m
long long modpow(long long a, long long b, long long m, int idx) {
	long long r = 1;
	for (int i = 0; i < 32; i++) {
		if ((b & (1LL << i)) != 0) { r *= Modulo_Precount[idx][i]; r %= m; }
	}
	return r;
}

// 1111111111111...111111111111 mod m
long long Concatenate(long long a, long long m, int idx) {
	long long ret = 0;
	for (int i = 0; i < 32; i++) {
		if ((a & (1LL << i)) != 0) {
			ret = (ret * Concat_Precount1[idx][i] + Concat_Precount2[idx][i]);
			mod_henkan(ret, m);
		}
	}
	return ret;
}

long long modpow_Slow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 34; i++) {
		if ((b & (1LL << i)) != 0) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}

// 1111111111111...111111111111 mod m
long long Concatenate_Slow(long long a, long long m, long long base) {
	long long ret = 0;
	long long a1 = base, a2 = 1;
	for (int i = 0; i < 34; i++) {
		if ((a & (1LL << i)) != 0) {
			ret = (ret * a1 + a2);
			mod_henkan(ret, m);
		}
		a2 = (a1 * a2 + a2); mod_henkan(a2, m);
		a1 = (a1 * a1); mod_henkan(a1, m);
	}
	return ret;
}

long long Euler(long long n) {
	long long r = n;
	long long cur = n;
	for (int i = 2; i * i <= n; i++) {
		if (cur % i != 0) continue;
		r = (1LL * r * (i - 1)) / (1LL * i);
		while (cur % i == 0) { cur /= i; }
	}
	if (cur >= 2) {
		r = (1LL * r * (cur - 1)) / cur;
	}
	return r;
}

long long GCD(long long a, long long b) {
	if (b == 0) return a;
	return GCD(b, a % b);
}

vector<int> yakusuu(int n) {
	vector<int> ret;
	for (int i = 1; i * i <= n; i++) {
		if (n % i != 0) continue;
		ret.push_back(i);
		if (i != n / i) ret.push_back(n / i);
	}
	sort(ret.begin(), ret.end());
	return ret;
}

long long solve() {
	if (N == 0) return 1;
	if (N == 1) return L % M;
	if (N == 2) return (2LL * L - 1LL) % M;

	// Prepare
	long long mod1 = M, cx = Euler(M); List.clear(); List.push_back(M);
	while (true) {
		List.push_back(cx);
		long long val1 = 2LL * Concatenate_Slow(TEISUU + 0, cx, L);
		long long val2 = 2LL * Concatenate_Slow(TEISUU + cx, cx, L);
		if (val1 == val2) {
			break;
		}
		else {
			vector<int> tmp = yakusuu(Euler(cx));
			for (int i = 0; i < (int)tmp.size(); i++) {
				long long val3 = 2LL * modpow_Slow(L, TEISUU + 0, cx);
				long long val4 = 2LL * modpow_Slow(L, TEISUU + tmp[i], cx);
				if (val3 == val4) {
					long long val5 = 2LL * Concatenate_Slow(TEISUU + tmp[i], cx, L);
					long long diff = abs(val1 - val5) % cx;
					long long bairitsu = cx / GCD(cx, diff);
					cx = tmp[i] * bairitsu;
					break;
				}
			}
		}
	}
	Initialize();

	// Initialize
	for (int i = 0; i <= N; i++) {
		dp1[i] = 0;
		for (int j = 0; j < (int)List.size(); j++) dp2[i][j] = 0;
	}
	dp1[1] = L % mod1;
	for (int i = 1; i < (int)List.size(); i++) dp2[1][i] = 1;

	// Dynamic Programming
	for (int i = 2; i <= N; i++) {
		dp1[i] = 2LL * modpow(L, dp2[i - 1][1], mod1, 0) - 1LL;
		dp1[i] = (dp1[i] + mod1) % mod1;
		for (int j = 1; j < (int)List.size(); j++) {
			int mod_nex = 1; if (j + 1 < (int)List.size()) mod_nex = List[j + 1];
			int nex_idx = j; if (j + 1 < (int)List.size()) nex_idx = j + 1;
			dp2[i][j] = 2LL * Concatenate(dp2[i - 1][nex_idx], List[j], j);
			mod_henkan(dp2[i][j], List[j]);
		}
	}
	return dp1[N];
}

int main() {
	// FILE* out = freopen("in1.txt", "w", stdout);
	for (int testcase = 1; testcase <= 869120; testcase++) {
		cin >> N >> M >> L;
		if (N + M + L == 0) break;
		long long ans = solve();
		cout << "Case " << testcase << ": " << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 490
memory_kb: 58096
final_rank: 7

code_snippet: #include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

inline int safeAdd(int a, int b, int m) {
	if ((long long)a + b >= m) {
		return m + ((long long)a + b) % m;
	} else {
		return a + b;
	}
}

inline int safeMul(int a, int b, int m) {
	if ((long long)a * b >= m) {
		return m + (long long)a * b % m;
	} else {
		return a * b;
	}
}

int powSum(int x, int n, int MOD) {
	if (n == 0) {
		return 0;
	}
	int ret = 0, s = 1, t = 1;
	while (n) {
		if (n & 1) {
			ret = safeAdd(ret, safeMul(s, t, MOD), MOD);
			s = safeMul(s, x, MOD);
		}
		t = safeMul(t, safeAdd(x, 1, MOD), MOD);
		x = safeMul(x, x, MOD);
		n >>= 1;
	}
	return ret;
}

int phi(int n) {
	int ret = 1;
	for (int i = 2; i * i <= n; ++i) {
		if (n % i == 0) {
			ret *= (i - 1);
			n /= i;
			while (n % i == 0) {
				ret *= i;
				n /= i;
			}
		}
	}
	if (n != 1) {
		ret *= (n - 1);
		n = 1;
	}
	return ret;
}

int l;

int getb(int n, int mp, int mq) {
	int m = mp * mq;
	if (n == 1) {
		return 1;
	}
	do {
		int g = __gcd(mq, l - 1);
		if (g == 1) {
			break;
		}
		mp *= g, mq /= g;
	} while (true);
	int bn1;
	if (mq == 1) {
		bn1 = 1;
		for (int i = 2; i < n; ++i) {
			int last = bn1;
			bn1 = safeMul(2, powSum(l, bn1, m), m);
			if (bn1 == last) {
				break;
			}
		}
	} else {
		bn1 = getb(n - 1, mp, phi(mq));
	}
	return safeMul(2, powSum(l, bn1, m), m);
}

int main() {
	int n, m;
	while (scanf("%d%d%d", &n, &m, &l) == 3 && l) {
		int ans;
		if (n == 0) {
			ans = 1 % m;
		} else if (n == 1) {
			ans = l % m;
		} else {
			int bn = getb(n, 1, m);
			ans = ((long long)(l - 1) * bn + 1) % m;
		}
		static int id = 0;
		printf("Case %d: %d\n", ++id, ans);
	}
	return 0;
}
accuracy: 1
time_ms: 810
memory_kb: 1188
final_rank: 2

code_snippet: #include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

inline int safeAdd(int a, int b, int m) {
	if ((long long)a + b >= m) {
		return m + ((long long)a + b) % m;
	} else {
		return a + b;
	}
}

inline int safeMul(int a, int b, int m) {
	if ((long long)a * b >= m) {
		return m + (long long)a * b % m;
	} else {
		return a * b;
	}
}

int powSum(int x, int n, int MOD) {
	if (n == 0) {
		return 0;
	}
	int ret = 0, s = 1, t = 1;
	while (n) {
		if (n & 1) {
			ret = safeAdd(ret, safeMul(s, t, MOD), MOD);
			s = safeMul(s, x, MOD);
		}
		t = safeMul(t, safeAdd(x, 1, MOD), MOD);
		x = safeMul(x, x, MOD);
		n >>= 1;
	}
	return ret;
}

int phi(int n) {
	int ret = 1;
	for (int i = 2; i * i <= n; ++i) {
		if (n % i == 0) {
			ret *= (i - 1);
			n /= i;
			while (n % i == 0) {
				ret *= i;
				n /= i;
			}
		}
	}
	if (n != 1) {
		ret *= (n - 1);
		n = 1;
	}
	return ret;
}

int l;

int getb(int n, int mp, int mq) {
	int m = mp * mq;
	if (n == 1) {
		return 1;
	}
	do {
		int g = __gcd(mq, l - 1);
		if (g == 1) {
			break;
		}
		mp *= g, mq /= g;
	} while (true);
	int bn1;
	if (mq == 1) {
		bn1 = 1;
		for (int i = 2; i < n; ++i) {
			bn1 = safeMul(2, powSum(l, bn1, m), m);
		}
	} else {
		bn1 = getb(n - 1, mp, phi(mq));
	}
	return safeMul(2, powSum(l, bn1, m), m);
}

int main() {
	int n, m;
	while (scanf("%d%d%d", &n, &m, &l) == 3 && l) {
		int ans;
		if (n == 0) {
			ans = 1 % m;
		} else if (n == 1) {
			ans = l % m;
		} else {
			int bn = getb(n, 1, m);
			ans = ((long long)(l - 1) * bn + 1) % m;
		}
		static int id = 0;
		printf("Case %d: %d\n", ++id, ans);
	}
	return 0;
}
accuracy: 1
time_ms: 1150
memory_kb: 1188
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef long long ll;
ll a[100010];
ll b[100010];
ll c[100010];
ll s[100010];
ll m[100010];
ll gcd(ll x,ll y){
	if(y==0) return x;
	return gcd(y,x%y);
}
ll ex(ll x,ll p,ll mod){
	ll a=1;
	while(p){
		if(p%2) a=a*x%mod;
		x=x*x%mod;
		p/=2;
	}
	return a;
}
ll geo(ll x,ll p,ll mod){	//1+x+..+x^(p-1)
	if(p==0) return 0;
	ll a=0,s=1,t=1;
	while(p){
		if(p%2){
			a=(a+s*t)%mod;
			s=s*x%mod;
		}
		t=t*(1+x)%mod;
		x=x*x%mod;
		p/=2;
	}
	return a;
}
typedef pair<int,int> P;
ll getord(ll M,ll a){
	ll H=sqrt(M)+1;
	vector<P> babies;
	ll p=1;
	rep(i,H){
		babies.pb(P(p,i));
		p=p*a%M;
	}
	ll q=p;
	sort(all(babies));
	rep1(i,H){
		int pos=upper_bound(all(babies),P(p,H))-babies.begin()-1;
		if(pos>=0&&babies[pos].fs==p) return i*H-babies[pos].sc;
		p=p*q%M;
	}
}
ll pre,parg;
void getnext(ll M,ll L,int id){
	ll p=L;
	ll gs[101];
	gs[0]=1;
	rep(i,100){
		gs[i+1]=gcd(p,M);
		if(gs[i]==gs[i+1]){
			a[id]=i;
			s[id]=geo(L,i,M);
			ll g=gs[i];
			c[id]=ex(L,i,M);
			M/=g;
			break;
		}
		p=p*L%M;
	}
	if(M==pre){
		m[id]=parg;
		return;
	}
	ll o=getord(M,L);
	ll ss=geo(L,o,M);
	m[id]=M/gcd(M,ss)*o;
	pre=M,parg=m[id];
}
ll getans(ll b,ll M,ll L){
	return (2*ex(L,b,M)+M-1)%M;
}
int solve(ll N,ll M,ll L){
	pre=-1;
	if(N==0) return 1;
	if(N==1) return L%M;
	if(N==2) return getans(1,M,L);
	if(N==3) return getans(2,M,L);
	if(N==4) return getans(2*(L+1),M,L);
	N++;
	m[N]=M;
	for(ll x=N-1;x>=4;x--){
		getnext(m[x+1],L,x);
	}
	b[4]=2*(L+1)%m[4];
	for(ll x=4;x<N;x++){
		ll p=(b[x]-a[x])%m[x];
		if(p<0) p+=m[x];
		b[x+1]=2*(s[x]+c[x]*geo(L,p,m[x+1]))%m[x+1];
	}
	return (b[N]*(L-1)+1)%M;
}
int main(){
	for(int t=1;;t++){
		ll N,M,L;
		cin>>N>>M>>L;
		if(M==0) break;
		printf("Case %d: %d\n",t,solve(N,M,L));
	}
}
accuracy: 1
time_ms: 400
memory_kb: 5524
final_rank: 1