标签:表示 tps int math 导致 while cstring 一个 algo
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline ll read(){
ll x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
int len,a[20];ll dp[20][20][200];
inline ll dfs(int pos,int pre,ll sum1,ll sum2,int lead,int limit){
if(pos>len){
if(!sum2)return 1;
if(sum1%sum2==0)return 1;
return 0;
}
if(dp[pos][pre][sum2]!=-1&&!lead&&!limit)return dp[pos][pre][sum2];
ll cnt=0;int res=limit?a[len-pos+1]:9;
for(int i=0;i<=res;++i){
if((!i)&&lead)cnt+=dfs(pos+1,0,0,0,1,limit&&(i==res));
else if(i&&lead)cnt+=dfs(pos+1,i,i,i,0,limit&&(i==res));
else cnt+=dfs(pos+1,i,1ll*sum1*10+i,sum2+i,0,limit&&(i==res));
}
return !lead&&!limit?dp[pos][pre][sum2]=cnt:cnt;
}
inline ll part(ll x){
len=0;while(x)a[++len]=x%10,x/=10;
memset(dp,-1,sizeof(dp));
return dfs(1,0,0,0,1,1);
}
int main(){
ll l=read(),r=read();
printf("%lld\n",part(r)-part(l-1));
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline ll read(){
ll x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
int len,mod,a[20];ll ans,dp[20][200][200];
inline ll dfs(int pos,int st,int sum,int lead,int limit){
//把普通模板中记录上一位填数的pre删掉了,因为它在本题中没有任何意义
if(pos>len)return (!st&&sum==mod);//判断该数是否合法
if(dp[pos][st][sum]!=-1&&!lead&&!limit)return dp[pos][st][sum];
ll cnt=0;int res=limit?a[len-pos+1]:9;
for(int i=0;i<=res;++i){
if((!i)&&lead)cnt+=dfs(pos+1,0,0,1,limit&&(i==res));
else if(i&&lead)cnt+=dfs(pos+1,i%mod,i,0,limit&&(i==res));
else cnt+=dfs(pos+1,(st*10+i)%mod,sum+i,0,limit&&(i==res));
}
return lead&&limit?cnt:dp[pos][st][sum]=cnt;
}
inline ll part(ll x){
ans=0;len=0;while(x)a[++len]=x%10,x/=10;
for(mod=1;mod<=9*len;++mod){//枚举模数
memset(dp,-1,sizeof(dp));
ans+=dfs(1,0,0,1,1);
}
return ans;
}
int main(){
ll l=read(),r=read();
printf("%lld\n",part(r)-part(l-1));
return 0;
}
标签:表示 tps int math 导致 while cstring 一个 algo
原文地址:https://www.cnblogs.com/PPXppx/p/11586085.html