Floyd algorithm for the computer room networking scheme (simple)

Source: Internet
Author: User
Tags gcd

The Computer room networking scheme of Qingyun (simple)

Qingyun is now going toNn A computer room connected to a interconnected network. Engineer Xiao Wang devised a plan: byNn a machine room between the layingN-1N−1 two-way fiber, connect all the engine room. It can be assumed that the optical fiber transmission between the data in two rooms needs11  unit time. Each room  ii  has an initial value  a_i a ? i??, When the initial values of the two engine rooms are coprime, we think the transmission performance between the two rooms is very important. Please help Xiao Wang calculate the sum of the transmission time between all the numerical coprime of the engine room.

Input format

The first line enters a positive integerNN, second line inputNn a positive integerA_1...a_na?1?? a ? nnn  The initial value of the machine room.

Next EnterN-1n−1 lines, each line input two number a , a,B, indicates that There is a two-way network pipeline between room A A and room b b.

For a simple version:N \leq 500n≤500,1 \leq a_i \leq1≤a? I?? ≤50;

For medium versions:N \leq 10000n≤10000, 1 \leq a_i \leq1≤a? I?? ≤500;

For difficult versions:N \leq 100000N≤1000< Span class= "Mord mathrm" >00,a_i \leq 100000 a ? i?? ≤100000.

Output format

Outputs a row that represents all the initial values of the coprime of the machine room to the transmission time and.

Sample input
41 2 3 41 22 33 4
Sample output
8
Prompt information

For the first set of samples, the transfer time for each set of initial value coprime is as follows:

(from) (1,2):11

(1,3) (1,3): 22

(1,4) (1,4): 33

(2,3) (2,3): 11

(3,4) (3,4): 11

Therefore, all initial values coprime the transmission time of the room pair and for  1+2+3+1+1=8 < Span class= "Mord mathrm" >1+2+ 3+1+ 1=8

Xyd gave me this contest and he didn't do it, so I made a

1 /*Floyd algorithm: Directly do it, violence statistics results*/2#include <iostream>3 using namespacestd;4 #defineN 5105 Long LongJz[n][n];6#include <cstdio>7#include <cstring>8 inta[n],n,x,y;9 intgcdintBintc)Ten { One     if(!c) A         returnb; -     returnGCD (c,b%c); - } the voidinput () - { -scanf"%d",&n); -memset (JZ,Ten,sizeof(JZ)); +      for(intI=1; i<=n;++i) -     { +scanf"%d",&a[i]); A     } at      for(intI=1; i<=n-1;++i) -     { -scanf"%d%d",&x,&y); -jz[x][y]=1; jz[y][x]=1; -     } - } in voidfloyed () - { to      for(intk=1; k<=n;++k) +        for(intI=1; i<=n;++i) -          for(intj=1; j<=n;++j) theJz[i][j]=min (jz[i][j],jz[i][k]+jz[k][j]); * } $ intMain ()Panax Notoginseng { - input (); the floyed (); +     Long Longans=0; A      for(intI=1; i<=n;++i) the        for(intj=i+1; j<=n;++j) +       { -           if(GCD (a[i],a[j]) = =1) $           { $ans+=Jz[i][j]; -           } -       } thecout<<ans<<Endl; -     return 0;Wuyi}

Floyd algorithm for the computer room networking scheme (simple)

Related Article

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.