2018第二届河北省大学生程序设计竞赛题解

icebound的账单

 

题目描述

icebound从小就有记账的习惯。又到了月末icebound统计资金状况的时候。icebound每个月除了不停的挥霍以外,有时他会良心发现,勤工俭学,因此会有一些微薄的收入。然而icebound数学不好,需要你来帮助他统计他本月的资金状况。

你将会得到一份icebound的账单,一共有 n​ 行整数,其中正数表示icebound打工挣来的收入,负数表示icebound消费的支出。数据保证不会出现 0​ 。

如果icebound本月总收入大于总支出,请你输出“icebound is happy.”;如果本月总收入等于总支出,请你输出“icebound is ok.";如果总收入小于总支出,请你输出"icebound is sad."。

输入描述

第一行,有一个正整数nn,代表账单有nn行。

接下来有nn行,每行一个整数,第i+1i+1行整数a_iai​。

1 \leq n \leq 10001≤n≤1000,\left| a_i \right|\leq 1000∣ai​∣≤1000, a_i \neq 0ai​≠0

输出描述

输出一行。如果icebound本月总收入大于总支出,请你输出“icebound is happy.”;如果本月总收入等于总支出,请你输出“icebound is ok.";如果总收入小于总支出,请你输出"icebound is sad."。

样例输入

2
100
-100

样例输出

icebound is ok.

思路:简单分类不解释

#include<bits/stdc++.h>
using namespace std;
long long sum = 0;

int main()
{
    int n,k;
    cin >> n;
    while(n--)
    {
        scanf("%d", &k);
        sum += k;
    }
    if(sum > 0)
        cout << "icebound is happy." << endl;
    else if (sum < 0)
        cout << "icebound is sad." << endl;
    else
        cout << "icebound is ok." << endl;
    return 0;
}


520

题目描述

“又到了五月了呢”,icebound望着五月的天空,眼角流出了泪痕。那一年,icebound还是一个懵懂的少年。那一年,她还是一个青涩纯真的少女。在那一次偶然的相遇之中,他们之间擦出了爱情的火花。他们欢笑着,奔跑着,他们展望着美好的未来,向往着幸福的明天。她像 icebound 心海中的灯塔,像icebound 头顶上的星辰,即使在海里浮沉,即使在夜里摸爬,心中也不会感到迷茫,感到阴寒。他们努力,奋进,向着六月的那一站前行。可是,美好总是短暂的。那海上的灯塔不再发出温情的光亮,那天空中的星辰不再绽放出温柔的色彩。那一站,到达了,icebound 得到了终点,但icebound 永远失去了她,也失去了他的心。

”侯门一入深似海,从此萧郎是路人“

今天是2018年5月20日,又是一年的520。这一天,icebound不小心读到上面的诗,icebound沉思着,回想起与她曾经的快乐时光,icebound留下了nn滴眼泪。icebound的每滴眼泪都带有太多的伤感之情了,以至于每滴眼泪都会感染到其他的生物,使得许多生物都一起掉下了眼泪。kk通过观察得知,当icebound流出nn滴眼泪时,所有生物产生的眼泪总数为2^n。现在,kk需要你帮助他写一个程序,计算当icebound流出nn滴眼泪时,所有生物产生的眼泪总数PP,对 2018052020180520 取模。

输入描述

一个正整数nn,代表icebound留下眼泪的个数。1 \leq n \leq 2 \times 10^91≤n≤2×109

输出描述

一个正整数PP,代表所有生物产生的眼泪总数,对 2018052020180520 取模。

样例输入

1

样例输出

2

思路:n太大,写一个快速幂

#include <bits/stdc++.h>
using namespace std;
long long int mi(long long int a,long long int b,long long int m)
{
    long long int ans=1;
    a=a%m;
    while(b!=0)
    {
        if(b&1)ans=ans*a%m;
        b >>= 1;
        a=a*a%m;
    }
    return ans%m;
}
int main()
{
    int n;
    cin>>n;
    int sum = mi(2,n,20180520)%20180520;
    cout<<sum;
    return 0;
}


