Codeforces 1110C (思维+数论)

题面

传送门

分析

这种数据范围比较大的题最好的方法是先暴力打表找规律

通过打表,可以发现规律如下:

定义\(x=2^{log_2a+1}\) (注意,cf官方题解这里写错了,官方题解中定义\(x=2^{log_2a}\)是有问题的

(1) 若\(a \neq 2^x-1\)

? 则当\(b=(2^x-1)\) xor a时a xor b=b=\(2^x-1\) ,a and b=0

? gcd(a xor b,a and b)=\(2^x-1\)有最大值

? (异或的性质,若a xor c =b ,则a xor b=c)

? 举个例就很显然了:(注意,例子中数均用二进制表示)

a=101,2^x-1=111
b=101 xor 111 =010
a xor b =111
a and b =000
gcd(111,000)=111

(2) 若\(a=2^x-1\)

? gcd(a xor b ,a and b)=\(gcd(2^x-1-b,b)\)

? 由于\(gcd(x,y)=gcd(x-y,y)\)

? \(gcd(2^x-1-b,b)=gcd(2^x-1,b)\)

? 所以我们只要找出\(2^x-1\)的最大且不为\(2^x-1\)的因数即可,可以直接试除法

?

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
inline int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int divide(int x){
    int sq=sqrt(x);
    int ans=1;
    for(int i=2;i<=sq;i++){
        if(x%i==0){
            ans=max(ans,i);
            ans=max(ans,x/i);
        }
    }
    return ans;
} 

int count(int x){
    int ans=0;
    while(x>0){
        x>>=1;
        ans++;
    }
    return ans;
} 

int solve(int x){
    int l=count(x);
    if((1<<l)-1==x) return divide(x);
    else return (1<<l)-1;
}

int main(){
    int q,x;
    scanf("%d",&q);
    while(q--){
        scanf("%d",&x);
        printf("%d\n",solve(x));
    }
}

原文地址:https://www.cnblogs.com/birchtree/p/10356770.html

时间: 2024-08-29 07:50:22

Codeforces 1110C (思维+数论)的相关文章

Codeforces 223APartial Sums 数论+组合数学

题意很简单,求不是那么好求的,k很大 要操作很多次,所以不可能直接来的,印象中解决操作比较多无非线段树 循环节 矩阵 组合数等等吧,这道题目 也就只能多画画什么 的了 就以第一个案例为主吧 , 3 1 2 3 k我们依据画的次数来自己定好了 下面的每个数表示这个位置的 数由最初的 数组num[]中多少个数加起来得到的 当k为0的时候呢,就是 1 1 1 k为1的时候呢 1 2 3 k为2的时候呢 1 3 6 那么k为3的时候 1 4 10 这里看一下 从数组下标0开始,那么其实就是 C(i +

Maximal GCD CodeForces - 803C (数论+思维优化)

C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1,?a2,?...

codeforces 735D Taxes(数论)

Maximal GCD 题目链接:http://codeforces.com/problemset/problem/735/D --每天在线,欢迎留言谈论. 题目大意: 给你一个n(2≤n≤2e9) 代表一个人的收入. 他需要交税,规则:交税金额为n的最大公约数(本身不算) 他想通过把钱分成几份,然后分别交税,达到交税最少. 知识点: 哥德巴赫猜想:①如果一个数为偶数,那么可以拆成两个质数相加 ②如果一个奇数 (n-2)为质数那么他也可以拆成两个质数相加(2+(n-2)) ③其他的奇数 可以拆成

【暑期集训第一场】欧拉回路 | 思维 | 数论构造 | 容斥原理 | 线段树 | 归并排序

集训1(HDU2018 Multi-University Training Contest 2) ID A B C D E F G H I J AC O O 补题 ? O ? O 代码 & 简易题解 [A]:期望? 神仙题,留坑.. [B]:?? 同\(\text{A}\) [C]:求欧拉通路条数,以及每条的路径 小学数竞里有讲过,无向图一笔画的充要条件是有零个或两个"奇点"(偶点个数不限),"奇点"在这里就是指度为奇数的点... 其实上面两种情况就分别对应

Codeforces 490D Chocolate(数论)

题目链接:Codeforces 490D Chocolate 两个种变换方式无疑是减掉一个因子3加上一个因子2和减掉一个因子2,所以从因子的角度出发,如果两组数存在不同的质因子肯定是不可以的.剩下的就是构造答案了. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int maxn = 105; int A

Queue CodeForces - 353D (思维dp)

https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为位置$i$处的$F$复位所花费时间, 有 $dp[i] = max(dp[i-1]+1,cnt_i)$, $cnt_i$为前$i$位$M$的个数 $dp$最大值即为答案 #include <iostream> #include <algorithm> #include <cstd

CodeForces 1131B(思维题)

You still have partial information about the score during the historic football match. You are given a set of pairs (ai,bi)(ai,bi), indicating that at some point during the match the score was "aiai: bibi". It is known that if the current score

CodeForces - 417B (思维题)

Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description During the "Russian Code Cup" programming competition, the testing system stores all sent solutions for each participant. We know th

CodeForces - 417A(思维题)

Elimination Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The finalists of the "Russian Code Cup" competition in 2214 will be the participants who win in one of the elimination rounds.