Even distribution
Time limit3sMemory limit256 MB
Count how many integers occur as the greatest common divisor of candy counts along some walk in the island graph.
- Level
Medium7 of 10
- Topics
- Number theory, Graph, DFS
- Solved
- No attempts yet
Problem
Endre has many nieces and nephews. Once a year he takes some of them on a trip to an archipelago, where a boat company runs two way services between some pairs of islands. Endre and the children can fly directly in and out of any island, so a trip is a nonempty sequence of islands in which consecutive islands and are joined by a boat service. The first and the last island may be the same or different, and an island may be visited more than once.
Every island makes its own kind of candy and gives each arriving group a fixed number of pieces. Endre does not eat candy, but the children finish theirs at once. To keep the peace, every time the group lands on an island and receives candy he splits it evenly among the children. If he brings children, the candy count of every island the trip visits must be divisible by .
The travel agency sends him the trip plan, the sequence , in advance. He wants to bring as many of the children as possible, so he works out the largest number of children he can bring without breaking the even split rule. He brings at least one child. Each trip plan fixes exactly one number of children.
Over the years the number of children has been different every time. Endre wants to know how many group sizes are possible. Count the integers such that some trip plan makes him bring exactly children.
Input
The first line contains two integers and (), the number of islands and the number of boat services. Islands are numbered with distinct integers from to .
The second line contains integers , where is the number of pieces of candy the group receives on island ().
Each of the next lines contains two integers and () describing one boat service, which runs from island to island and from island to island . No pair of islands is joined by more than one service.
Output
Print one line with the number of integers such that some trip plan makes Endre bring exactly children.