神殿

题目描述

icebound通过勤工俭学,攒了一小笔钱,于是他决定出国旅游。这天,icebound走进了一个神秘的神殿。神殿由八位守护者守卫,总共由6464个门组成,每一道门后都有一个迷宫,迷宫的大小均为100 \times 100100×100。icebound在迷宫中总共耗时TT小时,消耗食物KK公斤。历经千辛万苦之后,icebound终于穿越了迷宫,到达了神殿的中心。神殿的中心有一个宝箱。宝箱上显示有两个正整数ll和rr。icebound苦思冥想,终于发现一些打开宝箱的线索。你需要找到一个数PP,它具有一个美妙的性质:它是[l,r][l,r]中所有数的二进制表示里,11的个数最多的一个数。如果你发现了这个美妙的数字,你就可以打开宝箱,获得巨额财富。

比如[4,8][4,8]中:

4: 0100
5: 0101
6: 0110
7: 0111
8: 1000

二进制表示中11的个数最多的数是77,它含有33个11。

输入描述

输入一行,两个正整数:ll和rr,用空格隔开,代表神殿中宝箱上显示的数。

1 \leq T < 2^{31}1≤T<231,

1 \leq K \leq 10^51≤K≤105,

1 \leq l \leq r \leq 2 \times 10^{9}1≤l≤r≤2×109

输出描述

一个十进制数P,代表满足条件的解。如果有多个P满足条件,输出最小的P。

样例输入

4 8

样例输出

7

思路:

求区间内二进数中1最多的那个数是多少。

求不超过r的位或和。

比如:

0 1 1

1 0 0

----------

1 1 1

有一个1就为1

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long l,r,i;
    scanf("%lld %lld",&l,&r);
    for(i=l;i<=r;i++)if((i|i+1)>r)break;
    printf("%lld",i);
}


Mex Query

题目描述

Give nn non-negative integers, please find the least non-negative integer that doesn’t occur in the nn numbers.

输入描述

The first line is an integer TT, representing the number of test cases.

For each test case:

The first line of each test case is an integer nn.

The second line of each test case are nn non-negative integers a_iai​.

T \leq 10T≤10

n \leq 2 \times 10^5n≤2×105

0 \leq a_i < 2^{31}0≤ai​<231

输出描述

or each test case, output a line with the answer.

样例输入

2
4
4 0 1 3
2
1 1

样例输出

2
0

思路:找出序列里第一个没有出现过的数,排序,然后本数减前一个数大于一,就说明相差大于一,就找到了那个确实的数了。

#include<bits/stdc++.h>
using namespace std;
int arr[200005];
int main(){
    int t,k;
    cin >> t;

    while(t--){
        int n;
        scanf("%d", &n);

        for(int i = 1; i <= n; ++i)
            scanf("%d", &arr[i]);

        sort(arr+1, arr+n+1);
        arr[0] = -1;

        for(int i = 1; i < n; ++i){
            if(arr[i] - arr[i-1] > 1){
                printf("%d\n", arr[i-1]+1);
                break;
            }
        }
    }
    return 0;
}


icebound的商店

题目描述

icebound在得到神殿的宝藏之后,开了一家神秘的商店。你来到了商店,发现慷慨的icebound搞了TT次促销活动。在每次促销活动中,icebound都会想出一个他喜欢的数字,如果你买的商品的总价刚好等于icebound喜欢的数字,那么你就可以免费得到这些商品。

icebound的商店里一共有 1515 件商品,商品的价格和这家商店一样神秘,第一件商品的价格是 11 元,第二件商品的价格是 22元,设第nn件商品的价格为w_nwn​元,则:w_n = w_{n-1} + w_{n-2} (3 \leq n \leq 15)wn​=wn−1​+wn−2​(3≤n≤15)。

如果在某次活动中icebound喜欢的数字是 44,那么共有 44 种购买方案:

