蓝桥杯国赛训练 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);
    }
    
}
相关推荐
WispX8882 分钟前
【设计模式】门面/外观模式
java·开发语言·设计模式·系统架构·外观模式·插件·架构设计
琢磨先生David5 分钟前
简化复杂系统的优雅之道:深入解析 Java 外观模式
java·设计模式·外观模式
ademen6 分钟前
spring4第7-8课-AOP的5种通知类型+切点定义详解+执行顺序
java·spring
要阿尔卑斯吗17 分钟前
对一个变化的 Set 使用 SSCAN,元素被扫描的情况:
redis
快乐肚皮20 分钟前
EasyExcel高级特性和技术选型
java
寒士obj28 分钟前
Java对象创建过程
java·开发语言
Java知识库37 分钟前
「深度拆解」Spring Boot如何用DeepSeek重构MCP通信层?从线程模型到分布式推理的架构进化
java·开发语言·spring boot·程序员·编程
Bruce_Liuxiaowei41 分钟前
PHP文件读取漏洞全面剖析:触发点与利用技术
开发语言·php
摸鱼码42 分钟前
(头歌作业)-6.5 幻方(project)
开发语言·python
进阶的DW1 小时前
新手小白使用VMware创建虚拟机安装Linux
java·linux·运维