google kickstart 2018

Round A

https://codejam.withgoogle.com/codejam/contest/9234486/dashboard

A

题意

给一个数,每次可以加一或减一,求最少操作次数,使得最后这个数的十进制表示中每个位都是偶数。

数据范围

Limits
1 ≤ T ≤ 100.
Small dataset
1 ≤ N ≤ 10e5.
Large dataset
1 ≤ N ≤ 10e16

题解

显然少的操作次数为一直减少或增加知道满足题意。
对小的数据集,最多减少到0,也就是暴力O(n)的复杂度即可。
对大数据集,大力推敲一番可发现可以直接求出第一个比他小和比他大的数,然后相减取最小即可。
对于一个数n,设其最高的奇数位上为m。即:偶偶偶偶m___。则减小时应该减到偶偶偶偶(m-1)8888。
增加时则为偶偶偶偶(m+1)0000.(m=9时除外,此时产生进位)。

B

题意

有一个长为n的数组,从中随机抽取一个数出来,有K次机会放回重抽,求采取最佳策略下取出来的数的期望。

数据范围

Limits
1 ≤ T ≤ 100.
1 ≤ Vi ≤ 1e9.
1 ≤ N ≤ 20000.
Small dataset
0 ≤ K ≤ 1.
Large dataset
0 ≤ K ≤ 50000.

题解

K=0时,期望就是均值。K=1时,抽完第一次后可以选择是否重抽,当第一次抽出来的数小于第二次抽的期望(也就是K=0时的期望)时选择重抽即可。
也就是E[K] = sum{max(E[k-1], nums[i])} / n。直接递推复杂度为N×K。 可以将数组排序然后二分找第一个大于E[k-1]的数,之前用E[k-1]代替,之后的可
预处理出前缀和直接求出。复杂度O(klgn)

C

题意

给长为一个n的字符串,还有L个字符串,求这L个字符串中有多少个字符串的的变形体在长字符串中。
一个字符串的变形是除了头尾两个字符不可改变之外,其他字符可以任意重排。

数据范围

Limits
1 ≤ T ≤ 20.
No two words in the dictionary are the same.
Each word in the dictionary is between 2 and 105 letters long, inclusive.
The sum of lengths of all words in the dictionary does not exceed 105.
S1 and S2 are lowercase English letters.
0 ≤ A ≤ 1e9.
0 ≤ B ≤ 1e9.
0 ≤ C ≤ 1e9.
1 ≤ D ≤ 1e9.
Small dataset
1 ≤ L ≤ 1000.
2 ≤ N ≤ 1000.
Large dataset
1 ≤ L ≤ 20000.
2 ≤ N ≤ 1e6..

题解

对小数据集,直接暴力统计字符是否在长字符中是否出现即可。可通过统计26个字符的出现次数来判断两个子字符串是否相等。
复杂度O(ln26).

Round C

https://code.google.com/codejam/contest/4384486/dashboard#s=p0

A

题意

给定一个无向图,其中存在着唯一的一个环,求每个点到这个环的最短距离。

数据范围

≤ T ≤ 100.
1 ≤ xi ≤ N, for all i.
1 ≤ yi ≤ N, for all i.
xi ≠ yi, for all i.
(xi, yi) ≠ (xj, yj), for all i ≠ j.
The graph in which planets are nodes and tubes are edges is connected and has exactly one cycle.

small

3 ≤ N ≤ 30.

large

3 ≤ N ≤ 1000.

题解

显然关键在于找出这个环上的点。找出之后即使是大数据集也可以暴力BFS遍历每个点到这个换的最短距离(O(n^2)),或者可以反过来从环上bfs到所有点(O(n))。

比较暴力的找环方法是直接DFS遍历,然后list存储走过的点,当点的所有child dfs完之后将该点删掉。这样遇到重复的点时这之间的所有点就是该环。

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> g[1005];
vector<int> path;
vector<int> circle;
bool visited[1005];
void dfs(int cur, int pa) {
    if (visited[cur] == true) {
        auto it = find(path.begin(), path.end(), cur);
        if (it != path.end()) {
            while (it != path.end()) {
                circle.push_back(*it);
                it++;
            }
        }
        return;
    }
    visited[cur] = true;
    path.push_back(cur);
    for (int &child : g[cur]) {
        if (child != pa) {
            dfs(child, cur);
        }
    }
    path.pop_back();
}
void solve() {
    int n;
    cin >> n;
    int x, y;
    for (int i = 1; i <= n; i++) {
        g[i].clear();
    }
    path.clear();
    circle.clear();
    memset(visited, false, sizeof(visited));
    for (int i = 1; i <= n; i++) {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, -1);
    vector<int> dis(n+1, n+1);
    queue<int> q;
    int temp = circle.size();
    for (int i = 0; i < temp; i++) {
        //cout << circle[i] << "\n";
        dis[circle[i]] = 0;
        q.push(circle[i]);
    }
    int cur = 1;
    while (!q.empty()) {
        int sz = q.size();
        for (int i = 0; i < sz; i++) {
            int top = q.front();
            q.pop();
            for (int &child : g[top]) {
                if (dis[child] > cur) {
                    dis[child] = cur;
                    q.push(child);
                }
            }
        }
        cur++;
    }
    printf("%d",dis[1]);
    for (int i = 2; i <= n; i++) {
        printf(" %d", dis[i]);
    }
    printf("\n");
}
int main() {
    freopen("A-large.in", "r", stdin);
    freopen("A-large.out", "w", stdout);
    int t;
    cin >> t;
    for (int tt = 1; tt <= t; tt++) {
        printf("Case #%d: ", tt);
        solve();
    }
    return 0;
}

