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 與模數(shù) mod ,求出可以構(gòu)造出的滿足條件的數(shù)組 a 的個數(shù),需要滿足以下條件:
①數(shù)組 a 的長度大于等于 1
②數(shù)組 a 嚴格遞增
③數(shù)組 a 的最小值大于等于 1 ,數(shù)組 a 的最大值小于等于 d
對于數(shù)組 a ,構(gòu)造出一個數(shù)組 b :
i == 1 時:b[ 1 ] = a[ 1 ]
i > 1 時:b[ i ] = b[ i - 1 ] ^ a[ i ]
數(shù)組 b 嚴格遞增。
思路:牽扯到異或,我們就要考慮位運算了。對于a數(shù)組的每一個數(shù),如果它選擇了2,那么它接下來就沒有辦法選擇3;如果它選擇了4,那么它接下來就沒有辦法選擇5,6,7;如果它選擇了8,那么它接下來就沒有辦法選擇9,10,11,12,13,14,15.換句話說,最高位為i的數(shù)字只能出現(xiàn)一次。那么第i位可以選擇的數(shù)量為,但是第i位還有可能不加,也就是在原來基礎(chǔ)上加1.然后累乘就可以了。最后結(jié)果要減一,這代表的是所有位都沒有選擇數(shù)的情況。
代碼如下:
努力加油a啊,(o)/~
總結(jié)
以上是生活随笔為你收集整理的Dreamoon Likes Sequences CodeForces - 1330D(组合数学+位运算)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 幻读是什么?(幻读到底是什么)
- 下一篇: [蓝桥杯][2018年第九届真题]倍数问