当前位置: 代码迷 >> 综合 >> 【SCOI2009】生日快乐(dfs+思维)
  详细解决方案

【SCOI2009】生日快乐(dfs+思维)

热度:53   发布时间:2024-01-28 22:33:49.0

在这里插入图片描述

思路:我们用dfs去想,首先如果此时只有一个人那我们就直接返回答案,dfs(x,y,n)表示边长为x,y,切给n个人长边与短边之比最大值最小的值是多少,为了保证面积相同每次切若沿着x切必定是x/n的倍数若沿着y切必定是y/n的倍数,我们要看切完后分给多少个人,就看他当前切的是多少倍,然后我们按X,Y去切就行了求他们的最大值最小

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=2e6+10;
const int inf=0x7f7f7f7f;ll gcd(ll a,ll b)
{return b==0?a:gcd(b,a%b);
}ll lcm(ll a,ll b)
{return a*(b/gcd(a,b));
}template <class T>
void read(T &x)
{char c;bool op = 0;while(c = getchar(), c < '0' || c > '9')if(c == '-')op = 1;x = c - '0';while(c = getchar(), c >= '0' && c <= '9')x = x * 10 + c - '0';if(op)x = -x;
}
template <class T>
void write(T x)
{if(x < 0)x = -x, putchar('-');if(x >= 10)write(x / 10);putchar('0' + x % 10);
}double s;double dfs(double x,double y,int n)
{if(n==1) return max(x,y)/min(x,y);double a=x/n,b=y/n,ans=inf;for(int i=1;i<=n/2;i++){ans=min(ans,min(max(dfs(i*a,y,i),dfs(x-i*a,y,n-i)),max(dfs(x,i*b,i),dfs(x,y-i*b,n-i))));}return ans;
}
int main()
{double x,y;int n;scanf("%lf%lf%d",&x,&y,&n);s=dfs(x,y,n);printf("%.6f",s);return 0;
}