1. 买 4个 第一种商品
2. 买 2个 第一种商品 和 1个 第二种商品
3. 买 2个 第二种商品
4. 买 1个 第一种商品 和 1个 第三种商品

请你算出共有多少种方案可以免费购物,方案数对10000000091000000009(=10^9+9=109+9)取模。

输入描述

第一行给出一个整数TT,表示icebound搞了TT次活动。

接下来的TT行每行给出一个正整数xx,表示在这次活动中icebound喜欢的数字。

1 \leq T \leq 30001≤T≤3000

1 \leq x \leq 30001≤x≤3000

输出描述

输出TT行,每行输出一个正整数。

第ii行的正整数表示在第ii次活动中免费购物的方案数,方案数对10000000091000000009(=10^9+9=109+9)取模。

样例输入

3
5
20
30

样例输出

6
134
509

思路:动态规划,背包求方法数的变形,物品为斐波那契数列前几项,先求出来。然后转移方程为dp[j]=(dp[j]+dp[j-fib[i]])%MOD

(总结的技巧,一般把式子的max变为sum即可。)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e6;
const int MOD = 1e9 + 9;
int fib[50];
int dp[3005];
inline void init() //前15项商品
{
    fib[1] = 1, fib[2] = 2;
    for(int i  = 3; i <= 15; ++i) fib[i] = fib[i - 1] + fib[i - 2];
}
int main()
{
    init();
    int T, x;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &x);
        memset(dp, 0, sizeof dp);
        dp[0] = 1;
        for(int i = 1; i <= 15; ++i)
            for(int j = fib[i]; j <= x; ++j)
                dp[j] = (dp[j] + dp[j - fib[i]]) % MOD;//情况求和
        printf("%d\n", dp[x] % MOD);
    }
    return 0;
}


跑图

题目描述

跑图是RPG游戏中很烦躁的事情。玩家需要跑到距离他最近的传送点的位置。现在给你一张N \times MN×M的方格图,每个方格中数值00表示为平地,数值11表示为传送点,你的任务是输出一张N \times MN×M的矩阵,Matrix_{xy}Matrixxy​表示从(x,y)(x,y)到距离它最近的传送点的距离。 这里的距离是曼哈顿距离,(x_1,y_1) \rightarrow(x_2,y_2)(x1​,y1​)→(x2​,y2​) 的距离为|x_1-x_2|+|y_1-y_2|∣x1​−x2​∣+∣y1​−y2​∣。

输入描述

第一行,有两个数n,mn,m。接下来nn行,每行mm个数。

数据保证至少有一个传送点。

1 \leq n \leq 5001≤n≤500,1 \leq m \leq 5001≤m≤500

输出描述

nn行,每行mm个数,表示某个点到离它最近的传送点的距离。

样例输入

2 3
0 0 0
1 0 1

样例输出

1 2 1
0 1 0

思路:直接把传送点的位置放进一个队列里然后跑BFS即可,每个点只需要被访问一次,到传送点的的距离也都是最近的

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

const int inf = 0x3f3f3f3f;
const int maxn = 500 + 5;
int arr[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int dis[maxn][maxn];
int n, m;
struct Node{
    int x, y, dis;
};

bool check(int x, int y){
    if(x >= 0 && x < n && y >= 0 && y < m){
        return true;
    }

    return false;
}

int main(){
    int k;
    cin >> n >> m;

    memset(dis, inf, sizeof dis);
    queue<Node> q;

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            scanf("%d", &k);
            arr[i][j] = k;
            if(k == 1){
                Node node = {i, j, 0};
                q.push(node);
                dis[i][j] = 0;
            }
        }
    }


    while(q.size()){
        Node tmp = q.front();
        q.pop();
        int tx = tmp.x;
        int ty = tmp.y;
        int ts = tmp.dis;

        for(int i = 0; i < 4; ++i){
            if(check(tx+dir[i][0], ty+dir[i][1])){
                int dx = tx + dir[i][0];
                int dy = ty + dir[i][1];

                if(dis[dx][dy] > ts + 1){
                    dis[dx][dy] = ts + 1;

                    Node t = {dx, dy, ts+1};
                    q.push(t);
                }
            }
        }
    }

    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m-1; ++j){
            printf("%d ", dis[i][j]);
        }
        printf("%d\n", dis[i][m-1]);
    }

    return 0;
}


