Codeforces Global Round 1

本文介绍了一种在给定基数b和位数k的情况下,通过分析数的各位数字来快速判断该数是否为偶数的方法。文章提供了一个C++实现的示例,通过读取基数b和位数k,以及各个位上的数字,然后根据基数的奇偶性和最高位数字的奇偶性来确定整个数的奇偶性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 A. Parity

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an integer nn (n≥0n≥0) represented with kk digits in base (radix) bb. So,
n=a1⋅bk−1+a2⋅bk−2+…ak−1⋅b+ak.
n=a1⋅bk−1+a2⋅bk−2+…ak−1⋅b+ak.
For example, if b=17,k=3b=17,k=3 and a=[11,15,7]a=[11,15,7] then n=11⋅172+15⋅17+7=3179+255+7=3441n=11⋅172+15⋅17+7=3179+255+7=3441.
Determine whether nn is even or odd.

Input
The first line contains two integers bb and kk (2≤b≤1002≤b≤100, 1≤k≤1051≤k≤105) — the base of the number and the number of digits.
The second line contains kk integers a1,a2,…,aka1,a2,…,ak (0≤ai<b0≤ai<b) — the digits of nn.
The representation of nn contains no unnecessary leading zero. That is, a1a1 can be equal to 00 only if k=1k=1.

Output
Print “even” if nn is even, otherwise print “odd”.
You can print each letter in any case (upper or lower).

Examples
input
13 3
3 2 7
output
even
input
10 9
1 2 3 4 5 6 7 8 9
output
odd
input
99 5
32 92 85 74 4
output
odd
input
2 2
1 0
output
even

Note
In the first example, n=3⋅132+2⋅13+7=540n=3⋅132+2⋅13+7=540, which is even.
In the second example, n=123456789n=123456789 is odd.
In the third example, n=32⋅994+92⋅993+85⋅992+74⋅99+4=3164015155n=32⋅994+92⋅993+85⋅992+74⋅99+4=3164015155 is odd.
In the fourth example n=2n=2.

分析
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
const int MA=100005;
int a[MA];
int b,k;
int main()
{
    cin>>b>>k;
    int sum=0;
    for(int i=1;i<=k;++i){
        cin>>a[i];
        if(a[i]%2==1) sum++;
    }
    if(b%2==1){
      if(sum%2==1) cout<<"odd"<<endl;
      else cout<<"even"<<endl;
    }
    else {
      if(a[k]%2==1)cout<<"odd"<<endl;
      else cout<<"even"<<endl;
    }
    return 0;
}

2. B - Tape

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MA=100005;
int a[MA],b[MA];
int n,m,k,sum=0;
int main()
{
    cin>>n>>m>>k;
    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    if(n<=k){
        cout<<n<<endl;
    }
    else{
       for(int i=2;i<=n;++i){
        b[i]=a[i]-a[i-1]+1;
       }
       sort(b+2,b+n+1);
       for(int i=0;i<n-k;++i)
        sum+=b[2+i];
       cout<<sum+2*k-n<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值