C. The Legend of Freya the Frog

time limit per test

2 seconds

memory limit per test

256 megabytes

Freya the Frog is traveling on the 2D coordinate plane. She is currently at point (0,0)(0,0) and wants to go to point (x,y)(x,y). In one move, she chooses an integer dd such that 0≤d≤k0≤d≤k and jumps dd spots forward in the direction she is facing.

Initially, she is facing the positive xx direction. After every move, she will alternate between facing the positive xx direction and the positive yy direction (i.e., she will face the positive yy direction on her second move, the positive xx direction on her third move, and so on).

What is the minimum amount of moves she must perform to land on point (x,y)(x,y)?

Input

The first line contains an integer tt (1≤t≤1041≤t≤104) --- the number of test cases.

Each test case contains three integers xx, yy, and kk (0≤x,y≤109,1≤k≤1090≤x,y≤109,1≤k≤109).

Output

For each test case, output the number of jumps Freya needs to make on a new line.

Example

Input

Copy

复制代码

3

9 11 3

0 10 8

1000000 100000 10

Output

Copy

复制代码
8
4
199999

Note

In the first sample, one optimal set of moves is if Freya jumps in the following way: (0,00,0) →→ (2,02,0) →→ (2,22,2) →→ (3,23,2) →→ (3,53,5) →→ (6,56,5) →→ (6,86,8) →→ (9,89,8) →→ (9,119,11). This takes 8 jumps.

解题说明:此题是一道数学题,交替向上向右走,可以不走,请问到给定点需要走几次。由于可以走0步,所以向上走和向右走是相互独立的,只需要求出他们的最大值即可。注意走0步的移动也要统计在内。

cpp 复制代码
#include <iostream>
#include<algorithm>
using namespace std;

void solve()
{
	long long x, y, k;
	cin >> x >> y >> k;
	long long ansx = x / k + (x % k != 0), ansy = y / k + (y % k != 0);
	if (ansx > ansy)
	{
		cout << 2ll * ansx - 1 << endl;
	}
	else
	{
		cout << 2ll * ansy << endl;
	}
}
int main()
{
	int TT = 1;
	cin >> TT;
	while (TT--)
	{
		solve();
	}
	return 0;
}
相关推荐
qq_25183645730 分钟前
基于java Web 动漫视频网站毕业论文
java·开发语言·前端
野生风长32 分钟前
C++入门基础:从命名空间到引用与指针的全面解析
开发语言·c++
FoldWinCard1 小时前
D6 Python 基础语法 --- 保留关键字
开发语言·python
风吹心凉2 小时前
python3基础2026.7.22
开发语言·python
qq_452396232 小时前
第五篇:《接口与错误处理:Go 的哲学》
开发语言·后端·golang
Escalating_xu2 小时前
C++可变参数模板与引用折叠精讲
开发语言·c++
wuyk5552 小时前
59.嵌入式C语言高级宏定义实战:多行宏、字符串化与符号拼接
c语言·开发语言·stm32·单片机·嵌入式硬件·物联网·51单片机
云泽8082 小时前
Visual Studio 2026 新手保姆级教程:环境搭建、界面优化与编译器配置详解
开发语言·c++·c·开发工具·visual studio
2zcode3 小时前
基于MATLAB深度学习的乳腺钼靶影像乳腺癌智能诊断系统设计与实现
开发语言·深度学习·matlab·乳腺癌
MoonBit月兔3 小时前
错过大赛报名?MoonBit 开源大赛- 8 月黑客松正式开启
开发语言·ai-native·moonbit