跳到正文

个人记录

ans

Ch_Elowen约 6 分钟
原文

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

文章信息与版本

文章信息

收录
2026年4月24日
更新
2026年4月24日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

U404848 递归函数f(x,n)

#include
using namespace std ;
int t , n ;
double x ;
double ans[100005] ;
double dfs(int w)
{
	if(w == 0) return 1 ;
	if(ans[w]) return ans[w] ;
	return ans[w] = w / (x + dfs(w - 1)) ; 
}
int main()
{
	cin >> t >> x ;
	while(t--)
	{
		cin >> n ; 
		printf("%.2lf\n" , dfs(n)) ;
	}
return 0 ;
}

P10376 [GESP202403 六级] 游戏

C++
#include <bits/stdc++.h>
using namespace std ;
#define int long long
int n , a , b , c ;
int ans[200005] ;
int dfs(int w)
{
	if(w <= c) return 1 ;
	if(ans[w]) return ans[w] ;
	return ans[w] = (dfs(w - a) + dfs(w - b)) % 1000000007 ;
} 
signed main()
{
	cin >> n >> a >> b >> c ;
	cout << dfs(n) ;
return 0 ;
}

P1434 [SHOI2002] 滑雪

C++
#include <bits/stdc++.h>
using namespace std ;
int r , c ;
int maxn = INT_MIN , minn = INT_MAX ;
int a[105][105] , ans[105][105] ;
int dx[5] = {0 , 0 , -1 , 1} ;
int dy[5] = {-1 , 1 , 0 , 0} ;
int dfs(int x , int y)
{
	if(a[x][y] == minn) return 1 ;
	if(ans[x][y]) return ans[x][y] ;
    ans[x][y] = 1 ;
	for(int i = 0 ; i < 4 ; i++)
    {
		int xx = x + dx[i] ;
		int yy = y + dy[i] ;
		if(xx < 1 || xx > r || yy < 1 || yy > c) continue ;
		if(a[xx][yy] >= a[x][y]) continue ;
		ans[x][y] = max(ans[x][y] , dfs(xx , yy) + 1) ;
	}
	return ans[x][y] ;
}
int main()
{
	cin >> r >> c ;
	for(int i = 1 ; i <= r ; i++)
    {
		for(int j = 1 ; j <= c ; j++)
        {
			cin >> a[i][j] ;
			minn = min(minn , a[i][j]) ;
		}
    }
	for(int i = 1 ; i <= r ; i++)
    {
		for(int j = 1 ; j <= c ; j++)
        {
			maxn = max(maxn , dfs(i , j)) ;
        }
    }
	cout << maxn ;
return 0 ;
}

P5635 【CSGRound1】天下第一

C++
#include <bits/stdc++.h>
using namespace std ;
int t , p ;
int w , h ;
short f[10005][10005] ;
int dfs(int x , int y)
{
	if(f[x][y] == -1) return 3 ;
    if(f[x][y] > 0) 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--) 
    {
        cin >> w >> h ;
    	int ans = dfs(w , h) ;
    	if(ans == 3) cout << "error\n" ;
    	else cout << ans << '\n' ;
    }
return 0 ;
}

P2690 [USACO04NOV] Apple Catching G

C++
#include <bits/stdc++.h>
using namespace std ;
const int N = 1e3 + 5 ;
int t , w , ans , a[N] , dp[N][N] ;
int main()
{
    cin >> t >> w ;
    for(int i = 1 ; i <= t ; i++) cin >> a[i];
    for(int i = 1 ; i <= t ; i++)
    {
        for(int j = 0 ; j <= t && j <= w ; j++)
        {
            if(j == 0) dp[i][j] = dp[i - 1][j] ;
            else dp[i][j] = max(dp[i - 1][j] , dp[i - 1][j - 1]) ;
            if(a[i] == j % 2 + 1) dp[i][j]++ ;
        }
    }
    for(int i = 0 ; i <= w ; i++) ans = max(ans , dp[t][i]) ;
    cout << ans ;
return 0 ;
}