Codeforces eduR42 C (enumeration)

Source: Internet
Author: User

Since the Blue Bridge Cup has gone, the daily recovery from today's clock.

Portal: Click to open link

Daily Punch-In (1/1)

Main topic:

Give you a positive integer (1-2E9), asking for at least a few of the numbers to be deleted in order to make it into a full square number. Idea one

Also I think of the idea, for each of the number of N, we have to delete or do not delete the two operations, that is, at most 2^9 times, we can get results.

And how the enumeration became my other problem, previously written by DFS has been wrong.

So, we refer to the enumeration method of a previously written article.

for (int i=0;i< (1<<len); i++)
 Dfs (i,s);
for (int j=0;j<len;j++)
{
	if ((k>>j) &1)
	{
		 del (j);
	}
}

In this way, all the cases are done with the enumeration.

At this time we can choose to delete or not to delete, after the deletion and then determine whether the square number.

In the first writing, I put the value of the string s deleted in 0, and the result WA. This is obviously not advisable, to raise a chestnut, input n=101, the correct result is 2, but the output is all 1, the reason is to delete the end of the 1 for 0, resulting in error.

After the bug is fixed, a new error has occurred, in the above example, the output is still 1. After careful consideration, it is found that if you delete 1 of the Hundred, then the original number is credited to 01, which is still legal.

Thus, a second improvement was made, removing all 0 of the first number of digits.


while (1)
{
	if (Q.front () ==0)
	 Q.pop ();
	else break 
	 ;
}

And then re, it turns out that if you enter 0 (there are other examples), it will die loop ...

Therefore, the recirculation conditions are added. Q.empty ().

Finally, we get the AC solution

#include <bits/stdc++.h> using namespace std;
const int MAXN = 1E5;

const int inf = 0X3F3F3F3F;
string S;

int LEN,ANS,TOT,A[MAXN];
	BOOL Check (string s) {queue<int> q;
	int n = s.length ();
	for (int i=0;i<n;i++) {if (s[i]!= ' * ') q.push (s[i]-' 0 ');
		} while (1&&!q.empty ()) {if (Q.front () ==0) Q.pop ();
	else break;
	} tot = Len-q.size ();
		if (!q.empty ()) {int x = Q.front ();
		Q.pop ();
			while (!q.empty ()) {x = X*10+q.front ();
		Q.pop ();
	  	} int temp = sqrt (x);
  				if (temp*temp==x) {//cout<<x<<endl;
  		return true;
}} return false;

} void del (int x) {a[x] =!a[x];}
	void Dfs (int k,string s) {//cout<<k<<endl;
	memset (A,0,sizeof (a));
	tot = 0;
		for (int j=0;j<len;j++) {if ((k>>j) &1) {del (j);
		}} for (int i=0;i<len; i++) {if (A[i]) {S[i] = ' * ';
	}}//cout<<s<<endl;
	if (check (s)) {ans = min (ans,tot); }} int main () {cin>>s;
	Len = S.length ();
	cout<<len<<endl;
	ans = inf;
	for (int i=0;i< (1<<len); i++) Dfs (i,s);
		if (ans==inf) {cout<<-1<<endl;
	return 0;
	} cout<<ans<<endl;
return 0;  }

Idea two

On the CSDN saw a man who offered a thought.

is to enumerate all the full squares in the range of 1-n, and then compare each number to n, comparing them to the same number of bits, as large as possible.

Obviously, the end result is the length of n-the maximum of the same bit length.

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.