K Multiple Longest Commom Subsequence

题目描述

KK has two sequences, AA and BB, and wants to find the kk multiple longest common subsequence.A sequence SS is a kkmultiple common subsequence of AA and BB if and only if it satisfies the following conditions:

  1. SS is a subsequence of AA and is a subsequence of BB. (A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.)

  2. The length of SS is t\times kt×k where tt is a nonnegative integer. The first element of SS is S[1]S[1]. If we divide the sequence into tt groups with the iith group containing S[(i - 1) \times k + j] (1 \leq j \leq k)S[(i−1)×k+j](1≤j≤k), for every element gg, it shares the same value with other elements that are in the same group which gg belongs to.

For example, [1, 1, 2, 2][1,1,2,2] is a double common subsequence of [1, 2, 3, 1, 2, 3, 2][1,2,3,1,2,3,2] and [1, 3, 1, 2, 2][1,3,1,2,2]. KK wants to know the maximum length of such sequence.

输入描述

The first line is an integer TT, denoting the number of test cases.

For each test case, the first line are three integers kk , nn, mm, denoting the kind of subsequence, the length of AA and the length of BB.

The second line are nn integers A_1 \sim A_nA1​∼An​, representing the elements of AA.

The third line are mm integers B_1 \sim B_mB1​∼Bm​, representing the elements of BB.

1 \leq T \leq 101≤T≤10 , 1\leq k, n, m \leq 10^31≤k,n,m≤103, 1\leq A_i, B_i \leq 10^31≤Ai​,Bi​≤103.

输出描述

For each test case, output a line with the maximum length of kk multiple common subsequence.

样例输入

3
1 4 5
1 2 3 4
4 1 3 2 4
2 8 7
1 1 2 2 3 3 4 4
1 2 3 1 2 3 3
3 9 9
1 1 1 2 2 2 3 3 3
1 2 3 1 2 3 1 2 3

样例输出

3
4
3

题意:最长公共子序列,要求序列每个元素重复k次,比如1122重复两次,111222重复三次。

输入两个字符串和k,输出长度。

最长公共子序列的一个变形。。。

动态规划。设dp[i][j]表示a串处理到i位置,b串处理到j位置的答案。预处理出从a串i位置向前数,第k个与i位置数
字相同的位置p[i],用相同方式处理出B串的q[i]。
则转移方程为dp[i][j]=max(dp[i-1][j],dp[i][j-1],dp[i-p[i]][j-q[j]]+k),其中第三种转移必须在a[i]=b[j]而且p,q都存在的情况下转移。
 

#include <bits/stdc++.h>
using namespace std;
int k,n,m,dp[1005][1005];
int a[1005],b[1005];
int pa[1005]={0},pb[1005]={0};
queue<int> q[1005];
int main()
{
    int ak; cin>>ak;
    while(ak--)
    {
        for(int i=1;i<=n;i++)
            while(!q[i].empty()) q[i].pop();
        scanf("%d%d%d",&k,&n,&m);
        memset(dp,0,sizeof(dp));
        memset(pa,0,sizeof(pa));
        memset(pb,0,sizeof(pb));
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<=m;i++) cin>>b[i];
        for(int i=1;i<=n;i++)
        {
            q[a[i]].push(i);
            if(q[a[i]].size()==k)
            {
                pa[i]=q[a[i]].front();
                q[a[i]].pop();
            }
        }
        for(int i=1;i<=n;i++)
            while(!q[i].empty()) q[i].pop();
            
        for(int i=1;i<=m;i++)
        {
            q[b[i]].push(i);
            if(q[b[i]].size()==k)
            {
                pb[i]=q[b[i]].front();
                q[b[i]].pop();
            }
        }
        
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                if(a[i]==b[j] && pa[i]!=0 && pb[j]!=0)
                    dp[i][j]=dp[pa[i]-1][pb[j]-1]+k;
            }
        }
        printf("%d\n",dp[n][m]);
    }
    return 0;
}


