不知道为什么刚开始的代码不可以,,又要看别人的另一个模板。。。
错误代码(不知道为什么):
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<stack>
#include<cmath>
#include<map>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
#define bug1 cout<<"bug1"<<endl;
#define bug2 cout<<"bug2"<<endl;
#define bug3 cout<<"bug3"<<endl;
using namespace std;
int dig[30];
int tmp[30];
int dp[30][30][15];//dp[pos][mod][have];
int dfs(int pos,int pre,int mod,int have,bool limit){
if(pos==-1){
return mod%13==0&&have;
}
if(!limit&&dp[pos][mod][have]!=-1)return dp[pos][mod][have];
int up=limit?dig[pos]:9;
int ret=0;
for(int i=0;i<=up;++i){
if(pre==1&&i==3){
ret+=dfs(pos-1,i,(mod*10+i)%13,1,limit&&up==i);
}//不知道为什么这种写法错了。。。
else{
ret+=dfs(pos-1,i,(mod*10+i)%13,have,limit&&up==i);
}
}
if(!limit)dp[pos][mod][have]=ret;
return ret;
}
int solve(int x){
int tol=0;
memset(dig,0,sizeof(dig));
while(x){
dig[tol++]=x%10;
x/=10;
}
return dfs(tol-1,0,0,0,true);//注意这个
}
int main(){
int n;
memset(dp,-1,sizeof(dp));
while(~scanf("%d",&n)){
cout<<solve(n)<<'\n';
}
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<stack>
#include<cmath>
#include<map>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
#define bug1 cout<<"bug1"<<endl;
#define bug2 cout<<"bug2"<<endl;
#define bug3 cout<<"bug3"<<endl;
using namespace std;
int dig[30];
int tmp[30];
int dp[30][30][15][15];//dp[pos][mod][have];
int dfs(int pos,int pre,int mod,int have,bool limit){
if(pos==-1){
return mod%13==0&&have;
}
if(!limit&&dp[pos][mod][have][pre]!=-1)return dp[pos][mod][have][pre];
//dp[pos][mod][have]=0;
int up=limit?dig[pos]:9;
int ret=0;
for(int i=0;i<=up;++i){
ret+=dfs(pos-1,i,(mod*10+i)%13,(pre==1&&i==3)||have,limit&&up==i);
}
if(!limit)dp[pos][mod][have][pre]=ret;//要多记录一个pre,这说明当pos一样,mod一样,have一样的时候,
return ret;//还有可能有pre不一样导致错误。不过既然我的dfs过程中都有pre,那我的状态也是应该加一个pre的。。
}
int solve(int x){
int tol=0;
memset(dig,0,sizeof(dig));
while(x){
dig[tol++]=x%10;
x/=10;
}
return dfs(tol-1,0,0,0,true);//注意这个
}
int main(){
int n;
memset(dp,-1,sizeof(dp));
while(~scanf("%d",&n)){
cout<<solve(n)<<'\n';
}
}
正确代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<stack>
#include<cmath>
#include<map>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
#define bug1 cout<<"bug1"<<endl;
#define bug2 cout<<"bug2"<<endl;
#define bug3 cout<<"bug3"<<endl;
using namespace std;
int dig[30];
int tmp[30];
int dp[30][30][15];//dp[pos][mod][have];
int dfs(int pos,int mod,int have,bool limit){
if(pos==-1){
return mod%13==0&&have==2;
}
if(!limit&&dp[pos][mod][have]!=-1)return dp[pos][mod][have];
int up=limit?dig[pos]:9;
int ret=0;
for(int i=0;i<=up;++i){
int n_have=have;
if(have==0&&i==1){//末尾不是1,现在加入的是1
n_have=1;
}
if(have==1&&i!=1){//末尾是1,现在加入的不是1
n_have=0;
}
if(have==1&&i==3){//末尾是1,现在加入的是3
n_have=2;
}
ret+=dfs(pos-1,(mod*10+i)%13,n_have,limit&&up==i);
}
if(!limit)dp[pos][mod][have]=ret;
return ret;
}
int solve(int x){
int tol=0;
memset(dig,0,sizeof(dig));
while(x){
dig[tol++]=x%10;
x/=10;
}
return dfs(tol-1,0,0,true);//注意这个
}
int main(){
int n;
memset(dp,-1,sizeof(dp));
while(~scanf("%d",&n)){
cout<<solve(n)<<'\n';
}
}