组队赛(F/D) UVALive - 7426(dfs)

44 篇文章 0 订阅
43 篇文章 0 订阅

这个题题题目的一开始没有看懂,后来理解了,这个代码的意思,但也并不是很懂 ,

就是列举各种情况,在代码的各个位置插入输出,分割之后,程序崩坏另一边就不用判断,然后把已经求过的存下来,因为只要代码长度一样所花的时间是一样的看, 然后开始dfs返回搜索各种情况

这个dfs略魔性,是为了储存值用的吧


#include<cstdio>
#include<cstring>
#include <iostream>
#include <algorithm>
#define Min(a, b) ( (a < b) ? a : b )
#define Max(a, b) ( (a > b) ? a : b )
#define mem0(x)   memset(x, 0, sizeof x)

#define INFL      1000000000000000000

using namespace std;

typedef long long          LL;
typedef double             DB;
typedef long double        LD;
typedef pair<int , int >   pii;
typedef pair<LL, LL>       pll;
const LL N =1e6+5;
struct Node{
   int x, y;
   int id;
   bool operator < ( const Node & n) const {
      return x < n.x;
   }
}no[N];


LL n, r, p;
LL dp[N];

LL dfs(LL x)
{
   if ( x <= 1 ) return 0;
   if ( dp[x]  ) return dp[x];
   LL ans = INFL;
   LL sum = 0;
   for (int i = 2; i <= x; i++) {
        sum = r + (i - 1 ) * p;
        if (  x % i == 0 ) {
              sum += dfs( x / i );
        } else sum += dfs( x / i + 1 );//如果不是整除的话返回程序分割之后大的那一部分,因为这一部分耗时长
        ans = Min(ans,  sum);
   }
   return dp[x] = ans;
}

int main()
{
     //freopen("codecoder.in", "r", stdin);
     // freopen("codecoder.out", "w", stdout);
     //ios::sync_with_stdio(true);
     while ( ~scanf("%lld%lld%lld", &n, &r, &p))
     {
         mem0(dp);
        if (  n == 1 ) {
               puts("0"); continue;
         }
         printf("%lld\n", dfs(n) );
     }
     return 0;
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值