7.20 第一场 Mod, Or and Everything

Mod, Or and Everything

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 98 Accepted Submission(s): 62

Problem Description

You are given an integer n.

You are required to calculate (n mod 1) or (n mod 2) or … or (n mod (n - 1)) or (n mod n).

The “or” operation means “bitwise OR”.

Input

The first line contains an integer T(1≤T≤5000)representing the number of test cases.

For each test case, there is an integer n(1≤n≤1012)in one line.

Output

For each test case, print the answer in one line.

Sample Input

5
1
2
3
4
5

Sample Output

0
0
1
1
3

大概题意

就是计算 (n mod 1) or (n mod 2) or … or (n mod (n - 1)) or (n mod n)

思路

第一眼想到了递归,后来发现nint都爆……

这么大的数据,肯定有规律可循,用递归的方法求出前一百个数后对比发现以下规律

一个0

2的0次方个2的0次方减一

2的1次方个2的1次方减一

2的2次方个2的2次方减一

2的3次方个2的3次方减一

以此为根据编写代码即可

代码

//
// Created by Black on 2021/7/20.
//

#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;
long long t, n;

int main() {
//    for (int i = 0; i < 1000; ++i) {
//        cout << i + 1 << " ";
//    }
    cin >> t;
    while (t--) {
        cin >> n;
        if (n == 1) {
            cout << 0 << endl;
            continue;
        }
        int i = 0;
        long long temp = 1;
        while (temp < n) {
            temp *= 2;
            ++i;
        }
        long long res = temp / 2 - 1;
        printf("%lld\n", res);
//        cout << pow(2, i - 1) - 1 << endl;
    }
    return 0;
}
posted @   嘿,抬头!  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示