当前位置: 首页 > news >正文

D. Plus Minus Permutation

time limit per test

1 second

memory limit per test

256 megabytes

You are given 33 integers — nn, xx, yy. Let's call the score of a permutation†† p1,…,pnp1,…,pn the following value:

(p1⋅x+p2⋅x+…+p⌊nx⌋⋅x)−(p1⋅y+p2⋅y+…+p⌊ny⌋⋅y)(p1⋅x+p2⋅x+…+p⌊nx⌋⋅x)−(p1⋅y+p2⋅y+…+p⌊ny⌋⋅y)

In other words, the score of a permutation is the sum of pipi for all indices ii divisible by xx, minus the sum of pipi for all indices ii divisible by yy.

You need to find the maximum possible score among all permutations of length nn.

For example, if n=7n=7, x=2x=2, y=3y=3, the maximum score is achieved by the permutation [2,6–,1–,7–,5,4––,3][2,6_,1_,7_,5,4__,3] and is equal to (6+7+4)−(1+4)=17−5=12(6+7+4)−(1+4)=17−5=12.

†† A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in any order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (the number 22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3, but the array contains 44).

Input

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

Then follows the description of each test case.

The only line of each test case description contains 33 integers nn, xx, yy (1≤n≤1091≤n≤109, 1≤x,y≤n1≤x,y≤n).

Output

For each test case, output a single integer — the maximum score among all permutations of length nn.

Example

Input

Copy

 

8

7 2 3

12 6 3

9 1 9

2 2 2

100 20 50

24 4 6

1000000000 5575 25450

4 4 1

Output

Copy

12
-3
44
0
393
87
179179179436104
-6

Note

The first test case is explained in the problem statement above.

In the second test case, one of the optimal permutations will be [12,11,2–,4,8,9––,10,6,1–,5,3,7––][12,11,2_,4,8,9__,10,6,1_,5,3,7__]. The score of this permutation is (9+7)−(2+9+1+7)=−3(9+7)−(2+9+1+7)=−3. It can be shown that a score greater than −3−3 can not be achieved. Note that the answer to the problem can be negative.

In the third test case, the score of the permutation will be (p1+p2+…+p9)−p9(p1+p2+…+p9)−p9. One of the optimal permutations for this case is [9,8,7,6,5,4,3,2,1][9,8,7,6,5,4,3,2,1], and its score is 4444. It can be shown that a score greater than 4444 can not be achieved.

In the fourth test case, x=yx=y, so the score of any permutation will be 00.

解题说明:此题是一道数学题,设z为x与y的最小公倍数,本质上是找到n/x与n/y还有n/z这三个数字是多少,然后这就能确定两个数列的长度分别为n/x-n/z与n/y-n/z。然后就能高斯求和找到1到n中最大的(n/x-n/z)个数相加再减去1到n中最小的(n/y-n/z)个数的和。

#include<stdio.h>
long long int gcd(long long int x, long long int y)
{long long int t;if (y > x){t = y;y = x;x = t;}while (y > 0){x = x % y;t = x;x = y;y = t;}return x;
}
int main()
{long long int t;scanf("%lld", &t);for (long long int i = 0; i < t; i++){long long int n, x, y;long long int a, b;long long int z;scanf("%lld %lld %lld", &n, &x, &y);a = n / x;b = n / y;z = x * y / gcd(x, y);z = n / z;a = a - z;b = b - z;a = n * a - (a - 1) * a / 2;b = b * (b + 1) / 2;printf("%lld\n", a - b);}return 0;
}

http://www.xdnf.cn/news/992143.html

相关文章:

  • day28/60
  • 常用的免费网络API接口
  • 脑机新手指南(九):高性能脑文本通信:手写方式实现(上)
  • 【navigator.clipboard】复制链接弹出详情信息(模拟电商app)、页面中粘贴图片、复制文本自动添加版权信息
  • CentOS7自带的yum依然无法联网到官方源
  • 自我推荐一下
  • 关于亚马逊WOOT折扣力度
  • 中国北方GNSS业务站网积雪深度数据集(GSnow-CHINA v1.0, 12h/24h, 2013-2...
  • 【烧脑算法】三指针的降维打击:三线并行锁定解题细节
  • 数据隐私是什么?如何做好数据隐私规范?
  • Nuttx之mm_extend
  • Python数据类型大全:整型、浮点、字符串与布尔值
  • Codeforces 1029 Div3(ABCDE)
  • Windows10下利用VS2019编译JpegLib
  • seo优化新利器:AI如何让内容批量生成与排名提升双管齐下?
  • Gremlin创建schema(包括实体和关系)
  • 【质数】埃氏筛法、线性筛法(欧拉筛法)
  • 【Linux系统编程】System V
  • Java锁机制对决:ReadWriteLock vs StampedLock
  • 从0到1落地一个RAG智能客服系统
  • ConcurrentHashMap详解:原理、实现与并发控制
  • 专访伦敦发展促进署CEO:在AI与黄仁勋之外,伦敦为何给泡泡玛特和比亚迪留了C位?
  • MySQL优化器
  • 3.3.1_2 检错编码(循环冗余校验码)
  • 【完整源码+数据集+部署教程】安检爆炸物检测系统源码和数据集:改进yolo11-REPVGGOREPA
  • 接口测试之文件上传
  • 【完整源码+数据集+部署教程】石材实例分割系统源码和数据集:改进yolo11-CA-HSFPN
  • 【Docker】快速入门与项目部署实战
  • Haclon例程1-<剃须刀片检测程序详解>
  • < 买了个麻烦 (二) 618 京东云--轻量服务器 > “可以为您申请全额退订呢。“ 工单记录:可以“全额退款“