Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

A. Doggo Recoloring

ps:注意 n == 1

B. Weakened Common Divisor

题解:WCD出现中的数必然是 < a, b >中某个数的公约数。而 < a, b > 的贡献和 a * b 的贡献是等价,所以gcd就好了。

const int N = 150005;

struct node {
    LL sum, x, y;
}a[N];

LL gcd(LL x, LL y) {
    return (!y ? x : gcd(y, x % y));
}

void ca(LL x) {
    for (int i = 2; i <= (int)sqrt(x); ++i) if (x % i == 0) {
        cout << i << endl;
        return ;
    }
    cout << x << endl;
}

LL n;

int main()
{
    sc(n);
    for (int i = 1; i <= n; ++i) {
        sc(a[i].x), sc(a[i].y);
        a[i].sum = a[i].x * a[i].y;
    }

    if (n == 1) {
        cout << a[1].x << endl;
        return 0;
    }

    LL res = gcd(a[1].x, a[2].sum);
    for (int i = 3; i <= n; ++i) {
        res = gcd(res, a[i].sum);
    }

    if (res != 1) {
        ca(res);
        return 0;
    }

    res = gcd(a[1].y, a[2].sum);
    for (int i = 3; i <= n; ++i) {
        res = gcd(res, a[i].sum);
    }

    if (res != 1){
        ca(res);
        return 0;
    }

    cout << "-1" << endl;

    return 0;
}
 

 C. Plasticine zebra

题解:观察可得,分成几部分考虑,中间的最大值,以及头和尾的拼接。(细节啊啊啊啊啊,没想清楚鲁代码是很危险的!!!!),头部如果是奇数,那么尾部和头部是相反的。

const int N = 150005;

string s;

int main()
{
    cin >> s;
    int n = s.size();

    if (n == 1) {
        cout << 1 << endl;
        return 0;
    }

    int res = 1, ans = 0;
    char last = s[0];

    for (int i = 1; i < n; ++i) {
        if (last != s[i]) res++;
        else {
            upd(ans, res);
            res = 1;
        }
        last = s[i];
    }
    if (res) upd(ans, res);


    last = s[0];
    int l = 1, r = 1;
    for (int i = 1; i < n; ++i) {
        if (last != s[i]) l++;
        else break;
        last = s[i];
    }

    if (l & 1) {
        if (last == 'b') last = 'w';
        else last = 'b';
    }
    
    if (s[n - 1] != last) r = 0;
    else {
      for (int i = n - 2; ~i; --i) {
          if (last != s[i]) r++;
          else break;
          last = s[i];
      }
    }

    if ((l != 1 || r != 1) && l + r <= n) {
        upd(ans, l + r);
    }

    cout << ans << endl;

    return 0;
}

 

posted @ 2018-08-20 11:17  天之道,利而不害  阅读(194)  评论(0编辑  收藏  举报