zoukankan      html  css  js  c++  java
  • ACM学习历程—BestCoder Round #75

    1001:King's Cake(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5640

    这题有点辗转相除的意思。基本没有什么坑点。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <queue>
    #include <vector>
    #include <string>
    #define LL long long
    
    using namespace std;
    
    int n, m;
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        int T;
        scanf("%d", &T);
        for (int times = 1; times <= T; ++times)
        {
            scanf("%d%d", &n, &m);
            int ans = 0;
            while (n&&m)
            {
                if (n > m) swap(n, m);
                ans += m/n;
                m = m%n;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    View Code

    1002:King's Phone(模拟)

    http://acm.hdu.edu.cn/showproblem.php?pid=5641

    这题坑点有点多,3A。。首先任意一条连线,需要判断中间有没有经过什么点。

    然后不能有重复的点,需要判断。

    其次,对于0和大于9的数据需要判断掉。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <queue>
    #include <vector>
    #include <string>
    #define LL long long
    
    using namespace std;
    
    int k, s[10];
    bool vis[10];
    
    int cross(int from, int to)
    {
        if (from > to) swap(from, to);
        if (from == 1 && to == 3) return 2;
        if (from == 1 && to == 7) return 4;
        if (from == 1 && to == 9) return 5;
        if (from == 2 && to == 8) return 5;
        if (from == 3 && to == 7) return 5;
        if (from == 4 && to == 6) return 5;
        if (from == 3 && to == 9) return 6;
        if (from == 7 && to == 9) return 8;
        return 0;
    }
    
    bool work()
    {
        if (k < 4) return false;
        memset(vis, false, sizeof(vis));
        int t;
        for (int i = 1; i < k; ++i)
        {
            if (s[i-1] == 0 || s[i] == 0) return false;
            if (s[i-1] > 9 || s[i] > 9) return false;
            if (s[i] == s[i-1]) return false;
            if (vis[s[i]]) return false;
            t = cross(s[i-1], s[i]);
            if (t && !vis[t]) return false;
            vis[s[i-1]] = true;
            vis[s[i]] = true;
        }
        return true;
    }
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        int T;
        scanf("%d", &T);
        for (int times = 1; times <= T; ++times)
        {
            scanf("%d", &k);
            for (int i = 0; i < k; ++i)
                scanf("%d", &s[i]);
            if (work()) printf("valid
    ");
            else printf("invalid
    ");
        }
        return 0;
    }
    View Code

    1003:King's Order(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5642

    这题是个递推,但是没考虑清楚,递推式一直是错的,4A。。

    p[n][k]表示n长度字符串,结尾k个相同的情况数。

    那么:

    p[i][1] = 25*(p[i-1][1]+p[i-1][2]+p[i-1][3])%MOD;

    p[i][2] = p[i-1][1];

    p[i][3] = p[i-1][2];

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <queue>
    #include <vector>
    #include <string>
    #define LL long long
    #define MOD 1000000007
    
    using namespace std;
    
    const int maxN = 2005;
    int n;
    LL p[maxN][5];
    
    void init()
    {
        p[1][1] = 26;
        p[1][2] = 0;
        p[1][3] = 0;
        for (int i = 2; i < maxN; ++i)
        {
            p[i][1] = 25*(p[i-1][1]+p[i-1][2]+p[i-1][3])%MOD;
            p[i][2] = p[i-1][1];
            p[i][3] = p[i-1][2];
        }
    }
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        init();
        int T;
        scanf("%d", &T);
        for (int times = 1; times <= T; ++times)
        {
            scanf("%d", &n);
            cout << (p[n][1]+p[n][2]+p[n][3])%MOD << endl;
        }
        return 0;
    }
    View Code

    1004:King's Game(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5643

    这一题想法类似于O(n)做法的约瑟夫问题。需要考虑子问题,然后映射回来。具体的方法百度百科里有。

    最后

    p(n, k) = (p(n-1, k+1)+k)%n  or  n(if 0)

    不过我二逼的离线了一发,然后MLE了。。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <queue>
    #include <vector>
    #include <string>
    #define LL long long
    
    using namespace std;
    
    const int maxN = 5001;
    /*
    int p[maxN][maxN];
    
    void init()
    {
        for (int i = 0; i < maxN; ++i)
            p[1][i] = 1;
        for (int i = 2; i < maxN; ++i)
        {
            for (int j = 1; j < maxN; ++j)
            {
                p[i][j] = (p[i-1][j+1]+j)%i;
                if (!p[i][j]) p[i][j] = i;
            }
        }
    }
    */
    
    int dfs(int n, int k)
    {
        if (n == 1) return 1;
        int ans;
        ans = (dfs(n-1, k+1)+k)%n;
        if (!ans) ans = n;
        return ans;
    }
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        //init();
        int n, T;
        scanf("%d", &T);
        for (int times = 1; times <= T; ++times)
        {
            scanf("%d", &n);
            printf("%d
    ",dfs(n, 1));
        }
        return 0;
    }
    View Code

    1005:没有想法。。应该是水平还没到火候。。。

  • 相关阅读:
    AtCoder Beginner Contest 184 ABCDE 题解
    Codeforces Round #685 (Div. 2) C. String Equality 思维
    Codeforces Round #682 (Div. 2) ABC 题解
    Gym 102215 & 队内训练#5
    【题解】 CF436E Cardboard Box 带悔贪心
    【题解】 「NOI2019」序列 模拟费用流 LOJ3158
    【笔记】 exlucas
    【题解】 「HNOI2018」毒瘤 树形dp+动态dp+容斥+格雷码 LOJ2496
    【笔记】 异或高斯消元方案数
    【笔记】 用生成函数推二项式反演
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/5270278.html
Copyright © 2011-2022 走看看