Codeforces Round 925 (Div. 3) D. Divisible Pairs (Java)

Codes Round 925 (Div. 3) D. Divisible Pairs (Java)

比赛链接:Codeforces Round 925 (Div. 3)

D题传送门:D.Divisible Pairs

题目:D.Divisible Pairs

题目描述

在这里插入图片描述

输出格式

For each test case, output a single integer — the number of beautiful pairs in the array $ a $ .

样例 #1

样例输入 #1

7
6 5 2
1 2 7 4 9 6
7 9 5
1 10 15 3 8 12 15
9 4 10
14 10 2 2 11 11 13 5 6
9 5 6
10 7 6 7 9 7 7 10 10
9 6 2
4 9 7 1 2 2 13 3 15
9 2 3
14 6 1 15 12 15 8 2 15
10 5 7
13 3 3 2 12 11 3 7 13 14

样例输出 #1

2
0
1
3
5
7
0

分析:

题目要我们求美丽对的数量。

由美丽对的定义可得

  • a i + a j a_i + a_j ai+aj % x = 0 x = 0 x=0 等价于 ( a i (a_i % x = 0 ai% x + a j a_j % x = 0 aj% x)%x = 0
  • a i − a j a_i - a_j aiaj % y = 0 y = 0 y=0 等价于 a i a_i % x ai%y = a j a_j % x = 0 aj% y

我们定义一个HashMap的集合,集合的键 List 用来记录对 ai%x 和 ai%y 的答案,集合的值用来记录对应键出现的次数。

定义一个变量 t 来记录 a i a_i ai

定义 ArrayList< Integer > a 先记录 t%x ,再记录 t%y
定义 ArrayList< Intege r> b 先记录 (x-a.get(0))%x ,再记录 t%y

计算 map 中 b 出现的次数,然后将 a 加入到 map 中

代码:

import java.util.*;

public class Main{
    public static void main(String[] args) {     
    	Scanner sc = new Scanner(System.in);
    	int tr = sc.nextInt();
    	while(tr-->0) {
    		int n = sc.nextInt();
    		int x = sc.nextInt();int y = sc.nextInt();
    		Map<List<Integer>,Long> map = new HashMap<>();
    		long ans = 0;
    		for(int i = 0;i < n;i++) {
    			int t = sc.nextInt();
    			ArrayList<Integer> a = new ArrayList<>();
    			ArrayList<Integer> b = new ArrayList<>();
    			a.add(t%x);a.add(t%y);
    			b.add((x-a.get(0))%x);b.add(t%y);
    			if(map.containsKey(b)) {
    				ans += map.get(b);
    			}
    			map.put(a,map.getOrDefault(a, (long) 0)+1);
    		}
    		System.out.println(ans);
    	}
    	sc.close();
    }
}
  • 39
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值