前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >UVA 12627 – Erratic Expansion

UVA 12627 – Erratic Expansion

作者头像
全栈程序员站长
发布2022-07-10 13:54:41
发布2022-07-10 13:54:41
11900
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是全栈君。

一个红球能够分裂为3个红球和一个蓝球。 一个蓝球能够分裂为4个蓝球。

分裂过程下图所看到的:

设当前状态为k1。下一状态为k2。

k1的第x行红球个数 * 2 ⇒ k2第2*x行的红球个数。 k1的第x行红球个数 ⇒ k2第2*x+1行的红球个数。

特殊处理一下上下边界。递归求解就能够搞定了。

代码语言:javascript
代码运行次数:0
复制
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
#include <sstream>

typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
long long gao(int k, int a, int b)
{
    if(a > b) return 0;
    if(k == 0)
    {
        return 1;
    }
    int left = 0, right = 0;
    if(a % 2 == 0)
    {
        left = a;
        a++;
    }
    if(b % 2 == 1)
    {
        right = b;
        b--;
    }
    long long ans = gao(k-1, (a + 1) / 2, b / 2) * 2 + gao(k - 1, (a + 1) / 2, b / 2);
    if(left) ans += gao(k-1, left / 2,  left / 2);
    if(right) ans += gao(k-1, (right + 1) / 2,  (right + 1) / 2) * 2;
    return ans;
}
int main()
{
    #ifdef _Te3st
        freopen("test0.in", "r", stdin);
        freopen("test0.out", "w", stdout);
        srand(time(NULL));
    #endif
    int T, k, a, b;
    scanf("%d", &T);
    for(int i = 1; i <= T; i++)
    {
        scanf("%d %d %d", &k, &a, &b);
        printf("Case %d: %lld\n", i, gao(k, a, b));
    }
    return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115461.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年2月4,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档