当前位置: 首页 > 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.dtcms.com/a/243752.html

相关文章:

  • PocketSCP:蛋白质口袋动态时空拓扑可视化分析新方法
  • 论文略读:DAILYDILEMMAS:REVEALINGVALUEPREFERENCES OFLLMSWITHQUANDARIESOFDAILYLIFE
  • OCR(光学字符识别)算法
  • 智能合约中人工智能驱动的漏洞分析:趋势、挑战与未来方向
  • 【leetcode-合并两个有序链表】
  • AI助手一键生成专业PPT(Gamma/Genspark/Kimi)
  • [Git] 标签管理
  • 三.Gitee远程操作标签操作
  • FastAPI基础入门(三)
  • 智慧医疗能源事业线深度画像分析(下)
  • MySQL 基础笔记
  • Reactive-Resume:重构你的简历编写体验
  • 【系统设计【1】】系统设计面试方法论:从0到百万用户的需求到架构的推演
  • 分布式I/O在食品包装行业中的应用
  • 【 WWDC25:新系统,新命名】
  • < 买了个麻烦 (二) 618 京东云--轻量服务器 > 可以为您申请全额退订呢。 挣取来的,东京云 轻量服务器,可以“全额退款“
  • 【Linux】进程控制(下)---程序替换宝藏岛
  • 前端~三维地图(cesium)点位聚合
  • 黑马教程强化day2-1
  • 七牛云图片上传 前后端全过程
  • vue封装的echarts组件被同一个页面多次引用只显示一个的问题
  • Uncaught (in promise) TypeError: Cannot read properties of null (reading ‘xxx’)
  • Win10重装系统 (重生篇:我在华强修电脑)
  • AIGC方案-java实现视频伪动效果
  • SpringBoot + 自建GitLab + Jenkins + CentOS Stream 9 来实现自动化部署
  • 御微半导体面试总结
  • 内存泄漏系列专题分析之二十:camx swap内存泄漏实例分析
  • Jenkins + Docker + Kubernetes(JKD)自动化部署全链路实践
  • 基于OpenCV的图像增强技术:直方图均衡化与自适应直方图均衡化
  • 零基础设计模式——行为型模式 - 备忘录模式