Tolerance principle: POJ 3904 Sky Code

Source: Internet
Author: User

We try to understand the repulsion from the perspective of set theory.

Three circles and events can be represented as

Generalization to N-sets of a repulsion relationship

Can get such a relationship: odd Plus, even minus example one. POJ 3904 Sky Code

Main Topic

Give a string of numbers to solve the number of coprime four tuples (note that 22 coprime)

Thinking of solving problems

There are a lot of code on the Internet, but the detailed explanation is very few, here combined with the author's thoughts in detail on the problem-solving ideas, patience a look.

First, it is easy to think, want to calculate the number of coprime four yuan, and then the total minus, the key is how to count the number of coprime four tuples.
Enumeration of the number of conventions, for the same four-tuple (6,12,18,36) may have both the Convention number 2, the Convention number 3, and the Convention number 6, which we counted to 2 in the enumeration process. 3. It's still 6.
Let it be. The number of the Convention has an odd number of factors on the +, even a number of factors on the-(2, 3 contain a factor on the Plus, 6 contains 2, 32 factors are reduced); To find the number of the data to be read into each of the elements of the factor decomposition, a combination of elements, record combinations to get the factor k contains several factors, ; open a global array cnt[k] count the number of data containing the factor K. Then according to the tolerance theorem, if the factor k contains an even number of elements (such as 6=2x3) minus, odd number of elements (such as 2=2,5=5,30=2x3x5) plus, add up to get not coprime four of the total number of tuples.

There are several key points
Key 1: understanding Cnt[i] Indicates the number of data containing factor I (2,4,6,8,10 this set of data cnt[i]=5)
Num[i] Indicates the number of factors that factor I contains (num[6]=2, minus)

Key 2: how to get cnt[i], num[i], or how to implement a combination of the element factor. Using the idea of a binary representation , the 1 of the J-bit indicates that the J-factor participates in the multiplicative, and 0 indicates that it does not participate in the multiplicative, for example, the containing factor 2*3*5*7,1010 means that 2*1*5*1,0001 represents 1*1*1*7, and so on ... In this way, only factor factor decomposition of the record containing the number of elements factor tol, then there is tol bit binary, note that 2*3*5*7*11*13>10000, all up to 6-bit binary; for the number I in any range, traverse all bits J (from low to High),i& (1 j) ==1, which indicates that the J-bit binary of I is 1, which means that the J-factor participates in the multiplicative; in this sense, all binary digits J, updated array Cnt[k], and num[k], which correspond to all the numbers I and I in the binary range, are iterated.

Key 3: The author mistakenly understood that the prime factor combination is not an integer decomposition to get all the approximations , got the wrong num[i] and cnt[i], for example: 2,4,8,16,32 This set of data, corresponding to the
CNT = 5, 4, 3, 2, 1
num = 1, 1, 1, 1, 1
It is impossible to have coprime four yuan, but in the time will be cnt[2], cnt[4] are counted in, the reason is that I did the approximate decomposition, is not a combination of factors ...

The correct answer is
CNT = 5, 0, 0, 0, 0
num = 1, 0, 0, 0, 0 (because only one factor 2 cannot be combined out of 4,8,16,32)

Well, refer to the code to understand the chant.

Reference code + partial comment

#include <iostream> #include <cstdio> #include <algorithm> #include <map> #include <vector
> #include <cstring> #include <cmath> #include <climits> #define EPS 1e-8 using namespace std;
typedef long Long LL;
const int Maxx=int_max;
const int MAXN = 1E4+10;
int N,M,A[MAXN],CNT[MAXN],PRIME[MAXN],NUM[MAXN]; ll P[MAXN]; P[i] represents the combined number C (i,4) void Init () {for (ll i=4;i<maxn;i++) p[i]=i* (i-1) * (i-2) * (i-3)/24;//combined number preprocessing return;} void Divi
  De (int n) {int tol=0;
    for (int i=2;i*i<=n;i++) if (n%i==0) {prime[tol++]=i;
    do{n/=i;
  }while (n%i==0);    } if (n>1) prime[tol++]=n;
    For n factor decomposition, Tol represents the number of factors for the factor for (int i=1;i< (1<<tol); i++) {//using the binary of each bit 0, 1 to the element factor combination, such as: 0001~1111 int k=1;
    int sum=0; for (int j=0;j<tol;j++)//from low to high, find I in which one is 1 if (i& (1<<j)) {//and operation, J bit is 1, execute if statement k*=prime[j        ];
    Multiplicative factor sum++; } Cnt[k]++;//cnt[i] Indicates the number of data containing factor I num[k]=sum;//num[i]Represents the number of elements that I contain}} int main () {//Freopen ("Input.txt", "R", stdin);
     Init ();
     memset (num,0,sizeof (num));
       while (cin>>n) {memset (cnt,0,sizeof (CNT));
           for (int i=0;i<n;i++) {cin>>m;              Divide (m);
       Factor decomposition, and statistics related data} ll ans=0; for (int i=2;i<maxn;i++) if (cnt[i]>=4) {//pruning, cnt[i] must be greater than or equal to four if (num[i]&1) ans+=p[cnt [i]];/
        /If the number of elements of the element is odd, add; otherwise minus else ans-=p[cnt[i]]; } cout<<p[n]-ans<<endl;
   Finally, the total minus the non-conforming four-tuple number for (int i=2;i<=8;i++) cout<<num[i]<< "";cout<<endl;
} return 0;
 }

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.