CF1814A

发布时间 2023-10-13 21:30:01作者: 悲伤鳄鱼吃面包

Coins

题面翻译

本题一共有 \(t\) 组数据。

每组数据包含两个整数 \(n\)\(k\),如果存在两个非负整数 \(x,y\),满足 \(2\times x+k\times y=n\),输出 YES,否则输出 NO

题目描述

In Berland, there are two types of coins, having denominations of $ 2 $ and $ k $ burles.

Your task is to determine whether it is possible to represent $ n $ burles in coins, i. e. whether there exist non-negative integers $ x $ and $ y $ such that $ 2 \cdot x + k \cdot y = n $ .

输入格式

The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases.

The only line of each test case contains two integers $ n $ and $ k $ ( $ 1 \le k \le n \le 10^{18} $ ; $ k \ne 2 $ ).

输出格式

For each test case, print YES if it is possible to represent $ n $ burles in coins; otherwise, print NO. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

样例 #1

样例输入 #1

4
5 3
6 1
7 4
8 8

样例输出 #1

YES
YES
NO
YES

提示

In the first test case, you can take one coin with denomination $ 2 $ and one coin with denomination $ k = 3 $ .

In the second test case, you can take three coins with denomination $ 2 $ . Alternatively, you can take six coins with denomination $ k = 1 $ .

In the third test case, there is no way to represent $ 7 $ burles.

In the fourth test case, you can take one coin with denomination $ k = 8 $ .

分析

问我们能否用面值为2的硬币和面值为k的硬币表示 n , 即是问是否存在非负整数 x 和 y 使得 \(2\times x + k \times y = n\) , 如果存在, 那么其中y个面值为硬币肯定可以拆出 x' 个面值为2的硬币, 余数为 y' , 我们只需要从0到1枚举 y' , 看看 \(n - y'\times k\) 是否是个偶数, 即是否可以被二整除即可

代码

#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ULL;
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	
	int t; cin >> t;
	while (t--)
	{
		ULL n, k; cin >> n >> k;
		for (int i = 0; i <= 2; i++)
		{
			if (i == 2)
			{
				cout << "no" << endl;
				break;
			}
			ULL v = n - i * k;
			if (v % 2 == 0)
			{
				cout << "yes" << endl;
				break;
			}
		}
	}
}