跳到正文

个人记录

4.24总结

liuxiuqi_0203约 8 分钟
原文

这是 2026年5月3日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年4月30日
收录
2026年5月3日
更新
2026年11月7日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

4.24总结

U404848 递归函数f(x,n)

错误代码:

C++
#include<bits/stdc++.h>
using namespace std;
int t;
double x;
double a[10005];
double dfs(double x,double n){
  if(a[int(n)]!=0){
  	return a[int(n)];
  }
  if(n==1){
  	return 1/(x+1.0);
  }
  a[int(n)]=n/(x+dfs(x,n-1));
  return a[int(n)];
}
int main(){
	cin>>t>>x;
	while(t--){
		double n;
		cin>>n; 
		//cout<<dfs(x,n)<<' ';
		int df=int(dfs(x,n)*100);
		string str1="",str2="";
		while(df){
		  str1+=char('0'+df%10);
		  df=df/10;
		}
		for(int i=str1.size()-1;i>=0;i--){
		  str2+=str1[i];
		  if(i==2){
		  	str2+='.';
		  }
		}
		if(str2.size()<=2){
		  cout<<"0.";
		}
		if(str2.size()==1){
		  cout<<0;
		}
		if(str2.size()==0){
		  cout<<0;
		}
		cout<<str2<<'\n';
	}
	return 0;
} 

错误原因:

手动保留小数错误

正确思路:

把每个nn的阶段保留,随后dfsdfs,时间复杂度O(nt)O(nt)

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
int t;
double x;
double a[10005];
double dfs(double x,double n){
  if(a[int(n)]!=0){//调用记忆化
  	return a[int(n)];
  }
  if(n==1){
  	return 1/(x+1.0);//最后一层
  }
  a[int(n)]=n/(x+dfs(x,n-1));//递归
  return a[int(n)];
}
int main(){
	cin>>t>>x;
	while(t--){
		double n;
		cin>>n; 
		//cout<<dfs(x,n)<<' ';
		printf("%.2lf",dfs(x,n));//保留小数
        cout<<'\n';
		
	}
	return 0;
}

P1434 [SHOI2002] 滑雪

错误代码:

C++
#include<bits/stdc++.h>
using namespace std;
const int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m;
int a[105][105],l[105][105];
int dfs(int x,int y){
	if(l[x][y]==0){
		l[x][y]=1;
		for(int i=0;i<=3;i++){
          int nx=x+d[i][0],ny=y+d[i][1];
		  if(l[x][y]>l[nx][ny]&&nx>=1&&nx<=n&&ny>=1&&ny<=m){
		  	l[x][y]=max(l[x][y],1+dfs(nx,ny));
		  }
		}
	}
	return l[x][y];
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
	  for(int j=1;j<=m;j++){
	  	cin>>a[i][j]; 
	  }
	}
	int maxx=INT_MIN;
	for(int i=1;i<=n;i++){
	  for(int j=1;j<=m;j++){
	  	maxx=max(maxx,dfs(i,j));
	  }
	}
	cout<<maxx;
	return 0;
} 

错误原因:

C++
if(l[x][y]>l[nx][ny]&&nx>=1&&nx<=n&&ny>=1&&ny<=m){

这里把高度写成长度!

正确思路:

因为满足单调性,所以我们可以算出一个点的最长路线,然后递归

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
const int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m;
int a[105][105],l[105][105];
int dfs(int x,int y){
	if(l[x][y]==0){
		l[x][y]=1;
		for(int i=0;i<=3;i++){
          int nx=x+d[i][0],ny=y+d[i][1];
		  if(nx>=1 && nx<=n && ny>=1 && ny<=m && a[x][y] > a[nx][ny]){
		  	l[x][y]=max(l[x][y],1+dfs(nx,ny));
		  }
		}
	}
	return l[x][y];
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
	  for(int j=1;j<=m;j++){
	  	cin>>a[i][j]; 
	  }
	}
	int maxx=INT_MIN;
	for(int i=1;i<=n;i++){
	  for(int j=1;j<=m;j++){
	  	maxx=max(maxx,dfs(i,j));
	  }
	}
	cout<<maxx;
	return 0;
} 

P5635 【CSGRound1】天下第一

错误代码:

C++
#include<bits/stdc++.h>
using namespace std;
int t,p;
vector<pair<int,int> > vec; 
int dfs(int a,int b,int z){
  if(a==0){
  	return 1;
  }
  if(b==0){
  	return 2;
  }
  for(int i=0;i<vec.size();i++){
  	if(vec[i].first==a&&vec[i].second==b){
  	  return 3;
	}
  }
  vec.push_back({a,b});
  if(z==0){
  	dfs((a+b)%p,b,1);
  }else{
  	dfs(a,(a+b)%p,0);
  }
}
int main(){
	cin>>t>>p;
	while(t--){
		int x,y;
		cin>>x>>y;
		vec.clear();
		int dc=dfs(x,y,0);
		if(dc!=3){
		  cout<<dc<<'\n';
		}else{
		  cout<<"error\n";
		}
	}
	return 0;
}

错误原因:

vector<pair>vector<pair>找起来不方便,而且在不知名大数据下会RERE

正确思路:

用shortshort来存储,dfsdfs弄答案,因为shortshort他可以勉强存下10000210000^2的惊人大小,所以就用他来做记忆化

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
int t,p;
short f[10002][10002];//记忆化数组
int dfs(int x,int y){
	if(f[x][y]==-1){//可行性判断
		return 3;
	}
	if(f[x][y]){//记忆化
		return f[x][y];
	}
	f[x][y]=-1;
	if(x==0){
		f[x][y]=1;
	}else if(y==0){
		f[x][y]=2;
	}else{
		f[x][y]=dfs((x+y)%p,((x+y)%p+y)%p);//递归
	}
	return f[x][y];
}
int main(){
	cin>>t>>p;
	while(t--){
		int x,y;
		cin>>x>>y;
		int dc=dfs(x,y);
		if(dc!=3){
		  cout<<dc<<'\n';
		}else{
		  cout<<"error\n";//平局
		}
	}
	return 0;
}