Codeforces 990G. GCD Counting

14 篇文章 0 订阅
9 篇文章 0 订阅

题目连接:http://codeforces.com/contest/990/problem/G

题目描述:

G. GCD Counting
time limit per test
4.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a tree consisting of nn vertices. A number is written on each vertex; the number on vertex ii is equal to aiai.

Let's denote the function g(x,y)g(x,y) as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex xx to vertex yy (including these two vertices).

For every integer from 11 to 21052⋅105 you have to count the number of pairs (x,y)(x,y) (1xyn)(1≤x≤y≤n) such that g(x,y)g(x,y) is equal to this number.

Input

The first line contains one integer nn — the number of vertices (1n2105)(1≤n≤2⋅105).

The second line contains nn integers a1a1a2a2, ..., anan (1ai2105)(1≤ai≤2⋅105) — the numbers written on vertices.

Then n1n−1 lines follow, each containing two integers xx and yy (1x,yn,xy)(1≤x,y≤n,x≠y) denoting an edge connecting vertex xx with vertex yy. It is guaranteed that these edges form a tree.

Output

For every integer ii from 11 to 21052⋅105 do the following: if there is no pair (x,y)(x,y) such that xyx≤y and g(x,y)=ig(x,y)=i, don't output anything. Otherwise output two integers: ii and the number of aforementioned pairs. You have to consider the values of ii in ascending order.

See the examples for better understanding.

Examples
input
Copy
3
1 2 3
1 2
2 3
output
Copy
1 4
2 1
3 1
input
Copy
6
1 2 4 8 16 32
1 6
6 3
3 4
4 2
6 5
output
Copy
1 6
2 5
4 6
8 1
16 2
32 1
input
Copy
4
9 16 144 6
1 3
2 3
4 3
output
Copy
1 1
2 1
3 1
6 2
9 2
16 2
144 1

题解:由于2e5内gcd不会有太多种,所以直接暴力回溯。

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
int val[maxn];
ll cnt[maxn];
vector<int>g[maxn];
map<int, ll> c[maxn];

void dfs(int u, int p = 0)
{
    c[u][val[u]] ++;
    for(int v : g[u]) {
        if(v != p) {
            dfs(v, u);
            for(auto itu : c[u]) {
                for(auto itv : c[v]) {
                    cnt[__gcd(itu.first, itv.first)] += itu.second * itv.second; //先统计个数
                }
            }
            for(auto itv : c[v]) {
                c[u][__gcd(val[u], itv.first)] += itv.second; //在进行更新.
            }
            c[v].clear(); //must clear, or it will be MLE
        }
    }
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++) {
        cin >> val[i];
        cnt[val[i]] ++;
    }
    for(int i = 1;i < n;i ++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1);
    for(int i = 1;i < maxn;i ++) {
        if(cnt[i]) cout << i << ' ' << cnt[i] << '\n';
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值