UVa 12716 && UVaLive 6657 GCD XOR (数论)

题意:给定一个 n ,让你求有多少对整数 (a, b) 1 <= b <= a 且 gcd(a, b) = a ^ b。

析:设 c = a ^ b 那么 c 就是 a 的约数,那么根据异或的性质 b = a ^ c,那么我们就可以枚举 a 和 c和素数筛选一样,加上gcd, n*logn*logn。

多写几个你会发现 c = a - b,证明如下:

首先 a - b <= a ^ b,且 a - b >= c,下面等于等号,用反证法,假设存在 a - b > c,那么 c < a- b <= a ^ b,然后c = a ^ b矛盾。

然后剩下就好办了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 30000000;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn+1];

int main(){
    memset(a, 0, sizeof(a));
    int m = maxn / 2;
    for(int i = 1; i <= m; i++)
        for(int j = i * 2; j <= maxn; j += i){
            int b = j - i;
            if(i == (b ^ j))   a[j]++;
        }
    for(int i = 2; i <= maxn; i++)  a[i] += a[i-1];

    int cases = 0, T, n;   cin >> T;
    while(T--){
        scanf("%d", &n);
        printf("Case %d: %d\n", ++cases, a[n]);
    }
    return 0;
}
时间: 2024-10-29 03:20:18

UVa 12716 && UVaLive 6657 GCD XOR (数论)的相关文章

uvalive 6657 GCD XOR

//感觉太长时间没做题 好多基本的能力都丧失了(>_<) 首先大概是这样的,因为gcd(a,b)=c,所以a,b都是c的倍数,所以我们依次枚举a的值为2c 3c 4c......,a xor b=c于是有b=a xor c因此可以算出来b,然后再检查下gcd(a,b)是不是为c,这样做是n(logn)^2. 还有一种更优的做法:因为c=gcd(a,b)<=a-b<=a xor b,gcd(a,b)=a xor b,所以c=a-b,所以枚举c后自动满足gcd(a,b)=gcd(a,a

UVA12716 GCD XOR 数论数学构造

题目给你一个N,让你求 两个数字 A,B,且   A>=B<=N,是的 gcd(A,B) == A^B N的范围是 3*10^7大的吓人一开始没敢想构造,因为就算构造开的数组也太大了,已经10^7了,后来想了半天在^运算这里也没有想出来什么,所以没办法还是大胆构造吧,构造就去按照他题目的意思来了,构造两个数字 i,j其中j是i的倍数,那么j + i与i的最大公约数肯定是i了,那么(j+i)^i == i这样构造出来的就算满足了,然后再模仿gcd辗转相除的愿意  把它们放在一个数组里计数,这样预

UVA 12716 GCD XOR(数论+枚举+打表)

 题意:给你一个N,让你求有多少组A,B,  满足1<= B <= A <= N, 且 gcd(A,B) = A XOR B. 思路:首先我们可以得出两个结论: A-B >= A%B >= gcd(A, B) A xor B >= A-B 所以说A xor B >= A-B >= gcd(A, B),然后就可以推出 A xor B = A - B = gcd(A, B) =>    A xor B = A - B  &&  A -

uva 12119 - The Bells are Ringing(数论+枚举)

题目链接:uva 12119 - The Bells are Ringing 题目大意:有三个钟,分别间隔t1,t2,t3秒响一次,0时刻同时响,给定M,问有没又满足的三个数,最小公倍数为M.并且t3-t1<=25 解题思路:因为M为t1,t2,t3的最小公倍数,所以ti一定为M的因子,所以只要枚举因子判断即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace st

UVA 10548 - Find the Right Changes(数论)

UVA 10548 - Find the Right Changes 题目链接 题意:给定a,b,c,表示货物的价值,求由A货物和B货物组成C货物有几种方法,判断有无解和是否有无限多种 思路:扩展欧几里得求通解,去计算上限和下限就能判断 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const long l

【Math】GCD XOR 证明

题目:Given an integer N, and how many pairs (A;B) are there such that: gcd(A;B) = A xor B where 1<=B<=A<=N. 首先先爆一发,妥妥超时.其实真相是我想打表找规律.结果没什么规律可循. 后来分析:要想让GCD(A,B)==(A^B),A和B一定是同样的位数(二进制).因此打表方法可变为:(亦超时) void init() { int K=0; int last=0; for(int i=1;

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

UVA 10375 Choose and divide(数论)

The binomial coefficient C(m,n) is defined as m! C(m,n) = -------- n!(m-n)! Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s). The Input Input consists of a sequence of lines. Each line contains four non-n

HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)

GCD Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output t