Hdu5072 (Anshan regional problem C): refresh, same-color triangle Model

Source: Internet
Author: User

The fourth question on the scene .. At that time, I had no idea. I went back to school and scolded me. I finally passed the lecture by a senior.

Question:
Given the number of N, calculate the number of all or all three tuples that are not mutually qualitative

Let's talk about the same-color triangle model.

Each two points of n points are connected to an edge (red or black) to obtain the number of triangles with the same color as the three sides.

To solve this problem, you only need C (n, 3) minus the number of triangles of different colors.

For each vertex, the triangle of different colors is the number of red edges * The number of black edges, so it can be O (N) calculate the number of triangles of different colors (note that the total number must be divided by 2)

Use C (n, 3) to subtract it.

For this question, if we regard the mutual quality as the red side, and do not regard the mutual quality as the black side, we can convert it into a triangle of the same color.

How can we calculate the number of mutual quality and the number of non-mutual quality?

We can pre-process the number of multiples of each number in the range, then decompose the quality factor for each number, and finally refresh it.

Code:

#include <iostream>#include <stdio.h>#include<string.h>#include<algorithm>#include<string>#include<ctype.h>using namespace std;#define MAXN 1000long long n;int a[100010];int prime[100010];int num[100010];int isnotprime[100010];int num_prime=0;int fac[100010][10];int np[100010];long long gcd(long long a,long long b){    return b?gcd(b,a%b):a;}long long lcm(long long a,long long b){    return a/gcd(a,b)*b;}void setprime(){    for(int i = 2 ; i < MAXN ; i ++)    {        if(!isnotprime[i])             prime[num_prime ++]=i;        for(int j=0;j<num_prime&&i*prime[j]<MAXN;j++)        {            isnotprime[i * prime[j]] = 1;            if(!(i%prime[j] ) )                break;        }    }    return ;}void setfac(int x,int pos){    np[pos]=0;    for(int i=0;i<num_prime;i++)    {        if(!(x%prime[i]))        {            fac[pos][np[pos]++]=prime[i];        }        while(!(x%prime[i]))        {            x/=prime[i];        }    }    if(x>1)    {        fac[pos][np[pos]++]=x;    }}long long iae(int pos){    long long res=0;    for(int i=1;i<(1<<np[pos]);i++)    {        long long mut=1,tmp=0;        for(int j=0;j<np[pos];j++)        {            if(i&(1<<j))            {                mut*=fac[pos][j];                tmp++;            }        }        if(tmp&1)        {            res+=num[mut]-1;        }        else        {            res-=num[mut]-1;        }    }    return res;}void setnum(){    for(int i=2;i<=100000;i++)    {        for(int j=i+i;j<=100000;j+=i)            num[i]+=num[j];    }}int main(){    #ifndef ONLINE_JUDGE        //freopen("in.txt","r",stdin);    #endif    int T;    setprime();    scanf("%d",&T);    while(T--)    {        memset(num,0,sizeof(num));        scanf("%I64d",&n);        for(int i=0;i<n;i++)        {            scanf("%d",a+i);            num[a[i]]++;            setfac(a[i],i);        }        setnum();        long long ans=0;        for(int i=0;i<n;i++)        {            int tmp=iae(i);            ans+=(n-1-tmp)*tmp;        }        ans=n*(n-1)*(n-2)/6-ans/2;        printf("%I64d\n",ans);    }    return 0;}

 

Hdu5072 (Anshan regional problem C): refresh, same-color triangle Model

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.