社区讨论
萌新求助
P4999烦人的数学作业参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @lqjaelga
- 此快照首次捕获于
- 2023/12/24 17:28 2 年前
- 此快照最后确认于
- 2023/12/24 19:49 2 年前
本来我写的是
CPP#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int t;
int a[25],l,r,f[25][555];
int dp(int pos,bool limit,int sum){
if(!pos)
return sum;
if(limit){
int ans=0;
for(int i=0;i<=a[pos];i++){
if(i==a[pos])
ans+=dp(pos-1,true,sum+i)%mod;
else
ans+=dp(pos-1,false,sum+i)%mod;
ans%=mod;
}
return ans%mod;
}
if(f[pos][sum])
return f[pos][sum];
int ans=0;
for(int i=0;i<=9;i++){
ans+=dp(pos-1,false,sum+i)%mod;
ans%=mod;
}
return f[pos][sum]=ans;
}
int solve(int x){
int len=1;
while(x){
a[len++]=x%10;
x/=10;
}
len--;
return dp(len,true,0);
}
signed main(){
cin>>t;
while(t--){
cin>>l>>r;
cout<<(solve(r)-solve(l-1))%mod<<"\n";
}
return 0;
}
求问为什么
AC代码(改的地方用-围起来了)
CPP#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int t;
int a[25],l,r,f[25][555];
int dp(int pos,bool limit,int sum){
if(!pos)
return sum;
if(limit){
int ans=0;
for(int i=0;i<=a[pos];i++){
if(i==a[pos])
ans+=dp(pos-1,true,sum+i)%mod;
else
ans+=dp(pos-1,false,sum+i)%mod;
ans%=mod;
}
return ans%mod;
}
if(f[pos][sum])
return f[pos][sum];
int ans=0;
for(int i=0;i<=9;i++){
ans+=dp(pos-1,false,sum+i)%mod;
ans%=mod;
}
return f[pos][sum]=ans;
}
int solve(int x){
int len=1;
while(x){
a[len++]=x%10;
x/=10;
}
len--;
return dp(len,true,0);
}
signed main(){
cin>>t;
while(t--){
cin>>l>>r;
cout<<(solve(r)-solve(l-1)---+mod---)%mod<<"\n";
}
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...