CQOI手机号码

本文探讨了在处理特定数值(如1e10)时遇到的问题,并提出了两种解决方案。一种是特判处理,另一种是从首位开始填充,避免前导0的影响。通过动态规划算法实现了具体的计算方法。

传送门

先不考虑是否有前导 0 0 0的限制。
如果 l > 1 e 10 l>1e10 l>1e10,相减之后含前导 0 0 0的答案自然会被抵消。
如果 l = = 1 e 10 l==1e10 l==1e10 l − 1 l-1 l1就会少一位。这时候就抵消不了了。
1 e 10 1e10 1e10 1 e 10 − 1 1e10-1 1e101统计出来答案的差不止为1。为什么呢?可以这样考虑:
1 e 10 1e10 1e10的比 1 e 10 − 1 1e10-1 1e101多了一位。把这两个数右对齐。那么 1 e 10 1e10 1e10多出来了最左边一位。
如果这一位填 1 1 1,就只有 1 e 10 1e10 1e10这一个符合。这是多出来的一个。(然而不止这一个)
如果这一位填 0 0 0,那么后面接着的两位填 0 0 0,满足了连续三个相同,之后就可以任意填了(8与4不同时出现)但是这时候看另一个数。如果它前面两位填 0 0 0,后面是不能随便填的,因为还没有满足连续三个,于是它们之间差的就不止一个了。但是其他情况都是可以一一对应的。于是 符合条件的数的个数之差为:小于等于9999999999的数中(可含前导0),前两位为0且不满足条件的数(但需满足8与4不同时出现)的个数。因为这些数在11位数里符合,在10位数里就不符合了。

解决办法1:特判一下 l = = 1 e 10 l==1e10 l==1e10。加1是加上 1 e 10 1e10 1e10本身这个数。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll oo=1e10;
int a[20],len;ll f[20][2][10][10][2][2],l,r;
inline ll dp(int pos,int ok,int prepre,int pre,int eight,int four,bool limit,ll ans=0){
    if(eight&&four) return 0; 
    if(!pos) return ok;
    if(~f[pos][ok][prepre][pre][eight][four] && !limit) return f[pos][ok][prepre][pre][eight][four];
    int up=limit?a[pos]:9;
    for(int i=0;i<=up;++i) ans+=dp(pos-1,ok||((prepre==pre)&&(prepre==i)),pre,i,(i==8)||eight,(i==4)||four,(i==up)&&limit);
    if(!limit) f[pos][ok][prepre][pre][eight][four]=ans;
    return ans;
}
inline ll solve(ll x,int pos=0){
    memset(f,-1,sizeof(f));
    for(len=0;x;x/=10) a[++len]=x%10;
    return dp(len,0,-1,-1,0,0,true);
}
int main(){
    cin>>l>>r;
    if(l==oo) cout<<solve(r)-solve(l)+1;
    else  cout<<solve(r)-solve(l-1);
}

解决办法2:首位从1开始填。这样就不存在前导0了。然而当 l = = 1 e 10 l==1e10 l==1e10的时候, l − 1 l-1 l1的贡献就是0了。小于 1 e 10 1e10 1e10的数都是不符合的。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll oo=1e10;
int a[20],len;ll f[20][2][10][10][2][2],l,r;
inline ll dp(int pos,int ok,int prepre,int pre,int eight,int four,bool limit,ll ans=0){
    if(eight&&four) return 0; 
    if(!pos) return ok;
    if(~f[pos][ok][prepre][pre][eight][four] && !limit) return f[pos][ok][prepre][pre][eight][four];
    int up=limit?a[pos]:9;
    for(int i=(len==pos);i<=up;++i) ans+=dp(pos-1,ok||((prepre==pre)&&(prepre==i)),pre,i,(i==8)||eight,(i==4)||four,(i==up)&&limit);
    if(!limit) f[pos][ok][prepre][pre][eight][four]=ans;
    return ans;
}
inline ll solve(ll x,int pos=0){
    if(x<oo) return 0;
    memset(f,-1,sizeof(f));
    for(len=0;x;x/=10) a[++len]=x%10;
    return dp(len,0,-1,-1,0,0,true);
}
int main(){cin>>l>>r;cout<<solve(r)-solve(l-1);}
根据引用所述,交错序列是一个仅由0和1构成的序列,其中没有相邻的1(可以有相邻的0)。特征值定义为x^ay^b,其中x和y分别表示0和1出现的次数。长度为n的交错序列可能有多个。问题要求计算所有长度为n的交错序列特征值的和除以m的余数。 根据引用所述,输入文件包含一个行,该行包含三个整数n、a、b和m。其中,1≤n≤10000000,0≤a、b≤45,m<100000000。 为了解决这个问题,可以使用动态规划和矩阵快速幂优化的方法,具体实现可以参考引用提到的相关算法。算法的思路是通过计算长度为n的交错序列的特征值,然后将所有特征值求和并对m取余数。 具体步骤如下: 1. 使用动态规划计算长度为n的所有交错序列的特征值,将结果保存在一个矩阵中。 2. 使用矩阵快速幂优化,将动态规划的过程进行优化。 3. 对优化后的结果进行求和,并对m取余数。 4. 输出结果。 参考引用给出的博客中的代码实现,可以帮助你更好地理解和实现该算法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [BZOJ5298 CQOI2018 交错序列 【DP+矩阵快速幂优化】*](https://blog.youkuaiyun.com/weixin_30892987/article/details/99470493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值