HDU 5072 Coprime same-color triangle
Okay, I admit that I couldn't do it even if I had five more hours.
First, we will explain the same-color triangle problem:
Given n (n> = 3) points, some of these points are painted in red, and the rest are painted in black. Then they are connected to each other, so every three points will form a triangle,
That is, there are a total of sum = C (3, n) triangles. For a triangle, if the color of the three points is the same, the triangle is called the same color.
A very intuitive way of thinking is to refresh the number of sum-exclusive triangles ans.
Ans = (sigma (Xi * Yi)/2; (1 <= I <= n, Xi, yi indicates the number of red and black points connected to vertex I, respectively .)
When the status is poor, the code is written like shit.
#include #include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker, "/STACK:1024000000")#define EPS (1e-8)#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3fusing namespace std;int divi[100010][130];bool is[100010];int num[100010];int mem[100010];int ch[1001];int Check(int x){ int ans = 0; while(x) ans += (x&1),x >>= 1; return ans&1 ? 1:-1;}int main(){ int n = 100000,i,j,k; for(i = 0;i <= 1000; ++i) ch[i] = Check(i); for(i = 1;i <= n; ++i) divi[i][0] = 0; memset(is,false,sizeof(is)); for(i = 2;i <= n; ++i) { if(is[i] == false) { divi[i][++divi[i][0]] = i; for(j = i+i;j <= n; j += i) { divi[j][++divi[j][0]] = i; is[j] = true; } } } int Max,Mul,t; int wf; for(i = 1;i <= n; ++i) { Max = (1<= 1; --k,t <<= 1) { if((j&t) && j != t) Mul *= divi[i][k]; } if(Mul != 1) divi[i][++divi[i][0]] = Mul*ch[j]; } } int T,tmp; LL ans,sum; int Top; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(is,false,sizeof(is)); for(i = 1,Top = 0;i <= n; ++i) { scanf("%d",&num[i]); is[num[i]] = true; Top = max(Top,num[i]); } ans = 0; memset(mem,-1,sizeof(mem)); LL anw = 0; for(i = 1;i <= n; ++i) { tmp = num[i]; ans = 0; for(j = divi[tmp][0];j >= 1; --j) { if(mem[abs(divi[tmp][j])] != -1) sum = mem[abs(divi[tmp][j])]*(divi[tmp][j]/abs(divi[tmp][j])); else { sum = 0; for(k = abs(divi[tmp][j]);k <= Top; k += abs(divi[tmp][j])) sum += is[k] ? 1 : 0; mem[abs(divi[tmp][j])] = sum; sum *= (divi[tmp][j]/abs(divi[tmp][j])); } ans += sum; } if(ans) anw += (n-ans)*(ans-1); } LL tn = n; printf("%I64d\n",tn*(tn-1)*(tn-2)/6 -anw/2); } return 0;}