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(组合数学+位运算)相关推荐

  1. Codeforces Round #631 (Div. 2) D. Dreamoon Likes Sequences 思维 + 组合数学

    传送门 文章目录 题意: 思路: 题意: 给你d,modd,modd,mod,让你求能构造出如下序列aaa的个数模modmodmod: 思路: 首先可以发现aaa的长度不能超过log2dlog_2dl ...

  2. CodeForces - 1330D Dreamoon Likes Sequences(组合数学)

    题目链接:点击查看 题目大意:给出一个限制 d 与模数 mod ,求出可以构造出的满足条件的数组 a 的个数,需要满足以下条件: 数组 a 的长度大于等于 1 数组 a 严格递增 数组 a 的最小值大 ...

  3. Dreamoon Likes Coloring CodeForces - 1330C(贪心+思维)

    Dreamoon likes coloring cells very much. There is a row of n cells. Initially, all cells are empty ( ...

  4. Powered Addition CodeForces - 1339C(位运算)

    You have an array a of length n. For every positive integer x you are going to perform the following ...

  5. D. Dreamoon Likes Sequences(异或)

    Problem - 1330D - Codeforces 题意: 给你两个整数d,m,找出数组a的数量,满足以下约束. a的长度为n,n≥1 1≤a1<a2<⋯<an≤d 定义长度为 ...

  6. CodeForces - 1457D XOR-gun(位运算+暴力)

    题目链接:点击查看 题目大意:给出一个长度为 n 的非降序列,现在可以执行数次操作:选择相邻的两个位置将其替换成异或和,更具体的,每次操作可以选择一个位置 i ,满足 1 <= i < n ...

  7. Codeforces Round #631 (Div. 2) C. Dreamoon Likes Coloring 构造

    传送门 文章目录 题意: 思路: 题意: 思路: 针灸思维不行,数据结构来凑呗. 一开始做的时候想简单了,一直wawawa,后来想到了hackhackhack样例,开始换思路构造,结果死活想不到O(m ...

  8. 【CodeForces 1042B --- Vitamins】DP+位运算

    [CodeForces 1042B --- Vitamins]DP+位运算 题目来源:点击进入[CodeForces 1042B - Vitamins] Description Berland sho ...

  9. CodeForces - 1451E2 Bitwise Queries (Hard Version)(交互+构造+位运算)

    题目链接:点击查看 题目大意:给出一个长度为 n(n 保证了是 2 的幂次),每个数的范围在 [ 0 , n - 1 ] 的一个数组,现在要求通过有限次操作确定下来这个数组: 询问 a[ i ] xo ...

最新文章

  1. PHP 字符处理 加密解密 函数收集
  2. js_jQuery【下拉菜单联动dom操作】
  3. zookeeper 源码阅读(2)
  4. java中怎么表示数组中的某个值_简易Java(12):如何高效检查一个数组中是否包含某个值?...
  5. 如何踢掉 sql 语句中的尾巴,我用 C# 苦思了五种办法
  6. mac无法ssh localhost
  7. 简单暴力到dp的优化(入门篇)
  8. C#LeetCode刷题之#653-两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)
  9. B端评分卡在中小企业贷款中使用的三个阶段
  10. (二)docker常用命令
  11. Rust: 为什么同样的情况,有时不需要解引用?
  12. 2008 php mysql_windows 2008配置Nginx+PHP+Mysql
  13. json转换为对象 java对象数组对象_将json数组转换为java列表对象
  14. SAP物料编码- -
  15. html制作免费体验登录页面,Html制作简单而漂亮的登录页面
  16. 软件测试登陆注册经典测试用例
  17. Meld——目录文件对比工具
  18. error: #268: declaration may not appear after executable statement in block
  19. 多层感知器及常见激活函数-深度神经网络DNN及计算推导
  20. 如何通过组策略控制远程主机空闲时间自动断开并注销

热门文章

  1. iOS之AVPlayerViewController的使用oc
  2. gazebo入门_Gazebo仿真控制中,有哪些你不知道的秘密?
  3. git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.
  4. flask 模板 php,Flask四之模板
  5. centos7开启tcp6_Centos7下配置IPV6
  6. 二叉树 跳表_面试题之跳表
  7. uniapp中实现每次点击左侧菜单右边区域都从顶部开始
  8. 《深入浅出数据分析》第十章第十一章
  9. php怎么取随机3位数字,使用php怎么从指定数字中获取随机组合
  10. 在Qt Designer更改部件类名