POJ-3630-Phone List(字典树)

链接:

https://vjudge.net/problem/POJ-3630

题意:

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:

Emergency 911
Alice 97 625 999
Bob 91 12 54 26
In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.

思路:

建立字典树, 检查每一个结束的点, 后面有没有点了, 如果有说明存在前缀.
否则就没有.

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

LL R[50], M[50];
int n;

LL ExGcd(LL a, LL b, LL &x, LL &y)
{
    if (b == 0)
    {
        x = 1, y = 0;
        return a;
    }
    LL d = ExGcd(b, a%b, x, y);
    LL tmp = y;
    y = x-(a/b)*y;
    x = tmp;
    return d;
}

LL ExCRT()
{
    LL m = M[1], r = R[1], x, y, gcd;
    for (int i = 2;i <= n;i++)
    {
        gcd = ExGcd(m, M[i], x, y);
        if ((r-R[i])%gcd != 0)
            return -1;
        x = (r-R[i])/gcd*x%M[i];
        r -= m*x;
        m = m/gcd*M[i];
        r %= m;
    }
    return (r%m+m)%m;
}

int main()
{
    scanf("%d", &n);
    for (int i = 1;i <= n;i++)
        scanf("%lld%lld", &M[i], &R[i]);
    printf("%lld\n", ExCRT());

    return 0;
}

转载于:https://www.cnblogs.com/YDDDD/p/11576019.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值