Codeforces Round #631 (Div. 2)

编程入门 行业动态 更新时间:2024-10-27 12:25:16

<a href=https://www.elefans.com/category/jswz/34/1770097.html style=Codeforces Round #631 (Div. 2)"/>

Codeforces Round #631 (Div. 2)

题目链接


思路:能满足条件的a数组一定满足a【i-1】<a【i】并且a【i-1】的最高位要小于a【i】的最高位,例如a【i-1】为10的话,a【i】可以为100、1000、10000.。。。(以上以下都用二进制表示),由于n的长度不限定,我们就先找到2的幂次方中与d靠最近的(也就是代码里的pos),我们就可以考虑以下每一位的贡献,对于第i位a【i】有几种选择,假设a【i】是要从100和1000里来选择,那么很显然a【i】一共有1000个数可以选择(注意这个1000是二进制),其他位依次类推,2的幂次可以打表,最后根据乘法原理再相乘。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int f[maxn];
int main()
{int T,pos;f[0]=1;for(int i=1;i<=31;++i) f[i]=f[i-1]*2;scanf("%d",&T);while(T--){ll d,mod,ans=1;scanf("%lld %lld",&d,&mod);for(pos=0;d>=f[pos];pos++);ll t=d-f[pos-1]+1;for(int i=0;i<=pos-2;++i) ans=(ans+ans*f[i])%mod;ans=(ans+t*ans)%mod;printf("%lld\n",(ans-1+mod)%mod);}
}

更多推荐

Codeforces Round #631 (Div. 2)

本文发布于:2024-02-10 16:21:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1676203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Codeforces   Div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!