新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛) H XOR

本文介绍了一个基于XOR运算构建最小生成树的问题。国王XOR希望在N个城市间建立连接,通过计算城市编号间的XOR值作为连接成本,寻找使所有城市相连的最小总成本。文章详细解析了异或运算原理,并给出了一种有效的算法解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Once there was a king called XOR, he had a lot of land. Because of his name, he likes to play XOR games.

One day, he whimpered and wanted to establish N cities on that vast expanse of land, numbered 0, 1, 2..., N-1. He wanted to connect all the cities. If city A can reach City B through zero or one or several cities, then A and B are connected. The cost of repairing a road in City A and City B is the XOR value of number of City A and number of City B. This King XOR wanted to figure out the minimum cost for connecting all of the N cities.

Of course, like a fairy tale read as a child, there will be corresponding rewards after helping the king. If you help the king solve his problems, he will improve your ranking in the competition.

输入描述:
There are multi test cases
each test cases contains an integer N (2 ≤N≤ 20000), the number of cities the king wants to establish.
输出描述:
For each test case, print the minimum cost for connecting all of the N cities in one line.

题意:输入一个数N让你从0-N之间做一个最小生成树(连接两个数之间的消耗就是两个数异或的值)

Fine~

新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)- XOR

链接:https://www.nowcoder.com/acm/contest/116/H

来源:牛客网



题目描述

Once there was a king called XOR, he had a lot of land. Because of his name, he likes to play XOR games.


One day, he whimpered and wanted to establish N cities on that vast expanse of land, numbered 0, 1, 2..., N-1. He wanted to connect all the cities. If city A can reach City B through zero or one or several cities, then A and B are connected. The cost of repairing a road in City A and City B is the XOR value of number of City A and number of City B. This King XOR wanted to figure out the minimum cost for connecting all of the N cities.


Of course, like a fairy tale read as a child, there will be corresponding rewards after helping the king. If you help the king solve his problems, he will improve your ranking in the competition.


输入描述:

There are multi test cases

each test cases contains an integer N (2 ≤N≤ 20000), the number of cities the king wants to establish.

输出描述:

For each test case, print the minimum cost for connecting all of the N cities in one line.

示例1

输入

4

输出

4

说明

The weightof the minimum cost is 1+2+1=4 In the Sample Example.

题意:输入一个数N让你从0-N之间做一个最小生成树(连接两个数之间的消耗就是两个数异或的值)

思路:首先我们先了解一下异或,就是把数字换算成二进制 两个数异或比较如果同位上 相同为0 不同为1

例如 3异或4 就是(假设四位二进制)

   3:0011

   4:0100

  3^4:0111

那么3^4=0111->换算成十进制就是7

那么回到题意,如果一个数想要加入到最小生成树中那么就要异或一个值使得异或的值最小;

异或最小的话那就是只有最低位不相同其他位全部相同;

所以每次只要找出当前数的最低位1的位置就行了;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int n,m;
    while(cin>>n)
    {
        int num=0;
        for(int i=1; i<n; i++)
        {
            int ans=i;
            int cnt=0;
            while((ans&1)==0) 
            {
                ans>>=1;
                cnt++;
            }
            num+=pow(2,cnt);

        }
        cout<<num<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值