#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
const int N=35;
int f[N][N];
using namespace std;
int l,r,k,b;
int dp(int n){
vector<int>v;
while(n){
v.push_back(n%b);
n=n/b;
}
int last=0,res=0;
for(int i=v.size()-1;i>=0;i--){
int num=v[i];
if(num>0){
res+=f[i][k-last];
if(num>1){
if(k-last-1>=0) res+=f[i][k-last-1];
break;
}
else {
last++;
if(last>k) break;
}
}
if(!i&&k==last) res++;
}
return res;
}
void init(){
for(int i=0;i<N;i++){
for(int j=0;j<=i;j++){
if(!j) f[i][j]=1;
else f[i][j]=f[i-1][j-1]+f[i-1][j];
}
}
}
int main(){
cin >> l >> r >> k >> b;
init();
cout << dp(r)-dp(l-1);
return 0;
}
参考题解
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=35;
int f[N][N];
int l,r,k,b;
int a[N];
int dfs(int i,int j,bool limit){
if(i==0){
if(j==k) return 1;
else return 0;
}
if(!limit&&f[i][j]!=-1){
return f[i][j];
}
int res=0,m;
if(limit){
m=min(1,a[i]);
}
else {
m=1;
}
for(int op=0;op<=m;op++){
res+=dfs(i-1,j+(op==1),limit&&(op==a[i]));
}
if(!limit){
return f[i][j]=res;
}
else {
return res;
}
}
int dp(int n){
memset(f,-1,sizeof f);
int cnt=0;
while(n) {
a[++cnt]=n%b;
n=n/b;
}
return dfs(cnt,0,1);
}
int main(){
cin >> l >> r >> k >> b;
cout << dp(r)-dp(l-1);
return 0;
}
参考题解
参考题解
参考题解
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=50;
int l,r;
int a[N],f[N][N];
int dfs(int i,int j,bool limit){
if(i==0){
return 1;
}
if(!limit&&f[i][j]!=-1) return f[i][j];
int res=0,m;
if(limit){
m=a[i];
}
else {
m=9;
}
for(int op=0;op<=m;op++){
if(op<j) continue;
res+=dfs(i-1,op,limit&&(op==a[i]));
}
if(limit){
return res;
}
else {
return f[i][j]=res;
}
}
int dp(int n){
int cnt=0;
memset(f,-1,sizeof f);
while(n){
a[++cnt]=n%10;
n=n/10;
}
return dfs(cnt,0,1);
}
int main(){
while(cin >> l >> r){
cout << dp(r)-dp(l-1) << endl;
}
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=35;
int f[N][N][2];
int l,r;
int a[N];
int dfs(int i,int j,bool limit,int lead){
if(i==0) return 1;
if(!limit&&f[i][j][lead]!=-1) return f[i][j][lead];
int res=0,m=(limit)?a[i]:9;
for(int op=0;op<=m;op++){
if(lead||abs(op-j)>1) res+=dfs(i-1,op,limit&&op==a[i],lead&&op==0);
}
return limit?res:f[i][j][lead]=res;
}
int dp(int n){
int cnt=0;
memset(f,-1,sizeof f);
while(n){
a[++cnt]=n%10;
n=n/10;
}
return dfs(cnt,0,1,1);
}
int main(){
cin >> l >> r;
cout << dp(r)-dp(l-1);
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=35;
int f[N][110];
int l,r,mod;
int a[N];
int dfs(int i,int j,int limit){
if(i==0) {
if(j==0) return 1;
else return 0;
}
if(!limit&&f[i][j]!=-1) return f[i][j];
int res=0,m=limit?a[i]:9;
for(int op=0;op<=m;op++){
res+=dfs(i-1,(j+op)%mod,limit&&(op==a[i]));
}
return limit?res:f[i][j]=res;
}
int dp(int n){
memset(f,-1,sizeof f);
int cnt=0;
while(n){
a[++cnt]=n%10;
n=n/10;
}
return dfs(cnt,0,1);
}
int main(){
while(cin>>l>>r>>mod){
cout << dp(r)-dp(l-1) << endl;}
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=35;
int l,r;
int a[N];
int f[N][N];
int dfs(int i,int j,int last,int limit){
if(i==0) return 1;
if(!limit&&f[i][j]!=-1) return f[i][j];
int res=0,m=limit?a[i]:9;
for(int op=0;op<=m;op++){
if(op==4||(op==2&&last==6)) continue;
res+=dfs(i-1,op,op,limit&&a[i]==op);
}
return limit?res:f[i][j]=res;
}
int dp(int n){
int cnt=0;
memset(f,-1,sizeof f);
while(n){
a[++cnt]=n%10;
n=n/10;
}
return dfs(cnt,0,0,1);
}
int main(){
while(cin >>l >> r){
if(l!=0&&r!=0) cout << dp(r)-dp(l-1)<<endl;}
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
const int N=22,mod=1e9+7;
struct node{
int cnt=0,sum,sum2;
}f[N][10][10];
int t,l,r;
int p[N];
int a[N];
node dfs(int i,int j,int k,int limit){
if(i==0){
if(j!=0&&k!=0) return {1,0,0};
else return {0,0,0};
}
if(!limit&&f[i][j][k].cnt!=-1) return f[i][j][k];
node res={0,0,0};
int m=limit?a[i]:9;
for(int op=0;op<=m;op++){
if(op==7) continue;
node tmp=dfs(i-1,(j+op)%7,(k*10+op)%7,limit&&op==a[i]);
(res.cnt+=tmp.cnt)%=mod;
int B=op*p[i-1]%mod;
(res.sum+=tmp.cnt*B%mod+tmp.sum)%=mod;
(res.sum2+=tmp.cnt*B%mod*B%mod+tmp.sum2%mod+2*B%mod*tmp.sum%mod)%=mod;
}
return limit?res:f[i][j][k]=res;
}
int dp(int n){
int cnt=0;
memset(f,-1,sizeof f);
while(n){
a[++cnt]=n%10;
n=n/10;
}
return dfs(cnt,0,0,1).sum2;
}
signed main(){
cin >> t;
p[0]=1;
for(int i=1;i<N;i++) p[i]=(10*p[i-1])%mod;
while(t--){
int l,r;
cin >> l >> r;
cout << (dp(r)-dp(l-1)+mod)%mod << endl;
}
return 0;
}
参考题解
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=35;
int l,r;
struct node{
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,cnt=0;
}f[N][2];
int a[N];
node dfs(int i,int limit,int lead){
if(i==0) return {0,0,0,0,0,0,0,0,0,0,1};
if(!limit&&f[i][lead].a!=-1) return f[i][lead];
node res={0,0,0,0,0,0,0,0,0,0,0};
int m=limit?a[i]:9;
for(int op=0;op<=m;op++){
node tmp=dfs(i-1,limit&&op==a[i],lead&&op==0);
if(op==0&&lead!=1) res.a+=tmp.cnt;
else if(op==1) res.b+=tmp.cnt;
else if(op==2) res.c+=tmp.cnt;
else if(op==3) res.d+=tmp.cnt;
else if(op==4) res.e+=tmp.cnt;
else if(op==5) res.f+=tmp.cnt;
else if(op==6) res.g+=tmp.cnt;
else if(op==7) res.h+=tmp.cnt;
else if(op==8) res.i+=tmp.cnt;
else if(op==9) res.j+=tmp.cnt;
res.a+=tmp.a;
res.b+=tmp.b;
res.c+=tmp.c;
res.d+=tmp.d;
res.e+=tmp.e;
res.f+=tmp.f;
res.g+=tmp.g;
res.h+=tmp.h;
res.i+=tmp.i;
res.j+=tmp.j;
res.cnt+=tmp.cnt;
}
return limit?res:f[i][lead]=res;
}
node dp(int n){
int cnt=0;
memset(f,-1,sizeof f);
memset(a,0,sizeof a);
while(n){
a[++cnt]=n%10;
n=n/10;
}
return dfs(cnt,1,1);
}
int main(){
while(cin>>l>>r){
if(l==0&&r==0) break;
if(l>r) swap(l,r);
node tmpl=dp(l-1);
node tmpr=dp(r);
cout << tmpr.a-tmpl.a<<" "<< tmpr.b-tmpl.b<<" "<< tmpr.c-tmpl.c<<" "<< tmpr.d-tmpl.d<<" "<< tmpr.e-tmpl.e<<" "<< tmpr.f-tmpl.f<<" "<< tmpr.g-tmpl.g<<" "<< tmpr.h-tmpl.h<<" "<< tmpr.i-tmpl.i<<" "<< tmpr.j-tmpl.j<<" "<<endl;
}
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
const int N = 10;
int l, r;
int a[N], f[N][N];
int dfs(int pos, int sum, int x, int lead, int limit)
{
if (!pos)
return sum;
if (!limit && !lead && ~f[pos][sum])
return f[pos][sum];
int res = 0, up = limit ? a[pos] : 9;
for (int i = 0; i <= up; ++i)
if ((!lead || i) && (i == x))
res += dfs(pos - 1, sum + 1, x, lead && !i, limit && i == up);
else
res += dfs(pos - 1, sum, x, lead && !i, limit && i == up);
if (!limit && !lead)
f[pos][sum] = res;
return res;
}
int dp(int x, int i)
{
memset(f, -1, sizeof(f));
int len = 0;
while (x)
a[++len] = x % 10, x /= 10;
return dfs(len, 0, i, 1, 1);
}
int main()
{
while (cin >> l >> r, l || r)
{
if (l > r)
swap(l, r);
for (int i = 0; i <= 9; ++i)
cout << dp(r, i) - dp(l - 1, i) << ' ';
puts("");
}
return 0;
}