Principle of tolerance and repulsion
Test instructions: A bit of a matrix for n*m, the lower left corner of the point is (a), the upper right corner of the point (N,m), (in fact, turned back is not affected by the m*n), a person standing in (0,0) look at these points, in a straight line of sight, it can only see the front of that point, the back will be stuck he can't see, ask you, How many points does the man see in total?
This problem as long as the painting is not difficult to find, if a point (x, y), and y have not 1 of the Convention number Z, then they can actually be reduced together (x/z,y/z), try to connect the two points and (0,0), found that they are actually the same line, and (x/z,y/z)
In front, so actually (x, y) is blocked out of sight, which inspired us, if we find X and Y greatest common divisor g, then (x/g,y/g) must be the first point in this line, no other store can block him, he can be seen, and he can't see the point behind, then (x/g,y/ g) The nature of the gratification is that these two numbers coprime
The result is a conclusion that two digits (x, y) can be seen if two numbers are coprime, and if not coprime, then we are going to find out all two tuples (x, y) to make them coprime
We can fix a number and loop it with a number. For example, the matrix is n*m, we fixed m, with N to cycle, namely 1 and [1,m] inside how many number coprime, 2 and [1,m] inside how many number coprime, 3 and [1,m] inside how many number coprime ... n and [1,m] the number of coprime, the results are all added up can be
So the end of the question becomes, given a number x, how to find out how many coprime there are in it and 1 to Y?
Two digital coprime, in fact, they do not have a public quality factor, in turn two numbers have a public quality factor is certainly not coprime, then we can ask the opposite, X and 1 to y how many numbers are not coprime, and then use y minus can
Here we use the principle of repulsion: first find out how many numbers and X have a common quality factor, and then add, and then find how many number and X have 2 public quality factor, then subtract, then find out how many number of number and X have 3 public quality factor, then add ... The last number you get is the number of numbers that are not coprime with X.
Because one of the most basic rules of the repulsion principle is that--
To calculate the size of several sets of merge sets, we first calculate the size of all the individual collections , then subtract all the two sets that intersect , and then add the parts that intersect all the three sets, minus all four sets intersect , and so on, all the time, the part that intersects all the collections is calculated. ( odd Plus, even minus)
#include <iostream>#include<stdio.h>using namespaceStd;typedefLong Longll;BOOLisp[1010];intprime[1010],cnt,pp[1010],cnt0,n,m;ll ans;voidPR () {CNT=0; for(intI=2; i<= +; i++) { if(!Isp[i]) {prime[cnt++]=i; for(intj=i+i;j<= +; j=j+i) isp[j]=true; } }}voidCalintcur) {cnt0=0; for(intI=0;p rime[i]*prime[i]<=cur;i++) { if(cur%prime[i]==0) {pp[cnt0++]=Prime[i]; while(cur%prime[i]==0) cur/=Prime[i]; } } if(cur!=1) pp[cnt0++]=cur;}voidDfsintHavintCurintnum) { if(hav>m| | cur>=cnt0)return ; for(inti=cur;i<cnt0;i++) { inttemp=hav*Pp[i]; if(num&1) ans-=m/temp; Elseans+=m/temp; DFS (Temp,i+1, num+1); }}intMain () {PR (); intT; scanf ("%d",&t); while(t--) {scanf ("%d%d",&n,&m); Ans=m; for(intI=2; i<=n;i++) {cal (i); Ans+=m; for(intj=0; j<cnt0;j++) {ans-=m/Pp[j]; DFS (Pp[j],j+1,2); }} cout<<ans<<Endl; } return 0;}
HDU 2841 Visible Trees number theory + tolerant principle