新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛) H 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个点得到最小生成树的结果其实就是在n-1个点的基础上加上n-1这个点连接0~n-2中的最小权值,由于连接的点都比n-1小,那么与n-1异或值最小的m,应满足 其二进制中除开n-1的二进制数中最后一个1的位置不同,其他位都相同。

毕竟异或是相同就为0,不同就为1。

emmmm 然后就直接打表处理~~

// Asimple
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<" = "<<a<<endl
using namespace std;
typedef long long ll;
const int maxn = 20000 + 5;
ll T, n, sum, num, m, t, len, ans;
ll fa[maxn];


ll qpow(ll n) {
    ll ans = 1;
    ll a = 2;
    while( n ) {
        if( n&1 ) ans *= a;
        a = a*a;
        n >>= 1;
    }
    return ans;
}

ll count(ll n) {
    ll ans = 0;
    while( n%2 == 0 ) {
        n /= 2;
        ans ++;
    }
    return ans;
}

void init() {
    fa[2] = 1;
    for(int i=3; i<maxn; i++) {
        fa[i] = fa[i-1] + qpow(count(i-1));
    }
}

void input() {
    init();
    while( ~scanf("%d", &n) ){
        cout << fa[n] << endl;
    }

}
 
int main() {
    input();
    return 0;
}

 

全部评论

相关推荐

02-12 20:22
重庆大学 Java
字节暑期刚入职四天,因为是年前,所以很多正职都放假走了,也就没有给我分配mt,然后有一个老哥在我来的时候给我发了一个landing手册,然后还有关于部门业务的白皮书,还有一些业务代码。然后本人是java面的,进来第一次接触go语言&nbsp;前面几天熟悉了一下go的语法和go的框架,可以读但是还不太会写,然后业务白皮书也看的很头疼,包括landing手册里要了解的很多东西说实话我看文档真的快看死了,一个嵌套一个,问题是我还完全不知道咋用这个我了解的东西,还有就是那个项目代码,那个老哥喊我去写写单测,熟悉一下go的语法,但也进行的很困难(这是我第一段实习,之前都是springboot那一套,真不太熟悉这个)想问问大家的建议,就是我从现在开始到在开年回来之前应该做些什么,我目前就一个想法&nbsp;就是复现一个landing手册上的go框架小项目&nbsp;就是相当于帮自己锻炼锻炼怎么写go&nbsp;或者各位大佬有没有更好的锻炼go语法的建议还有就是大家都在说vibe&nbsp;coding,那我应该怎么锻炼自己使用ai的能力,感觉我除了给一些需求然后它给我生成代码,好像就没别的用法了,那些什么工作流、拆解、skill啥的都不知道从哪一个地方开始,包括我现在正在实习,不知道精力该怎么分配,去网上想找找关于agent开发的一些学习流程,说实话,众说纷纭,有的是从python开始打基础然后系统学那些rag&nbsp;prompt&nbsp;langchain&nbsp;mcp等等,有的是说直接找一个github上的ai项目然后反复问ai,我确实有点迷茫,恳求各位大佬能留下你们宝贵的建议,我一定认真反复深刻学习有一说一&nbsp;我觉得字节饭挺好吃的!
双非后端失败第N人:1. go语言我建议你让ai带着你先把基本语法速通了,然后再去用go重新刷你以前刷过的leetcode,这样熟悉起来很快 2. 直接看你们组go项目,里面用***比较复杂,然后把每一个语法现象都喂给ai,一点点看
字节跳动公司福利 1371人发布
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务