B

题意

求从一个无向图中拆出的边能组成的凸边形个数。其中拆出的便不能有相同的端点。

数据范围

0 ≤ Li, j ≤ 1000 for all i, j.
Li, i = 0, for all i.
Li, j = Lj, i, for all i, j.

small

N = 6

large

6<=N<=15

题解

对小数据集,显然最多只能拆出三条边组成三角形,暴力即可。

大数据集不会,待补。

C

题意

给一个数组,对数组中的所有连续子数组按以下公式求k次和,然后求和最后所有的结果(modulo 1000000007)。
对连续子数组Aj, Aj+1, ..., Ak,第i次求和公式为 A[j] × 1^i + A[j+1] × 2^i + A[j+2] × 3i + ... + A[k] × (k-j+1)^i.

样例:

In Sample Case #1, the Parameter Array is [3, 2]. All the contiguous subarrays are [3], [2], [3, 2].

For i = 1:

1-st Exponential-power of [3] = 3 × 1^1 = 3
1-st Exponential-power of [2] = 2 × 1^1 = 2
1-st Exponential-power of [3, 2] = 3 + 2 × 2^1 = 7
So POWER1 is 12.
For i = 2:

2-nd Exponential-power of [3] = 3 × 1^2 = 3
2-nd Exponential-power of [2] = 2 × 1^2 = 2
2-nd Exponential-power of [3, 2] = 3 + 2 × 2^2 = 11
So POWER2 is 16.
For i = 3:

3-rd Exponential-power of [3] = 3 × 1^3 = 3
3-rd Exponential-power of [2] = 2 × 1^3 = 2
3-rd Exponential-power of [3, 2] = 3 + 2 × 2^3 = 19
So POWER3 is 24.

So answer is 24 + 12 + 16 = 53.

数据范围

1 ≤ T ≤ 100.
1 ≤ x1 ≤ 1e5.
1 ≤ y1 ≤ 1e5
1 ≤ C ≤ 1e5.
1 ≤ D ≤ 1e5.
1 ≤ E1 ≤ 1e5.
1 ≤ E2 ≤ 1e5.
1 ≤ F ≤ 1e5.

small

1 ≤ N ≤ 100.
1 ≤ K ≤ 20.

large

1 ≤ N ≤ 1e6.
1 ≤ K ≤ 1e4

题解

首先根据xycdef生成数组A。
对于小数据集暴力即可。O(k*n^3)
对于大数据集,可以考虑每个数对最后结果的贡献,大力推推就可以出来了。

考虑第i次计算,则数组A中第j位计算的值为:A[j] * (N+1-j) * {1^i + 2^i + .... + j^i} ( j从1开始) 。直接枚举的话复杂度O(k * n^2lgk) ,当然第j^i求和可以累加,能降到O(knlgk) 。
另一种思路是调换一下求和顺序,则ans=sum_{i=1..j} {A[j] * (N+1-j) * sum_{i=1..k}{1^i + 2^i + .... + ji}}。其中mi求和可以用等比数列公式,再通过累加可以降到O(k
lgk)。(lgk为求幂时的复杂度)

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
ll pw(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b&1) ans = ans * a % mod;
        b /= 2;
        a = a * a % mod;
        //cout << ans << " " << a << "\n";
    }
    return ans;
}
ll calc(ll a, ll b) {
    //cout << pw(a, b) << "\n";
    ll ans = a * ((pw(a, b) - 1 + mod) % mod) % mod * pw(a-1, mod-2) % mod;
    //cout << ans <<"\n";
}
ll solve() {
    ll n, k;
    cin >> n >>k;
    vector<ll> nums(n+1);
    ll x, y;
    cin >> x >> y;
    ll c,d,e1,e2,f;
    cin >> c >> d >> e1 >> e2 >> f;
    nums[1] = (x+y) % f;
    for (int i = 2; i <= n; i++) {
        ll tx = x, ty = y;
        x = (c*tx+d*ty+e1) %f;
        y = (d*tx+c*ty+e2) %f;
        nums[i] = (x+y) % f;
    }
    ll ans = nums[1]*n%mod*k%mod;
    ll sum = k;
    //cout << ans <<"\n";
    for (int i = 2; i <= n; i++) {
        sum = (sum + calc(i, k)) % mod;
        //cout << sum << "\n";
        ans = (ans + nums[i] * (n+1-i) % mod * sum % mod) % mod;
        //cout << i << " " << ans << "\n";
    }
    return ans;
}
int main() {
    freopen("C-large-practice.in", "r", stdin);
    freopen("C-large.out", "w", stdout);
    int t;
    cin >> t;
    for (int tt = 1; tt <= t; tt++) {
        std::cerr << tt << "\n";
        printf("Case #%d: %lld\n", tt, solve());
    }
    return 0;
}
posted @ 2018-06-14 01:49  hxidkd  阅读(1882)  评论(0编辑  收藏  举报