有2n个硬币和一个天平,其中有两个假硬币一个质量为m+1,一个质
问题: 有2n个硬币和一个天平,其中有一个质量为m1,一个质量为m-1,其余质量都为m,用O(logn)的时间复杂度找到这两个假硬币? 解答: 假设2n有k个2进制位。设计k次称量,第i(i=1~k)次是把二进制序号第i位为0的硬币给取出来称。 这样第i次称量的结果如下,左
问题:
有2n个硬币和一个天平,其中有一个质量为m+1,一个质量为m-1,其余质量都为m,用O(logn)的时间复杂度找到这两个假硬币?
解答:
假设2n有k个2进制位。设计k次称量,第i(i=1~k)次是把二进制序号第i位为0的硬币给取出来称。
这样第i次称量的结果如下,左边2列是偏重偏轻的硬币的序号在第i列的二进制值,第3列是第i次称量结果:0 0 正常
0 1 偏轻
1 0 偏重
1 1 正常
经过这k次称量,偏重偏轻的如果同一个二进制位数码不同的话那个位上的值已经知道了。接下来,假设第m位是刚才求出来的序号不同的那一位(这样的位一定存在,否则两枚硬币序号就会相同)。再做k次称量,这次,第i次挑选的硬币是第i位位0并且第m位为0。这样相当于排除了其中一枚硬币以后再O(logn)把它挑出来。挑出一个以后另一个也就挑出来了。
举例:例如有2*3 = 8个硬币,所以k=3(如果2n为14 ,则选取k=4)。对每个硬币编码:000 001 010 011 100 101 110 111。 则先称出个位为零的硬币是不是4m(有四个零),再十位.... 以此类推。。。便可在O(logn)时间内找出假硬币。。。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to get the coins in Bilibili? There are a lot of rewards in this software, and users can use different methods to obtain different rewards. Some users will get additional rewards when they log in to this software. For users who have just registered, how should we get the coin rewards? If you still don’t know how to get the coins inside, then quickly watch the following method of getting the coins on Bilibili brought by the editor of this website. How to get the coins on Bilibili 1. You can get coins by logging in as a regular member. If you become a regular member, you can get a coin directly by logging in to station b every day. 2. Restart before arrival. Automatic distribution may be delayed. If it has not arrived yet, it is recommended.

Solve the "error:redefinitionofclass'ClassName'" problem in C++ code. In C++ programming, we often encounter various compilation errors. One of the common errors is "error:redefinitionofclass 'ClassName'" (redefinition error of class 'ClassName'). This error usually occurs when the same class is defined multiple times. This article will

The clustering effect evaluation problem in the clustering algorithm requires specific code examples. Clustering is an unsupervised learning method that groups similar samples into one category by clustering data. In clustering algorithms, how to evaluate the effect of clustering is an important issue. This article will introduce several commonly used clustering effect evaluation indicators and give corresponding code examples. 1. Clustering effect evaluation index Silhouette Coefficient Silhouette coefficient evaluates the clustering effect by calculating the closeness of the sample and the degree of separation from other clusters.

Known for its powerful performance and versatile features, the iPhone is not immune to the occasional hiccup or technical difficulty, a common trait among complex electronic devices. Experiencing iPhone problems can be frustrating, but usually no alarm is needed. In this comprehensive guide, we aim to demystify some of the most commonly encountered challenges associated with iPhone usage. Our step-by-step approach is designed to help you resolve these common issues, providing practical solutions and troubleshooting tips to get your equipment back in peak working order. Whether you're facing a glitch or a more complex problem, this article can help you resolve them effectively. General Troubleshooting Tips Before delving into specific troubleshooting steps, here are some helpful

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

The label acquisition problem in weakly supervised learning requires specific code examples. Introduction: Weakly supervised learning is a machine learning method that uses weak labels for training. Different from traditional supervised learning, weakly supervised learning only needs to use fewer labels to train the model, rather than each sample needs to have an accurate label. However, in weakly supervised learning, how to accurately obtain useful information from weak labels is a key issue. This article will introduce the label acquisition problem in weakly supervised learning and give specific code examples. Introduction to the label acquisition problem in weakly supervised learning:

The generalization ability of machine learning models requires specific code examples. With the development and application of machine learning becoming more and more widespread, people are paying more and more attention to the generalization ability of machine learning models. Generalization ability refers to the prediction ability of a machine learning model on unlabeled data, and can also be understood as the adaptability of the model in the real world. A good machine learning model should have high generalization ability and be able to make accurate predictions on new data. However, in practical applications, we often encounter models that perform well on the training set, but fail on the test set or real

What are the questions involved in the Yulong 8 Wine Master exam? What is the corresponding answer? How to pass the exam quickly? There are many questions that need to be answered in the Master of Wine Examination activities, and we can refer to the answers to solve them. These questions all involve knowledge of wine. If you need a reference, let’s take a look at the detailed analysis of the answers to the Yakuza 8 Wine Master exam questions! Detailed explanation of answers to questions in the Rulong 8 Wine Master exam 1. Questions about "wine". This is a distilled liquor produced by a distillery established by the royal family. It is brewed from the sugar of sugarcane grown in large quantities in Hawaii. What is the name of this wine? Answer: Rum 2. Question about "wine". The picture shows a drink made from dry ginseng and dry vermouth. It is characterized by the addition of olives and is known as "cockney"
