see-371. Sum of Two Integers

本文介绍了一种使用位运算实现两个整数相加的方法。通过异或运算实现无进位加法,与运算获取进位,并通过循环迭代直至没有进位完成整个加法过程。

利用位运算求二个整数的和:原理异或运算是无进制加法,与运算可求得进位

class Solution {
public:
    int getSum(int a, int b) {
        int res;
        int flag=a&b;//进位
        while(flag)
        {
            res=(a^b); //xor
            flag=flag<<1;//进位左移
            a=res;
            b=flag;
            flag=a&b;//进位
            
        }
        return a^b;
    }
};

 

r-906 & Hatsune Miku - All I Can See Is You Define a sequence of integers a valid if and only if ∀1≤i≤n,0≤ai≤i . Define the weight f(a) of a valid sequence a of length n : Initially, a token is placed at each integer point in the closed segment [1,n] on the number axis. Perform n operations in sequence. During the i -th operation, if ai≠0 , remove a token in the closed segment [ai,i] that has not been removed; otherwise, do nothing. f(a) is the number of ways to remove tokens. Two ways are considered different if there exists a t such that the positions of the tokens removed by the two ways are different at the t -th operation. For example, f([0,2,1])=2 , because we can remove tokens on 2,1 or 2,3 in sequence. JT gives you two integers n,m and asks you to find the sum of the weights over all (n+1)! valid sequences of length n . Since the answer may be too large, print it modulo m . Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000 ). The description of the test cases follows. The only line of each test case contains two integers n and m (1≤n≤5000,108≤m≤1.01⋅109 ) — the length of valid sequences, and the modulus. It is guaranteed that the sum of n2 over all test cases does not exceed 2.5⋅107 . Output For each test case, output an integer — the sum of the weights over all (n+1)! valid sequences of length n , modulo m . Example InputCopy 6 1 1000000007 2 1000000007 3 1000000007 4 1000000007 5 1000000007 114 514191981 OutputCopy 2 7 37 273 2672 393775292 Note In the first test case, valid sequences are [0] and [1] , and the answer is f([0])+f([1])=1+1=2 . In the second test case, valid sequences are [0,0],[0,1],[0,2],[1,0],[1,1],[1,2] . The weight of [0,1] is 2 , and the others are 1 , so the answer is 5⋅1+1⋅2=7 . 會用到啥概念
07-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值