Debugging Gym - 101485D (数论分块 + 记忆化搜索)

思路:因为对于均等划分来说,有两种情况,一是完全化成等分,二是一部分等分 + 一块小一点的,而对于后者来说,小一点的那块越大越好,也就联想到了用数论分块来求解。然后直接搜就行,但是存在最优子结构,所以需要加上记忆化搜索=_=,场上想到分块了,但是感觉不加记忆化搜索不会T结果T了,然后因时间紧,没来的及加记忆化,虽然改成记忆化也挺简单的。。。。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
ll n, s, p;
ll d[maxn];

ll dfs(ll now) {
    if(now <= 1) return 0;
    if(~d[now])  return d[now];
    d[now] = 1e17;
    for(ll l = 1, r = 0; l <= now; l = r + 1) {
        r = now / (now / l);
        if(now % l != 0) {
            d[now] = min(d[now], dfs(l) + (now / l) * p + s);
        }
        if(now % r == 0) {
            d[now] = min(d[now], dfs(r) + (now / r - 1) * p + s);
        }
    }
    return d[now];
}
int main()
{   
    // freopen("i.txt", "r", stdin);      
    scanf("%lld%lld%lld", &n, &s, &p);          
    memset(d, -1, sizeof(d));      
    printf("%lld\n", dfs(n));  
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write a Model class with the following UML specification: +----------------------------------------------+ | Model | +----------------------------------------------+ | - score: int | | - bubbles: ArrayList<IShape> | +----------------------------------------------+ | + Model() | | + getScore(): int | | + addBubble(int w, int h): void | | + moveAll(int dx, int dy): void | | + clearInvisibles(int w, int h): void | | + deleteBubblesAtPoint(int x, int y): void | | + drawAll(Graphics g): void | | + testModel(): void | +----------------------------------------------+ When a new model object is created, the score must be zero and the arraylist must be empty. The getScore method returns as result the current score for the game. The addBubble method adds a new bubble to the arraylist of bubbles. The position of the center of the new bubble is random but must be inside a window of width w and height h (the arguments of the addBubble method), like this: new Bubble((int)(w * Math.random()), (int)(h * Math.random())) The moveAll method moves the positions of all the bubbles in the arraylist of bubbles by the amount dx in the x direction and by the amount dy in the y direction. The clearInvisibles method takes as argument the width w and the height h of the window, and deletes from the arraylist of bubbles any bubble which is not visible in the window anymore. For each bubble which is deleted, the score decreases by 1.The deleteBubblesAtPoint method takes as argument the coordinates (x, y) of a point, and deletes from the arraylist of bubbles any bubble which contains this point (multiple bubbles might contain the point, because bubbles can overlap in the window). For each bubble which is deleted, the score increases by 1. The drawAll method draws all the bubbles in the arraylist of bub
最新发布
05-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值