Dreamoon Likes Sequences CodeForces - 1330D(组合数学+位运算)
Dreamoon likes sequences very much. So he created a problem about the sequence that you can’t find in OEIS:
You are given two integers d,m, find the number of arrays a, satisfying the following constraints:
The length of a is n, n≥1
1≤a1<a2<⋯<an≤d
Define an array b of length n as follows: b1=a1, ∀i>1,bi=bi−1⊕ai, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b1<b2<⋯<bn−1<bn should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1≤t≤100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d,m (1≤d,m≤109).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0
题意:
给出一个限制 d 与模数 mod ,求出可以构造出的满足条件的数组 a 的个数,需要满足以下条件:
①数组 a 的长度大于等于 1
②数组 a 严格递增
③数组 a 的最小值大于等于 1 ,数组 a 的最大值小于等于 d
对于数组 a ,构造出一个数组 b :
i == 1 时:b[ 1 ] = a[ 1 ]
i > 1 时:b[ i ] = b[ i - 1 ] ^ a[ i ]
数组 b 严格递增。
思路:牵扯到异或,我们就要考虑位运算了。对于a数组的每一个数,如果它选择了2,那么它接下来就没有办法选择3;如果它选择了4,那么它接下来就没有办法选择5,6,7;如果它选择了8,那么它接下来就没有办法选择9,10,11,12,13,14,15.换句话说,最高位为i的数字只能出现一次。那么第i位可以选择的数量为,但是第i位还有可能不加,也就是在原来基础上加1.然后累乘就可以了。最后结果要减一,这代表的是所有位都没有选择数的情况。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;int n,mod;int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&mod);ll ans=1;for(int i=0;i<30;i++){if((1<<i)>n) break;ans=(ans*(min((1<<(i+1))-1,n)-(1<<i)+2));ans%=mod;}ans=(ans-1+mod)%mod;cout<<ans<<endl;}return 0;
}
努力加油a啊,(o)/~
Dreamoon Likes Sequences CodeForces - 1330D(组合数学+位运算)相关推荐
- Codeforces Round #631 (Div. 2) D. Dreamoon Likes Sequences 思维 + 组合数学
传送门 文章目录 题意: 思路: 题意: 给你d,modd,modd,mod,让你求能构造出如下序列aaa的个数模modmodmod: 思路: 首先可以发现aaa的长度不能超过log2dlog_2dl ...
- CodeForces - 1330D Dreamoon Likes Sequences(组合数学)
题目链接:点击查看 题目大意:给出一个限制 d 与模数 mod ,求出可以构造出的满足条件的数组 a 的个数,需要满足以下条件: 数组 a 的长度大于等于 1 数组 a 严格递增 数组 a 的最小值大 ...
- Dreamoon Likes Coloring CodeForces - 1330C(贪心+思维)
Dreamoon likes coloring cells very much. There is a row of n cells. Initially, all cells are empty ( ...
- Powered Addition CodeForces - 1339C(位运算)
You have an array a of length n. For every positive integer x you are going to perform the following ...
- D. Dreamoon Likes Sequences(异或)
Problem - 1330D - Codeforces 题意: 给你两个整数d,m,找出数组a的数量,满足以下约束. a的长度为n,n≥1 1≤a1<a2<⋯<an≤d 定义长度为 ...
- CodeForces - 1457D XOR-gun(位运算+暴力)
题目链接:点击查看 题目大意:给出一个长度为 n 的非降序列,现在可以执行数次操作:选择相邻的两个位置将其替换成异或和,更具体的,每次操作可以选择一个位置 i ,满足 1 <= i < n ...
- Codeforces Round #631 (Div. 2) C. Dreamoon Likes Coloring 构造
传送门 文章目录 题意: 思路: 题意: 思路: 针灸思维不行,数据结构来凑呗. 一开始做的时候想简单了,一直wawawa,后来想到了hackhackhack样例,开始换思路构造,结果死活想不到O(m ...
- 【CodeForces 1042B --- Vitamins】DP+位运算
[CodeForces 1042B --- Vitamins]DP+位运算 题目来源:点击进入[CodeForces 1042B - Vitamins] Description Berland sho ...
- CodeForces - 1451E2 Bitwise Queries (Hard Version)(交互+构造+位运算)
题目链接:点击查看 题目大意:给出一个长度为 n(n 保证了是 2 的幂次),每个数的范围在 [ 0 , n - 1 ] 的一个数组,现在要求通过有限次操作确定下来这个数组: 询问 a[ i ] xo ...
最新文章
- PHP 字符处理 加密解密 函数收集
- js_jQuery【下拉菜单联动dom操作】
- zookeeper 源码阅读(2)
- java中怎么表示数组中的某个值_简易Java(12):如何高效检查一个数组中是否包含某个值?...
- 如何踢掉 sql 语句中的尾巴,我用 C# 苦思了五种办法
- mac无法ssh localhost
- 简单暴力到dp的优化(入门篇)
- C#LeetCode刷题之#653-两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)
- B端评分卡在中小企业贷款中使用的三个阶段
- (二)docker常用命令
- Rust: 为什么同样的情况,有时不需要解引用?
- 2008 php mysql_windows 2008配置Nginx+PHP+Mysql
- json转换为对象 java对象数组对象_将json数组转换为java列表对象
- SAP物料编码- -
- html制作免费体验登录页面,Html制作简单而漂亮的登录页面
- 软件测试登陆注册经典测试用例
- Meld——目录文件对比工具
- error: #268: declaration may not appear after executable statement in block
- 多层感知器及常见激活函数-深度神经网络DNN及计算推导
- 如何通过组策略控制远程主机空闲时间自动断开并注销
热门文章
- iOS之AVPlayerViewController的使用oc
- gazebo入门_Gazebo仿真控制中,有哪些你不知道的秘密?
- git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.
- flask 模板 php,Flask四之模板
- centos7开启tcp6_Centos7下配置IPV6
- 二叉树 跳表_面试题之跳表
- uniapp中实现每次点击左侧菜单右边区域都从顶部开始
- 《深入浅出数据分析》第十章第十一章
- php怎么取随机3位数字,使用php怎么从指定数字中获取随机组合
- 在Qt Designer更改部件类名