【Codeforces Global Round 1 C】Meaningless Operations

【链接】 我是链接,点我呀:)
【题意】

给你一个a
让你从1..a-1的范围中选择一个b
使得gcd(a^b,a&b)的值最大

【题解】

显然如果a的二进制中有0的话。
那么我们就让选择的b的二进制中对应的位置为1
剩下全为0就好
这样a的二进制全都变成1之后就是答案了(gcd的右边是0)。
但是如果a的二进制里面全是1的话。
就没办法这么构造了
这里有两种情况。
①.1的个数是偶数
那么就101010这样构造
另外一个数就是010101
答案就是010101转换成十进制
②.1的个数是奇数
找不出来规律。。然后就打表了。
(就25个数字>_<)

【代码】

import java.io.*;
import java.util.*;

public class Main {

    static int N = 100;
    static InputReader in;
    static PrintWriter out;
    static int a,q;
    static int bin[];

    public static void main(String[] args) throws IOException{
        in = new InputReader();
        out = new PrintWriter(System.out);

        bin = new int[N+10];
        q = in.nextInt();
        for (int i = 1;i <= q;i++) {
            a = in.nextInt();
            int num = 0,cnt1 = 0;
            int cur = 1;
            while (a>0){
                cur = cur*2;
                num++;
                if (a%2==1) {
                    cnt1++;
                }
                a/=2;
            }
            if (num!=cnt1){
                //exsit zero
                out.println(cur-1);
            }else {
                if(num%2==1) {
                    switch(num) {
                        case 3:
                            out.println(1);
                        break;
                        case 5:
                            out.println(1);
                        break;
                        case 7:
                            out.println(1);
                        break;
                        case 9:
                            out.println(73);
                        break;
                        case 11:
                            out.println(89);
                        break;
                        case 13:
                            out.println(1);
                        break;
                        case 15:
                            out.println(4681);
                        break;
                        case 17:
                            out.println(1);
                        break;
                        case 19:
                            out.println(1);
                        break;
                        case 21:
                            out.println(299593);
                        break;
                        case 23:
                            out.println(178481);
                        break;
                        case 25:
                            out.println(1082401);
                        break;
                    }

                }else {
                    cur = 1;
                    int ans = 0;
                    for (int j = 1;j <= num;j++) {
                        if (j%2==1) {
                            ans = ans + cur;
                        }
                        cur*=2;
                    }
                    out.println(ans);
                }
            }
        }
        out.close();
    }

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;

        public InputReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        }

        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

原文地址:https://www.cnblogs.com/AWCXV/p/10355883.html

时间: 2024-07-29 13:03:02

【Codeforces Global Round 1 C】Meaningless Operations的相关文章

【 Codeforces Global Round 1 B】Tape

[链接] 我是链接,点我呀:) [题意] x轴上有m个连续的点,从1标号到m. 其中有n个点是特殊点. 让你用k段区间将这n个点覆盖. 要求区间的总长度最小. [题解] 一开始假设我们需要n个胶带(即包含每一个点) 然后因为k<=n 所以可能胶带不够用. 那么就得一个胶带跨过两个点. 怎么选择最好呢? 可以把b[i]-b[i-1]-1处理出来排个序. (优先取较小的花费) 然后取前n-k个累加和sum. 因为每取一个就少用一段胶带. 然后sum+n就是答案了 [代码] import java.i

【Codeforces Global Round 1 A】Parity

[链接] 我是链接,点我呀:) [题意] 给你一个k位数b进制的进制转换. 让你求出来转成10进制之后这个数字是奇数还是偶数 [题解] 模拟一下转换的过程,加乘的时候都记得对2取余就好 [代码] import java.io.*; import java.util.*; public class Main { static int N = (int)1e5; static InputReader in; static PrintWriter out; static int b,k; static

【Codeforces Global Round 1 E】Magic Stones

[链接] 我是链接,点我呀:) [题意] 你可以把c[i]改成c[i+1]+c[i-1]-c[i] (2<=i<=n-1) 问你能不能把每一个c[i]都换成对应的t[i]; [题解] d[i] = c[i+1]-c[i]; (1<=i<=n-1) change c[i] c[i]' = c[i+1]+c[i-1]-c[i]; d[i-1] = c[i]'-c[i-1]; = c[i+1]+c[i-1]-c[i]-c[i-1] == c[i+1]-c[i] = d[i]; d[i]

【手抖康复训练1 】Codeforces Global Round 6

[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思路手抖过不了样例,C题秒出思路手抖过不了样例*3 D题 手抖 过的了样例 ,调了1h,赛后发现变量名写错了,改一个字符就能AC... 题目等补完题一起放上来QAQ 原文地址:https://www.cnblogs.com/ttttttttrx/p/12110199.html

Codeforces Global Round 1 (A-E题解)

Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^(k-1)+a2*b^(k-2)+...+ak*b^0的奇偶性. 题解: 暴力求模2意义下的值就好了. 代码如下: #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+5; int

C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题

C. Meaningless Operations time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.

[Codeforces]Codeforces Global Round 1

A - Parity 题意 给定一个$b$进制数,要求输出它在十进制下是奇数还是偶数. 分析 花了我略多的时间,首先题目中给的数字范围很大,不能直接转化为10进制. 分析性质,发现只有奇数乘奇数还是奇数,其他都是偶数. 对奇数进制和偶数进制分类讨论. 偶数进制看最低位的奇偶性,如果是奇数那么这个数就是奇数,不然是偶数. 奇数进制看每一位上奇数的个数,如果是奇数个奇数就是奇数,不然是偶数. 代码 1 #include <bits/stdc++.h> 2 using namespace std;

Codeforces Global Round 1

A. Parity 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 100010 5 int b, k, a[N]; 6 7 int main() 8 { 9 while (scanf("%d%d", &b, &k) != EOF) 10 { 11 int res = 0; 12 for (int i = 1; i <= k; ++i) scanf("%d&

Codeforces Global Round 7【ABCD】(题解)

目录 涵盖知识点:思维.构造.马拉车. 比赛链接:传送门 D题只有数据范围的区别,故只写D2. 好多题啊,随缘更新.(其实懒得写) A - Bad Ugly Numbers B - Maximums C - Permutation Partitions D2 - Prefix-Suffix Palindrome (Hard version) 涵盖知识点:思维.构造.马拉车. 比赛链接:传送门 D题只有数据范围的区别,故只写D2. 好多题啊,随缘更新.(其实懒得写) A - Bad Ugly Nu