这题和spoj 2319很相像,都是数位dp+大数。
spoj 2319可以参考论文——2009 - 刘聪《浅谈数位类统计问题》。
读入n,m,k,还有一个大数
dp[i][j](1<=i<=n,0<=j<k)表示用i位表示的数模k余j的数的个数
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <cstdlib>
#include <map>
#define pb push_back
#define eps 1e-9
#define zero(x) (fabs(x)<eps)
#define pi acos(-1.0)
#define MS(x, y) memset(x, y, sizeof(x))
#define FOR(i, x, y) for(int i=x; i<=y; i++)
#define rFOR(i, x, y) for(int i=x; i>=y; i--)
using namespace std;
const int maxn = 175;
struct bigint
{
int s[maxn];
bigint() {MS(s, 0);}
bigint(int num) {*this = num;}
bigint(const char* num){*this = num;}
bigint operator = (const char* num)
{
MS(s, 0);
if(num[0] == '-')
{
num = num + 1;
s[0] = -1;
}
else s[0] = 1;
while(num[0] == '0') num = num + 1;
s[0] = s[0] * strlen(num);
int len = abs(s[0]);
FOR(i, 1, len) s[i] = num[len - i] - 48;
return *this;
}
bigint operator = (int num)
{
char s[maxn];
sprintf(s, "%d", num);
*this = s;
return *this;
}
string str() const
{
string res = "";
FOR(i, 1, abs(s[0])) res = (char)(s[i] + 48) + res;
if(res == "") return res = "0";
if(s[0] < 0) res = '-' + res;
return res;
}
bool operator < (const bigint& b) const
{
if(s[0] != b.s[0]) return s[0] < b.s[0];
int len = abs(s[0]);
rFOR(i, len, 1)
if(s[i] != b.s[i])
return (s[i] < b.s[i]) ^ (s[0] < 0);
return false;
}
bool operator > (const bigint& b) const{return b < *this;}
bool operator <= (const bigint& b) const{return !(b < *this);}
bool operator >= (const bigint& b) const{return !(*this < b);}
bool operator != (const bigint& b) const{return b < *this || *this < b;}
bool operator == (const bigint& b) const{return !(b < *this) && !(*this < b);}
friend bigint abs(bigint b)
{
b.s[0] = abs(b.s[0]);
return b;
}
friend bigint _add(const bigint& a, const bigint& b)
{
bigint c;
c.s[0] = max(a.s[0], b.s[0]);
FOR(i, 1, c.s[0]) c.s[i] = a.s[i] + b.s[i];
FOR(i, 1, c.s[0])
if(c.s[i] >= 10)
{
c.s[i+1]++;
c.s[i]-=10;
}
if(c.s[c.s[0]+1]) ++c.s[0];
return c;
}
friend bigint _sub(const bigint& a, const bigint& b)
{
bigint c;
c.s[0] = a.s[0];
FOR(i, 1, c.s[0]) c.s[i] = a.s[i] - b.s[i];
FOR(i, 1, c.s[0])
if(c.s[i] < 0)
{
c.s[i+1]--;
c.s[i] += 10;
}
while(!c.s[c.s[0]] && c.s[0]) --c.s[0];
return c;
}
bigint operator + (const bigint& b) const
{
if(s[0] >= 0 && b.s[0] >= 0) return _add(*this, b);
if(b.s[0] < 0) return *this - abs(b);
if(s[0] < 0) return b - abs(*this);
}
bigint operator - (const bigint& b) const
{
if(s[0] >= 0 && b.s[0] >= 0)
{
bigint c;
if(*this >= b) return _sub(*this, b);
c = _sub(b, *this);
c.s[0] = -c.s[0];
return c;
}
if(b.s[0] < 0) return *this + abs(b);
if(s[0] < 0)
{
bigint c;
c = abs(*this) + b;
c.s[0] = -c.s[0];
return c;
}
}
bigint operator * (const bigint& b) const
{
bigint c;
c.s[0] = abs(s[0]) + abs(b.s[0]);
FOR(i, 1, abs(s[0]))
FOR(j, 1, abs(b.s[0]))
c.s[i + j - 1] += s[i] * b.s[j];
FOR(i, 1, c.s[0])
{
c.s[i+1] += c.s[i] / 10;
c.s[i] %= 10;
}
while(!c.s[c.s[0]] && c.s[0]) --c.s[0];
if((s[0] > 0) != (b.s[0] > 0)) c.s[0] = -c.s[0];
return c;
}
bigint operator / (const bigint& _b) const
{
bigint c, t;
bigint b = abs(_b);
c.s[0] = abs(s[0]);
rFOR(i, c.s[0], 1)
{
rFOR(j, t.s[0], 1) t.s[j + 1] = t.s[j];
t.s[1] = s[i];
if (t.s[t.s[0]+1])t.s[0]++;
while(t >= b)
{
++c.s[i];
t -= b;
}
}
while(!c.s[c.s[0]] && c.s[0]) --c.s[0];
if((s[0] > 0) != (b.s[0] > 0)) c.s[0] = -c.s[0];
return c;
}
bigint operator % (const bigint& _b) const
{
bigint c, t;
bigint b = abs(_b);
rFOR(i, abs(s[0]), 1)
{
rFOR(j, t.s[0], 1) t.s[j + 1] = t.s[j];
t.s[1] = s[i];
if (t.s[t.s[0]+1])t.s[0]++;
while(t >= b) t -= b;
}
if((s[0] < 0)) t.s[0] = -t.s[0];
return t;
}
bigint operator += (const bigint& b) {*this = *this + b;return *this;}
bigint operator -= (const bigint& b) {*this = *this - b;return *this;}
bigint operator *= (const bigint& b) {*this = *this * b;return *this;}
bigint operator /= (const bigint& b) {*this = *this / b;return *this;}
bigint operator %= (const bigint& b) {*this = *this % b;return *this;}
friend bigint operator + (int& a, const bigint& b){return (bigint)a + b;}
friend bigint operator - (int& a, const bigint& b){return (bigint)a - b;}
friend bigint operator * (int& a, const bigint& b){return (bigint)a * b;}
friend bigint operator / (int& a, const bigint& b){return (bigint)a / b;}
friend bigint operator % (int& a, const bigint& b){return (bigint)a % b;}
friend bigint operator < (int& a, const bigint& b){return (bigint)a < b;}
friend bigint operator <= (int& a, const bigint& b){return (bigint)a <=b;}
friend bigint operator > (int& a, const bigint& b){return (bigint)a > b;}
friend bigint operator >= (int& a, const bigint& b){return (bigint)a >=b;}
friend bigint operator == (int& a, const bigint& b){return (bigint)a ==b;}
friend bigint operator != (int& a, const bigint& b){return (bigint)a !=b;}
};
istream& operator >> (istream &in, bigint& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream &out, const bigint& x)
{
out << x.str();
return out;
}
using namespace std;
int l,m,k;
bigint x;
bigint dp[101][50];
void doit()
{
cin>>x;x+=1;
for (int z=0;z<k;z++)
dp[0][z]=0;
dp[0][0]=1;
for (int i=1;i<=l;i++)
{ for (int z=0;z<k;z++)
dp[i][z]=0;
for (int j=1;j<=m;j++)
for (int z=0;z<k;z++)
dp[i][(z+j)%k]+=dp[i-1][z];
}
int w=l,i,p=0;
bool have=0;
bigint tmp;
while (w)
{ for (i=1;i<=m;i++)
{ tmp=dp[w-1][((p-i)%k+k)%k];
if (x<=tmp) break;else x-=tmp;
}
if (have) printf(" "); have=1;
printf("%d",i);
w--;
p=((p-i)%k+k)%k;
}
printf("\n");
}
int main()
{ while (scanf("%d%d%d",&l,&m,&k)!=EOF) doit();
}
/*
3 10 4
213
*/