CCF_CSP认证2020年6月第四题1246(c++)

在这里插入图片描述

在这里插入图片描述
刚自己看题时就知道这题自己写不出来,拿分不难,前面几个测试点直接暴力就可,

然后第一次看了这篇博客

[https://blog.csdn.net/weixin_45884316/article/details/108237376?utm_medium=distribute.pc_feed_404.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_feed_404.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecas]

然后又补了下不熟的快速幂,这道题要拿96分难点就是发现规律,并且写出变换矩阵,就尝试自己写了份代码,和原博客几乎一样,只是变量名不同和多了些注释。

规律:

前面测试点输出字符串长度只有2位,这就只有14个数,可以看题目描述中给的样例得出,也可以从1,2,4,6四个数推出来,1,2,4,6,16,26,41,42,44,46,61,62,64,66,14个数,其中2只能由1推出,4只能由2, 6推出,单个字符只需考虑一个字符的情况,两个字符是合并而来的,对于2个字符组成的字符串,除了16,64可以由单个字符,其余都是由两个字符组成的字符串转化而来

1->2
2->4
4->1和6
6-6和4
两个字符可以拼接而成,但前提是拼成元字符要存在,比如2,4拼成24,但12不存在,所以24也不存在

具体变换在代码里注释了

源码

#include<bits/stdc++.h>
using namespace std;
const int mod = 998244353;
struct mat{
    long long m[15][15];
} ans;
map<string, int> id;
void init_id()
{
    id["1"] = 1; id["2"] = 2; id["4"] = 3; id["6"] = 4;
    id["16"] = 5; id["26"] = 6; id["41"] = 7; id["42"] = 8;
    id["44"] = 9; id["46"] = 10; id["61"] = 11; id["62"] = 12;
    id["64"] = 13; id["66"] = 14;
    return ;
}
mat Matrix_multi(mat a, mat b)
{
    mat c;
    for(int i = 0; i < 15; i ++){
        for(int j = 0; j < 15; j ++){
            c.m[i][j] = 0;
            for(int k = 0; k < 15; k ++){
                c.m[i][j] += a.m[i][k] * b.m[k][j];
                c.m[i][j] %= mod;
            }
        }
    }
    return c;
}
void Matrix_power(mat & a, int n)
{
    for(int i = 1; i < 15; i ++)
        ans.m[i][i] = 1;
    while(n > 0){
        if(n & 1)
            ans = Matrix_multi(ans, a);
        a = Matrix_multi(a, a);
        n >>= 1;
    }
    return;
}
int main()
{
    mat a =
    {{
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//0
        {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1<-4
        {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//2<-1
        {0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//4<-2, 16
        {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//6<-4, 6
        {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//16<-4
        {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},//26<-16
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},//41
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},//42
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},//44
        {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},//46
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},//61<-44
        {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},//62<-41
        {0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},//64<-6, 42
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},//66<-46
    }};
    init_id();
    int n;
    cin>>n;
    Matrix_power(a, n);
    string s;
    cin>>s;
    cout<<ans.m[id[s]][1] % mod<<endl;
}

这个提交只有96分,然后再在网上去找100分代码,先找到了一个java的

[https://blog.csdn.net/m0_37483148/article/details/108433622]:

尝试看了下,看不懂,去找c++的,皇天不负有心人,找到了

[https://www.it610.com/article/1304661599860068352.htm]:

根据要查找的字符串向上回溯,直到回溯到字符串长度<=2。

#include<bits/stdc++.h>
using namespace std;
const int mod = 998244353;
struct mat{
    long long m[15][15];
};
const int N = 1e6 + 10;
map<string, int> id;
pair<string, int> q[N];
long long int res = 0;
void init_id()
{
    id["1"] = 1; id["2"] = 2; id["4"] = 3; id["6"] = 4;
    id["16"] = 5; id["26"] = 6; id["41"] = 7; id["42"] = 8;
    id["44"] = 9; id["46"] = 10; id["61"] = 11; id["62"] = 12;
    id["64"] = 13; id["66"] = 14;
    return ;
}
mat Matrix_multi(mat a, mat b)
{
    mat c;
    for(int i = 0; i < 15; i ++){
        for(int j = 0; j < 15; j ++){
            c.m[i][j] = 0;
            for(int k = 0; k < 15; k ++){
                c.m[i][j] += a.m[i][k] * b.m[k][j];
                c.m[i][j] %= mod;
            }
        }
    }
    return c;
}
mat Matrix_power(mat  a, int n)
{
    mat ans;
    for(int i = 0; i < 15; i++)
        for(int j = 0; j < 15; j ++)
            ans.m[i][j] = 0;
    for(int i = 1; i < 15; i ++)
        ans.m[i][i] = 1;
    while(n > 0){
        if(n & 1)
            ans = Matrix_multi(ans, a);
        a = Matrix_multi(a, a);
        n >>= 1;
    }
    return ans;
}
long long int qm(string s, int n)
{
    mat a =
    {{
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//0
        {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//1<-4
        {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//2<-1
        {0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//4<-2, 16
        {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//6<-4, 6
        {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},//16<-4
        {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},//26<-16
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},//41
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},//42
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},//44
        {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},//46
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},//61<-44
        {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},//62<-41
        {0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},//64<-6, 42
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},//66<-46
    }};
    mat ans = Matrix_power(a, n);
    return ans.m[id[s]][1] % mod;
}
string findfa(string s)
{
    string fa = "";
    //cout<<"s"<<s[0]<<endl;
    for(int i = 0; i < s.size(); i ++){
        //cout<<"fa1:"<<fa<<endl;
        if(s[i] == '2') fa+= '1';//1产生2
        else if(s[i] == '4') fa += '2';//2产生4
        else if(s[i] == '1' && (i == s.size() - 1 || s[i + 1] == '6')){
            fa += '4'; i ++;//4产生16
        }
        else if(s[i] == '6' && (i == s.size() - 1 || s[i + 1] == '4')){
            fa += '6'; i ++;//6产生64
        }
        else return "";
    }
    return fa;
}
void bfs(string s, int n)
{
    int search_num = 0, sum = -1;//搜索到的编号, 搜索队列中的总数
    q[++ sum] = {s, n};
    while(search_num <= sum){
        pair<string, int> u = q[search_num ++];
        string ss = u.first;
        int depth = u.second;
        if(ss == "" || depth <= 0) continue;
        if(ss.size() <= 2) res = (res + qm(ss, depth)) % mod;
        else{
            q[++ sum] = {findfa(ss), depth - 1};//回溯到上一层
            if(ss[0] == '6') q[++ sum] = {findfa("1" + ss), depth - 1};//以6开头可能是16
            if(ss[0] == '4') q[++ sum] = {findfa("6" + ss), depth - 1};
        }
    }
}
int main()
{

    init_id();
    int n;
    cin>>n;
    string s;
    cin>>s;
    if(s.size() <= 2)
        res = qm(s, n);
    else
        bfs(s, n);
    cout<<res<<endl;
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值