Codeforces Global Round 1 A. Parity(数学思维)

You are given an integer n (n≥0) represented with k digits in base (radix) b. So,

n=a1⋅bk−1+a2⋅bk−2+…ak−1⋅b+ak.
For example, if b=17,k=3 and a=[11,15,7] then n=11⋅172+15⋅17+7=3179+255+7=3441.

Determine whether n is even or odd.

Input
The first line contains two integers b and k (2≤b≤100, 1≤k≤105) — the base of the number and the number of digits.

The second line contains k integers a1,a2,…,ak (0≤ai<b) — the digits of n.

The representation of n contains no unnecessary leading zero. That is, a1 can be equal to 0 only if k=1.

Output
Print “even” if n 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

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cctype>
using namespace std;

#define PI acos(-1.0)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;	
const int INF32M=0x3f3f3f3f;
const ll INF64M=0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+5;
int a[100005];	//k个系数 
int main()
{
	ios::sync_with_stdio(false);
	int b,k,cnt=0;
	cin>>b>>k;
	for(int i=1;i<=k;i++)
		cin>>a[i];
	if(b%2==0)
	{
		if(a[k]%2==0)	//ak为偶数 
			cout<<"even"<<endl;
		else	//偶+奇=奇 
			cout<<"odd"<<endl;
	}
	else	//底数b为奇数 
	{
		for(int i=1;i<=k;i++)
		{
			if(a[i]%2!=0)	//奇*奇=奇 
				cnt++;
			//else 奇(b^k)*偶(系数)=偶 
		}
		if(cnt%2==0)	//奇系数有偶数个 奇数项就有偶数个 3+7=10
			cout<<"even"<<endl;
		else	//奇+奇=偶 奇+奇+奇=奇 
			cout<<"odd"<<endl;
	} 
	return 0;
}

观察多项式
b为偶数 b^k为偶数 系数无论奇数偶数
奇乘偶=偶 偶乘偶=偶
只需要考虑ak奇偶性 偶+奇=奇 偶+偶=偶
b为奇数 b^k为奇数 偶系数不管 得偶
cnt判断奇系数有几个 b^k乘奇系数=奇
奇+奇=偶 偶+奇=奇
奇系数有几个意味着奇数项有几个
奇数项有偶数个 加法为偶数
奇数项有奇数个 加法为奇数 偶(偶系数*b^k=偶)+奇=奇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值