HDOJ 2065 "红色病毒"问题

本文针对HDU ACM 2065题目进行了解析,通过深度优先搜索找到了规律,并给出了一种高效的解决方案。最终实现了快速计算特定条件下字符串组合数量的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给出一个只有”ABCD"四种字符的字符串,字符串中必须为偶数个A和C(可为0个),求长度为n时,存在多少种满足要求的字符串( 输出总数的最后两位即可)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2065

思路:dfs暴搜,找到规律Kn = 2 * 4^(n-1) - 2 ^ ( n-1),打表。

注意点:循环节为3-22,注意取余方式


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
126774662015-01-10 16:26:40Accepted20650MS1248K2194 BG++luminous11
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);

/*
int cnt = 0;
int k;
int num[100] = { 0 };

void check()
{
    int a[5] = { 0 };
    for ( int i = 1; i <= k; i ++ ){
        a[num[i]]++;
    }
    if ( ( a[1] % 2 == 0 ) && ( a[3] % 2 == 0 ) ){
        cnt ++;
    }
}

void dfs ( int n )
{
    if ( n == 0 ){
        check();
        return;
    }
    for ( int i = 1; i < 5; i ++ ){
        num[n] = i;
        dfs ( n - 1 );
    }
}

int main()
{
    for ( k = 1; k < 30; k ++){
        cnt = 0;
        dfs ( k );
        cout << k << ' ' << cnt << end;
    }
    return 0;
}
*/
//以上为dfs暴搜过程

int main()
{
    ull a[80] = { 1, 2 };
    ull b[80] = { 1 };
    for ( int i = 1; i < 60; i ++ ){
        b[i] = b[i-1] * 2;
    }
    for ( int i = 2; i < 60; i ++){
        a[i] = 4 * a[i-1] - b[i-1];
    }
    ull k;
    int t;
    bool flag = 0;
    while ( cin >> t && t ){
        for ( int i = 1; i <= t; i ++ ){
            cin >> k;
            if ( k >= 23 ){
                k = ( k - 3 ) % 20;
                k += 3;
            }
            cout << "Case " << i << ": " << a[k] % 100 << endl;
        }
        cout << endl;
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值