Nim Game

总感觉复制到这里符号就不对了,原题网址:http://newoj.acmclub.cn/problems/2013

题目描述

Description

Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap [From Wikipedia, the free encyclopedia]The goal of the game is to avoid being the player who doesn’t have any object to remove. The player who remove the last project is the winner.

Now KK and TT are playing Nim game with the optimal strategy. There are nn heaps of stones. The number of stones in ii -th heap is a_iai​. They play this game mm times, and KK is the player making the first move. During the ii -th time they play the game on the heaps whose index in interval [l_i,r_i][li​,ri​]. KK wants to know whether he has a winning strategy or not.

输入描述

Input

The input consists of several test cases. The first line of the input gives the number of test cases, T(1\le T\le 10^3)T(1≤T≤103).

For test case, the first line contains two integers n(1\le n\le 10^6)n(1≤n≤106) and m(1\le m\le 10^6)m(1≤m≤106), representing the number of heap of stones and the game times.

The second line contains nn positive integers a_1,a_2,\cdots,a_n(1\le a_i\le 10^9)a1​,a2​,⋯,an​(1≤ai​≤109), representing The number of stones in ii-th heap.

In the next mm lines, each line contains two integers l_i,r_ili​,ri​, which means the $i: KaTeX parse error: $ within math mode$th game is played on the interval [l_i,r_i][li​,ri​].

It’s guaranteed that \sum n\le 2\times 10^6∑n≤2×106 and \sum m\le 2\times 10^6∑m≤2×106.

输出描述

Output

For each test case, let f_ifi​ represents the answer of the ii th game.

If KK has a winning strategy in the ii th game then f_i=1fi​=1, otherwise f_i=0fi​=0. Please output \sum f_i*2^{m-i}\ mod\ 10^9+7∑fi​∗2m−i mod 109+7,in which 1\le i\le m1≤i≤m.

样例输入

3
2 1
1 1
1 2
2 1
1 2
1 2
3 2
1 2 2
1 2
2 3

样例输出

0
1
2

思路:
和普通Nim游戏不同在于每一次会给出对应的堆区间

比如现在有5堆,每一堆个数分别为1 2 3 4 5,选择1 3,代表选择1 2 3这三堆为本轮的堆数
测试这区间的堆数的异或值是否为0,如果为0,必败,否则必胜
需要记录一个前缀异或和数组优化时间

每次给出[l,r],只需要pre[r]^pre[l-1]即可

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e6;
const int MOD = 1e9 + 7;
int arr[N + 5];
ll pre[N + 5];
ll powmod(ll a, ll b)
{
    ll res=1;
    a %= MOD;
    while(b)
    {
        if(b & 1) res = res * a % MOD;
        a=a * a % MOD;
        b >>= 1;
    }
    return res;
}
int main(void)
{
    int T, n, m, l, r;
    scanf("%d", &T);
    while(T--) {
        pre[0] = 0;
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; ++i) {
            scanf("%d", arr + i);
            pre[i] = pre[i - 1] ^ arr[i]; //记录异或前缀和
        }
        ll sum  = 0;
        for(int i = 1; i <= m; ++i) {
            scanf("%d %d", &l, &r);
            if(pre[r] ^ pre[l - 1]) {//一个数如果异或两次等于没有异或过,比如1^2^3^2=1^3
                sum += powmod(2, m - i);
                sum %= MOD; //记得取模
            }
        }
        printf("%lld\n", sum % MOD);
    }
    return 0;
}

注:这个题是看yzx大佬思路的。



剩下的暂时不会做啦。。。。

 

评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兔老大RabbitMQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值