SOJ3191 of Problem solving report free square

Source: Internet
Author: User

SOJ3191 of Problem solving report free square

Description

Time limit:5000 MS Memory limit:65536 KDescriptionA Positive integer is said to being squarefree if it is divisible by no perfect square larger than 1. For example, the first few squarefree numbers is {1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, ...}. Can you tell me the k-th (1-indexed) smallest squarefree number. InputThe first line of the input would be an integer to represent the number of test cases. The For all test case there are only one line contains only one integer K. (1 <= k <= 10^9) There is a blank line befor e each test case.OutputThe For all test case output The answer to a single line:the k-th smallest squarefree number.Sample Input6127100012345671000000000Sample Output1210163720307451644934081Source

The main idea: the non-square component number is defined as a number cannot be divisible by any >1, then it is a non-square component number, and now asks you what is the K non-square constituent number? Note that K Max is 10^9.
analysis: Such a big data, certainly not a try it, then how to do? In fact, this question is the principle of tolerance, remember the filter interval is any number of multiples of the question? The idea of this problem is similar: first assume that all the numbers in the interval meet the conditions, and then subtract those that are a number of square multiples of the number, plus the same time is a certain two square number of times the number, minus the same time is three square number times number, ... Note, however, that the selection criteria for the number of squares used in this filter appears at most once per prime as a factor. Because if a mass factor occurs more than once, it causes unnecessary re-screening and increases the steps of the repulsion.
then we first get the prime number within 250, through the primes within 250 to determine whether the numbers within 500000 should be added or reduced or not concerned (depending on the quantity of its quality factors). In 1~50000, if a number has a duplicate quality factor, we do not use it, the coefficient array co[i]=0; If a number has an odd number of distinct qualitative factors, then the allowance should be reduced (because it is a square number), co[i]=-1; If a number has an even number of non-repeating mass factors, The explanation should be added (because it is much less), co[i]=1.
after we have constructed the coefficient matrix, we begin to tolerate the number of non-square components before a certain number. This is because the number of the question is how many, clever use of two points, if the number is assumed to be mid, if you can meet then the mid-cap should be >=k.

The meaning of the final output answer is that the number of <=ans has k non-square component number, then ans is the k non-square component number.
on the code:< /span>
#include <iostream> #include <algorithm>using namespace std;typedef long long ll;const int MAXN = 4000;const int MAXM = 100100;int isprime[maxn];int prime[maxn];int co[maxm];int cnt;void getp ()//Linear prime Sieve {for (int i = 1; i < MAXN; i++) Isprime[i] = 1;for (int i = 2; i < MAXN; i++) {if (Isprime[i]) prime[cnt++] = i;for (int j = 0; J < CNT&AMP;&AMP;I*PR IME[J] < MAXN; J + +) {Isprime[i*prime[j]] = 0;if (i%prime[j] = = 0) break;}}} void Getco ()//Calculate prime Matrix {for (int i = 2; i < MAXM; i++) {co[i] = 1; int tem = i;for (int j = 0; prime[j] * Prime[j] <= tem ; J + +) {if (tem%prime[j] = = 0) {Co[i] =-co[i];//found a mass factor changes the addition and subtraction of the tolerance tem/= PRIME[J];} if (tem%prime[j] = = 0) {Co[i] = 0;//is not in our consideration break;}} if (Tem > 0&&co[i]!=0) co[i] =-co[i];}} int main () {int Kase, n;cin >> kase;getp (); Getco (); while (kase--) {cin >> n;ll l = 1, R = 2e10;ll Mid, Tem,ans;wh Ile (L <= R)//two score, find the number of K {mid = L + (r-l)/2;tem = Mid;for (Long long i = 2; i*i <= mid; i++) {tem = tem + (Mid/(  i*i)) *co[i]; //mid/(I*i) calculates how many numbers in the mid range are divisible by (i*i) and then multiplied by the repulsion factor}if (TEM >= n) {r = Mid-1;ans = Mid;} Elsel = mid + 1;} cout << ans << endl;} return 0;}

The principle of tolerance is really difficult to look, continue to cultivate.

SOJ3191 of Problem solving report free square

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.