Ultraviolet A 12472

Source: Internet
Author: User

Description:

Calculate a minimum binary number of S for the three numbers (the data size is quite large) of the ABC, and the value contains the binary string of P (for example, if P is 101, 1101 meets the requirements ), and A <= S <= B.

----------------------------------------

Question:

I am sorry for this question. At that time, I was so stupid that I didn't have an ac .. Ah.

First, convert A to A binary number string and store it in the array (the subscript is small and the number of digits is small). Then, use 0 to fill it with the same binary number as B. Scanning starts from the place where the subscript is 0. Each time we take out the binary string SA with the same digits as P for comparison. If SA = P, the answer is A, and the program ends. If SA <P, replace the current SA with P, replace all the bits scanned before SA with 0, update the minimum value, and continue scanning. If SA> P, replace the current SA with P, replace all the bits scanned in front of SA with 0, and Add 1 to the heap number in front of SA, I found that the first 0 was changed to 1. I really don't know which one of my ribs is wrong !!), Update the minimum value to continue scanning.

----------------------------------------

Source code:

[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <iostream>
# Include <algorithm>
 
Using namespace std;
# Deprecision MAX 1000000000000001
# Define min (a, B) (a)> (B )? (B) ())
 
Int cha (int * p, long int)
{
Int k = 0;
 
While (a> 0)
{
P [k ++] = a & 1;
A = a> 1;
}
Return k;
}
 
Int cmp (int * a, int I, int pk, long int p)
{
Int k = 0, j = 0;
Long int temp = 0;
 
J = I + pk-1;
For (k = j; k> = I; k --)
Temp = (temp <1) + a [k];
 
If (p> temp) return 1;
Else if (p = temp) return 0;
Else return-1;
}
 
Long int chaback (int * a, int I, int pk, int ak, int * p)
{
Int k = 0, j = 0;
Long int ans = 0;
 
J = I + pk-1;
If (ak-1> j)
{
For (k = ak-1; k> j; k --)
Ans = (ans <1) + a [k];
}
 
For (k = pk-1; k> = 0; k --)
Ans = (ans <1) + p [k];
 
For (k = I-1; k> = 0; k --)
Ans = (ans <1 );
 
Return ans;
}
 
Long int add (long int a, int I, int ak)
{
Long int j = 1, k = 1;
 
J = (j <(ak-I + 1)-1;
K = (k <I)-1;
J = j <I;
 
K = k &;
J = a & j;
J = j> I;
J = j + 1;
J = j <I;
 
Return k | j;
}
 
Int main ()
{
Long int a = 0, B = 0, p = 0, ans = 0;
Int t = 0, k = 0, I = 0, j = 0;
Int at [60], bt [60], pt [60], ak = 0, bk = 0, pk = 0;
 
Scanf ("% d", & t );
For (k = 1; k <= t; k ++)
{
Scanf ("% lld", & a, & B, & p );
Ak = cha (at, );
Bk = cha (bt, B );
Pk = cha (pt, p );
Ans = MAX;
 
For (I = ak; I <bk; I ++)
At [I] = 0;
 
For (I = 0; I <= bk-pk; I ++)
{
If (cmp (at, I, pk, p) = 0)
{
Ans =;
Break;
}
 
If (cmp (at, I, pk, p) = 1)
Ans = min (chaback (at, I, pk, ak, pt), ans );
 
If (cmp (at, I, pk, p) =-1)
{
Long int temp = 0;
Temp = chaback (at, I, pk, ak, pt );
Ans = min (ans, add (temp, I + pk, ak ));
} Www.2cto.com
}
If (ans> B)
Printf ("Case % d: NONE \ n", k );
Else
Printf ("Case % d: % lld \ n", k, ans );
}
 
Return 0;
}


Author: violet_xrym

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.