"BZOJ3505" [CQOI2014] triangular numbers

"BZOJ3505" [CQOI2014] triangular numbers

Do not directly ask this question, consider the inclusion and exclusion, first select the three points regardless of whether legitimate program number $ C _ {(n + 1) * (m + 1)} ^ {3} $, then Save to the three-point line number just fine. Obviously can not be enumerated endpoint, we can enumerate consider two points x, y difference i, j, then the number of intermediate points of the whole (gcd (i, j) -1), so that a plurality of square, so (n-i + 1) * (m-j + 1) * (gcd (i, j) -1) * 2, is multiplied by 2 because there are two diagonal lines, but when i = 0 or j = 0 it is you can not take the 2.

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #define int LL
 4 #define LL long long
 5 using namespace std;
 6 int n,m;
 7 int gcd(int a,int b){return !b?a:gcd(b,a%b);}
 8 LL ans;
 9 signed main()
10 {
11     cin>>n>>m;n++,m++;
12     ans=((n*m)*(n*m-1)*(n*m-2)/3)/2;
13     n--,m--;
14     for(int i=0;i<=n;i++)
15         for(int j=0;j<=m;j++)    
16         if(i||j)
17             if(!i||!j)ans-=(n-i+1)*(m-j+1)*(gcd(i,j)-1);        
18             else      ans-=(n-i+1)*(m-j+1)*(gcd(i,j)-1)*2;    
19     cout<<ans<<endl;
20 }
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/Al-Ca/p/11234578.html