#题目:GCD XOR UVA - 12716

本文探讨了一道关于寻找满足特定条件的整数对的数学问题,即当两个数的最大公约数等于这两个数的异或值时,如何在一定范围内找出所有符合条件的数对。文章分享了解题思路,包括利用异或的性质和通过筛法优化计算过程,并提供了详细的代码实现。
部署运行你感兴趣的模型镜像

题目描述

Given an integer N, find how many pairs (A, B) are there such that: gcd(A, B) = A xor B where
1 ≤ B ≤ A ≤ N.
Here gcd(A, B) means the greatest common divisor of the numbers A and B. And A xor B is the
value of the bitwise xor operation on the binary representation of A and B.

input
The first line of the input contains an integer T (T ≤ 10000) denoting the >number of test cases. The
following T lines contain an integer N (1 ≤ N ≤ 30000000).
output
For each test case, print the case number first in the format, ‘Case X:’ (here, X is the serial of the
input) followed by a space and then the answer for that case. There is no new-line between cases.
Sample Input
2
7
20000000
Sample Output
Case 1: 4
Case 2: 34866117

题意:

多组数据输入,每组数据输入一个n,问在小于n的数字里面能找到多少对数满足gcd(a,b)==a^b(大于b)

解题思路:

根据异或的性质我们可以发现,a^gcd(a,b)=b;
而gcd(a,b)又是a的约数,所以我们可以枚举c,就可以像筛素数那样做,然后在暴力小数据打表的时候发现gcd(a,b)=a-b,证明详见紫书318页。所以我们在筛的时候就找a^(a-b)是否等于b就行

代码

#include <iostream>
using namespace std;

int gcd (int a,int b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
int a[300000005];

void init()
{
    a[1]=0;
    a[2]=0;

    for(int i=1;i<300000007;i++)
    {
        for(int j=i+i;j<30000007;j+=i)
        {
            int tmp = j-i;
            if((j^tmp) == i)
            {
                a[j]++;
            }

        }
        a[i]+=a[i-1];
    }


}

int main()
{
   init();
   int t;
   cin>>t;
   int flg=1;
   while(t--)
   {
    int n;
    cin>>n;
    int ans=a[n];
    cout<<"Case "<<flg++<<": "<<ans<<endl;

   }
   return 0;
}

总结:

要多去想规律,比如说看到异或就要想到:如果a^b=c那么a^c=b,然后还要结合打表。

转载于:https://www.cnblogs.com/IAMjm/p/9429146.html

您可能感兴趣的与本文相关的镜像

Kotaemon

Kotaemon

AI应用

Kotaemon 是由Cinnamon 开发的开源项目,是一个RAG UI页面,主要面向DocQA的终端用户和构建自己RAG pipeline

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值