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

蓝桥杯国赛训练 day1

目录

k倍区间

舞狮

交换瓶子


k倍区间

取模后算组合数就行

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;public class Main {static Scanner sc = new Scanner(System.in);public static void main(String[] args) {solve();}public static void solve() {int n = sc.nextInt();long k = sc.nextLong();//        System.out.println(calC(2,5));long[] arr = new long[n + 1];long[] pre = new long[n + 1];for (int i = 1; i <= n; i++) {arr[i] = sc.nextLong();pre[i] = pre[i - 1] + arr[i];}HashMap<Long,Long>hm=new HashMap<>();for(int i=1;i<=n;i++) {long preKey=pre[i]%k;hm.put(preKey, hm.getOrDefault(preKey, 0L)+1);}long cnt=0;if(hm.containsKey(0L)) cnt+=hm.get(0L);for(long l:hm.keySet()) {long preVal=hm.get(l);if(preVal>=2) {cnt+=calC(2,preVal);} }System.out.println(cnt);}/***   计算组合数* @param m 上标* @param n 下标* @return*/public static long calC(long m, long n) {m = Math.min(m, n - m); long result = 1;for (long i = 1; i <= m; i++) {result *= (n - m + i) ;result /= i;}return result;}
}

组合数模版

    /*** 计算组合数* @param m 上标* @param n 下标* @return*/public static long calC(long m, long n) {m = Math.min(m, n - m); long result = 1;for (long i = 1; i <= m; i++) {result *= (n - m + i) ;result /= i;}return result;}

舞狮

暴力就完了

不然应该是一个dfs 找环

import java.util.*;// xixi♡西
public class Main {static Scanner sc = new Scanner(System.in);public static void solve() {int n=sc.nextInt();long arr[]=new long[n];for(int i=0;i<n;i++) {arr[i]=sc.nextLong();}Arrays.sort(arr);ArrayList<ArrayList<Long>>list=new ArrayList<>();loop:for(long num:arr) {boolean isAdd =false;for(ArrayList<Long> forList:list) {if(num>forList.size()) {isAdd=true;forList.add(num);continue loop;}}if(isAdd==false) {ArrayList<Long>newList=new ArrayList<>();newList.add(num);list.add(newList);}}System.out.print(list.size());}public static void main(String[] args){int t = 1;
//        t = sc.nextInt();while (t-- > 0) {solve();}}}

交换瓶子

import java.util.*;public class Main {static Scanner sc = new Scanner(System.in);public static void main(String[] args) {solve();}public static void solve() {int n = sc.nextInt();int[] arr = new int[n + 1];for(int i = 1; i <= n; i ++){arr[i] = sc.nextInt();}int count = 0;for(int i = 1; i <= n; i ++){if(arr[i] != i){int temp = arr[i];arr[i] = arr[temp];arr[temp] = temp; count++;i = 1;}}System.out.println(count);}}
http://www.lqws.cn/news/113491.html

相关文章:

  • BGP笔记的基本概要
  • Abaqus连接器弹片正向力分析:
  • 【高等数学】(2)函数
  • 域自适应 (Domain Adaptation,DA)基础
  • Ubuntu系统 | 本地部署ollama+deepseek
  • MySQL存储过程
  • MySQL 搜索特定桩号距离之间的数据
  • 传输层协议:网络通信的关键纽带
  • 12.7 LangChain实战:1.2秒响应!用LCEL构建高效RAG系统,准确率提升41%
  • 现代密码学介绍
  • 从0开始学linux韦东山教程第四章问题小结(3)
  • 【LUT技术专题】图像自适应3DLUT代码讲解
  • 蛋白质设计软件LigandMPNN介绍
  • 数据加密标准(DES)解析
  • 40、响应处理-【源码分析】-基于请求参数的内容协商原理
  • malloc 内存分配机制:brk 与 mmap
  • C#面试问题61-80
  • 时代星光推出战狼W60智能运载无人机,主要性能超市场同类产品一倍!
  • PDF.js无法显示数字签名
  • 基于MATLAB的FTN调制和硬判决的实现
  • 《仿盒马》app开发技术分享-- 个人中心关于逻辑完善(端云一体)
  • 关于线缆行业设备数据采集异构问题的解决
  • 实现对deepseek流式返回的json数据,进行逐字解析并实时渲染
  • 【计算机网络】第七章 运输层
  • 蛋白质结构预测软件openfold介绍
  • 永磁同步电机控制算法--基于PR电流环的矢量控制
  • HCIP(BGP基础)
  • 模型上下文协议(MCP)简介
  • 【HarmonyOS 5】鸿蒙mPaas详解
  • 【网络安全】SRC漏洞挖掘思路/手法分享