HDU 5778 ABS (brute Force enumeration)

Source: Internet
Author: User

Abs

Time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 399 Accepted Submission (s): 140


Problem Descriptiongiven A number x, ask positive integer y≥2, that satisfy the following conditions:
1. The absolute value of y-x is minimal
2. To prime factors decomposition of Y, every element factor appears.

Inputthe first line of input was an integer T (1≤T≤ )
For each test case,the a single line contains, an integer x ( 1≤x≤ten)

Outputfor each testcase print the absolute value of y-x

Sample Input511124290871699579095

Sample Output23656724470 Description: Give you a long long range of number x, and then ask if you can find a number y, number y in the qualitative factor decomposition, each number just appeared two times, the output is abs (X-y).  The puzzle: When the game did not come out, and then see the puzzle found so water = =. Official problem: As the Y-factor decomposition of each factorization appear 2 times, then Y is a complete square number, set Y=z*z, the title can be converted to Z, so that each factorization appear 1 times. We can brute Force enumeration z, check whether z conforms to requirements, obviously when Z is prime number is compliant, by the prime number theorem can get, Z's enumerator at Logn level complexity O (\sqrt[4]{n}log\sqrt[2]{n}?4??√?N???LOg ? 2 ?? √ ? n ? ?? ) < Span>< Span class= "Mord" > < Span class= "Baseline-fix" > just need to enumerate around X, the scope of the enumeration is very small ... Then z to determine whether the condition is satisfied is also very simple. Code:
1#include <cstdio>2#include <cmath>3#include <iostream>4#include <algorithm>5#include <vector>6#include <stack>7#include <cstring>8#include <queue>9#include <Set>Ten#include <string> One#include <map> A #defineINF 9223372036854775807 - #defineINF 9e7+5 - #definePI ACOs (-1) the using namespacestd; -typedefLong Longll; -typedefDoubledb; - Const intMAXN = 1e5 +5; + Const intMoD = 1e9 +7; - ConstDB EPS = 1e-9; +  A //judge that this number is not satisfied with the condition.  at BOOLOK (ll x) { -      for(LL i =2; I*i <= x; i++) { -         intnum =0; -          while(x% i = =0) {/*Here I meet while the condition must be prime, for what, and the prime number of the principle of the screening method is similar. */ -num++; -X/=i; in             if(Num >1)return false;//if the condition is not met about two times -         } to     } +     return true; - } the  * ll ABSs (ll x) { $     returnX >=0? X:-x;Panax Notoginseng } -  the voidsolve () { +ll N, ans; CIN >>N; All z = sqrt (n+0.5D); the     //cout << z << endl; +      for(LL i =0; ; i++) { -ll tmp = z +i; $         if(TMP * tmp >= n && OK (tmp)) {/*because it does not match n small, so to meet >= n This condition, the test sample when found 8 root is 2, and then 2 is to meet the conditions, at this time found the answer is not right. */ $Ans = ABSS (tmp*tmp-n); -             //cout << tmp << Endl; -              Break; the         } -     }Wuyi      for(LL i =0;; i++) { thell tmp = Z-i; -         if(OK (tmp)) { Wuans = min (ans, ABS (TMP*TMP-n)); -              Break; About         } $     } -     if(N <4) ans =4N/*less than 4 of the root is 1, and then the OK function to determine the 1 is satisfied with the condition, but 1 is not a prime number, so the special sentence. */ -cout << ans <<Endl; - } A  + intMain () the { -     //Cin.sync_with_stdio (false); $     //freopen ("isharp.in", "R", stdin); the     //freopen ("Hh.txt", "w", stdout); the     intT CIN >>T; the  the      while(t--) - solve (); in     return 0; the}

HDU 5778 ABS (brute Force enumeration)

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.