#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <iomanip> #include <cstring> #include <map> #include <queue> #include <set> #include <cassert> #include <stack> #include <bitset> #include <unordered_set> #define mkp make_pair using namespace std; const double EPS=1e-8; typedef long long lon; const lon SZ=21,SSZ=2*SZ,APB=20,INF=0x7FFFFFFF,mod=1000000007; lon m,n,k,sz,arr[SZ]; int dp[SZ]; void release() { } int dfs(int pos,int lim,bool type) { int up=lim?arr[pos]:9; if(!lim&&dp[pos]!=-1)return dp[pos]; else if(pos==sz-1) { int res=0; for(int i=0;i<=up;++i)res+=i; if(!lim)dp[pos]=res; return res; } else { int res=0; for(int i=0;i<=up;++i) { //res+=i*(pow(k,sz-pos-1)*(sz-pos-1)*(k-1)); int tmp=pow(k,sz-pos-1); if(lim&&i==up)tmp=(type?n:m)%(int)pow(k,sz-pos-1),++tmp; res+=i*tmp; if(sz==2)cout<<res<<endl; res+=dfs(pos+1,lim&&i==up,type); if(sz==2)cout<<res<<endl; } if(!lim)dp[pos]=res; return res; } } int calc(int x,bool type) { memset(dp,-1,sizeof(dp)); sz=0; if(x==0) { arr[sz++]=0; } else for(;x;x/=10)arr[sz++]=x%10; reverse(arr,arr+sz); int res=dfs(0,1,type); return res; } void init() { cin>>m>>n>>k; --m; cout<<calc(n,1)-calc(m,0)<<endl; } void work() { } int main() { //std::ios::sync_with_stdio(0); //freopen("d:\\2.txt","r",stdin); //freopen("d:\\3.txt","w",stdout); lon casenum; //cin>>casenum; //cout<<casenum<<endl; //for(lon time=1;time<=casenum;++time) //for(lon time=1;scanf("%d%d",&n,&m)!=EOF;++time) { init(); work(); release(); } return 0; }