HUAWEI Programming Contest 2024(AtCoder Beginner Contest 342)

D - Square Pair

题目大意

  • 给一长为n的数组a,问有多少对A_i,A_j(1\leq i< j\leq n),两者相乘为非负整数完全平方数

解题思路

  • 一个数除以其能整除的最大的完全平方数,看前面有多少个与其余数相同的数,两者乘积满足条件(已经是完全平方数的部分无用)
  • 对于0,特判(与%=0区分开)

import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;







public class Main{
	static long md=(long)998244353;
	static long Linf=Long.MAX_VALUE/2;
	static int inf=Integer.MAX_VALUE/2;
	

	
	public static void main(String[] args) throws IOException{
		AReader input=new AReader();
	    PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	    
	    int n=input.nextInt();
	    int[] a=new int[n+1];
	    HashMap<Integer, Integer> hs=new HashMap<Integer, Integer>();
	    long ans=0;
	    for(int i=1;i<=n;++i) {
	    	a[i]=input.nextInt();
	    	if(a[i]==0) {
	    		continue;
	    	}
	    	int t=a[i];
	    	int y=(int)Math.sqrt(a[i]);
	    	while(y>0) {
	    		int z=y*y;
	    		if(t%z==0) {
	    			t/=z;
	    			break;
	    		}
	    		y--;
	    	}
	    	
	    	if(hs.get(t)!=null) {
	    		int p=hs.get(t);
	    		ans+=p;
	    		hs.put(t, p+1);
	    	}else {
	    		hs.put(t, 1);
	    	}
	    }
	    int zero=0;
	    for(int i=1;i<=n;++i) {
	    	if(a[i]==0) {
	    		ans+=i-1;
	    		zero++;
	    	}else {
	    		ans+=zero;
	    	}
	    }
	    out.print(ans);
	    
 	    out.flush();
	    out.close();
	}
	//System.out.println();
	//out.println();
	//String o="abcdefghijklmnopqrstuvwxyz";
	//char[] op=o.toCharArray();
	static
	class AReader{ 
	    BufferedReader bf;
	    StringTokenizer st;
	    BufferedWriter bw;

	    public AReader(){
	        bf=new BufferedReader(new InputStreamReader(System.in));
	        st=new StringTokenizer("");
	        bw=new BufferedWriter(new OutputStreamWriter(System.out));
	    }
	    public String nextLine() throws IOException{
	        return bf.readLine();
	    }
	    public String next() throws IOException{
	        while(!st.hasMoreTokens()){
	            st=new StringTokenizer(bf.readLine());
	        }
	        return st.nextToken();
	    }
	    public char nextChar() throws IOException{
	        //确定下一个token只有一个字符的时候再用
	        return next().charAt(0);
	    }
	    public int nextInt() throws IOException{
	        return Integer.parseInt(next());
	    }
	    public long nextLong() throws IOException{
	        return Long.parseLong(next());
	    }
	    public double nextDouble() throws IOException{
	        return Double.parseDouble(next());
	    }
	    public float nextFloat() throws IOException{
	        return Float.parseFloat(next());
	    }
	    public byte nextByte() throws IOException{
	        return Byte.parseByte(next());
	    }
	    public short nextShort() throws IOException{
	        return Short.parseShort(next());
	    }
	    public BigInteger nextBigInteger() throws IOException{
	        return new BigInteger(next());
	    }
	    public void println() throws IOException {
	        bw.newLine();
	    }
	    public void println(int[] arr) throws IOException{
	        for (int value : arr) {
	            bw.write(value + " ");
	        }
	        println();
	    }
	    public void println(int l, int r, int[] arr) throws IOException{
	        for (int i = l; i <= r; i ++) {
	            bw.write(arr[i] + " ");
	        }
	        println();
	    }
	    public void println(int a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	    public void print(int a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void println(String a) throws IOException{
	        bw.write(a);
	        bw.newLine();
	    }
	    public void print(String a) throws IOException{
	        bw.write(a);
	    }
	    public void println(long a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	    public void print(long a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void println(double a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	    public void print(double a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void print(char a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void println(char a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	}
}


E - Last Train

题目大意

  • n个点m条边,每条边有信息l,d,k,c

  • 表示l,l+d,l+2*d,\cdots ,l+(k-1)*d时刻有代价为c的路径

  • 1\rightarrow n-1每个点到n的最长距离

解题思路

  •  单一汇点,多源点,反向建图
  • 若当前时间为tim,则到下一个点的时间为may=tim-c
  • 则下一点最晚出发的时间为其等差数列中最大的小于may的时刻
  • 由于有最晚出发时间的限制,所以不会有走环的情况
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;







public class Main{
	static long md=(long)998244353;
	static long Linf=Long.MAX_VALUE/2;
	static int inf=Integer.MAX_VALUE/2;
	
	static
	class Edge{
		int fr,to,nxt;
		long l,d,k,c;
		public Edge(int u,int v,long L,long D,long K,long C) {
			fr=u;
			to=v;
			l=L;
			d=D;
			c=C;
			k=K;
		}
	}
	static Edge[] e;
	static int[] head;
	static int cnt;
	static void addEdge(int fr,int to,long l,long d,long k,long c) {
		cnt++;
		e[cnt]=new Edge(fr, to, l, d, k, c);
		e[cnt].nxt=head[fr];
		head[fr]=cnt;
	}
	
	static 
	class Node{
		int x;
		long dis;
		public Node(int X,long D) {
			x=X;
			dis=D;
		}
	}
	
	
	public static void main(String[] args) throws IOException{
		AReader input=new AReader();
	    PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	    int n=input.nextInt();
	    int m=input.nextInt();
	    e=new Edge[m+1];
	    head=new int[n+1];
	    cnt=0;
	    for(int i=1;i<=m;++i) {
	    	long l=input.nextLong();
	    	long d=input.nextLong();
	    	long k=input.nextLong();
	    	long c=input.nextLong();
	    	int u=input.nextInt();
	    	int v=input.nextInt();
	    	addEdge(v, u, l, d, k, c);
	    }
	    PriorityQueue<Node> q=new PriorityQueue<Node>((o1,o2)->{
	    	if(o2.dis-o1.dis>0)return 1;
	    	else if(o2.dis-o1.dis<0)return -1;
	    	else return 0;
	    });
	    
	    long[] dis=new long[n+1];
	    Arrays.fill(dis, -1);
	    dis[n]=Linf;
	    q.add(new Node(n, Linf));
	    while(!q.isEmpty()) {
	    	Node now=q.peek();
	    	q.poll();
	    	int x=now.x;
	    	if(x!=n&&dis[x]>=now.dis)continue;
	    	dis[x]=now.dis;
	    	for(int i=head[x];i>0;i=e[i].nxt) {
	    		int v=e[i].to;
	    		long c=e[i].c;
	    		long may=dis[x]-c;
	    		long l=e[i].l;
	    		long d=e[i].d;
	    		long k=e[i].k;
	    		if(may>=l) {
	    			may=l+Math.min((may-l)/d, k-1)*d;
	    			q.add(new Node(v, may));
	    		}
	    	}
	    }
	    for(int i=1;i<n;++i) {
	    	if(dis[i]==-1) {
	    		out.println("Unreachable");
	    	}else out.println(dis[i]);
	    }
	    
 	    out.flush();
	    out.close();
	}
	//System.out.println();
	//out.println();
	static
	class AReader{ 
	    BufferedReader bf;
	    StringTokenizer st;
	    BufferedWriter bw;

	    public AReader(){
	        bf=new BufferedReader(new InputStreamReader(System.in));
	        st=new StringTokenizer("");
	        bw=new BufferedWriter(new OutputStreamWriter(System.out));
	    }
	    public String nextLine() throws IOException{
	        return bf.readLine();
	    }
	    public String next() throws IOException{
	        while(!st.hasMoreTokens()){
	            st=new StringTokenizer(bf.readLine());
	        }
	        return st.nextToken();
	    }
	    public char nextChar() throws IOException{
	        //确定下一个token只有一个字符的时候再用
	        return next().charAt(0);
	    }
	    public int nextInt() throws IOException{
	        return Integer.parseInt(next());
	    }
	    public long nextLong() throws IOException{
	        return Long.parseLong(next());
	    }
	    public double nextDouble() throws IOException{
	        return Double.parseDouble(next());
	    }
	    public float nextFloat() throws IOException{
	        return Float.parseFloat(next());
	    }
	    public byte nextByte() throws IOException{
	        return Byte.parseByte(next());
	    }
	    public short nextShort() throws IOException{
	        return Short.parseShort(next());
	    }
	    public BigInteger nextBigInteger() throws IOException{
	        return new BigInteger(next());
	    }
	    public void println() throws IOException {
	        bw.newLine();
	    }
	    public void println(int[] arr) throws IOException{
	        for (int value : arr) {
	            bw.write(value + " ");
	        }
	        println();
	    }
	    public void println(int l, int r, int[] arr) throws IOException{
	        for (int i = l; i <= r; i ++) {
	            bw.write(arr[i] + " ");
	        }
	        println();
	    }
	    public void println(int a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	    public void print(int a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void println(String a) throws IOException{
	        bw.write(a);
	        bw.newLine();
	    }
	    public void print(String a) throws IOException{
	        bw.write(a);
	    }
	    public void println(long a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	    public void print(long a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void println(double a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	    public void print(double a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void print(char a) throws IOException{
	        bw.write(String.valueOf(a));
	    }
	    public void println(char a) throws IOException{
	        bw.write(String.valueOf(a));
	        bw.newLine();
	    }
	}
}


  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
华为OD机试真题2024 PDF是华为公司推出的在线笔试平台的一道题目,具体内容未给出,以下回答仅为猜测。 根据题目的命名,可以猜测这是华为面向2024年的在线笔试题目。华为作为一家知名的科技公司,其面试流程一般包括在线笔试环节,用于筛选应聘者的能力和素质。 华为OD机试真题2024 PDF可能涉及以下内容: 1. 编程能力测试:华为作为一家技术导向的公司,对于应聘者的编程能力往往有较高的要求。题目可能涉及各种编程语言,如C/C++、Java、Python等,考察应聘者的算法设计和编码实现能力。 2. 数据结构与算法题目:为了评估应聘者的算法思维和解决问题的能力,题目可能涉及一些常见的数据结构和算法,如链表、树、图、排序算法、查找算法等。 3. 实际案例分析:华为作为一家全球化的科技企业,其业务涉及通信网、智能手机、云计算等领域。题目可能涉及实际案例分析,要求应聘者结合自己相关领域的知识,解决华为在某个具体问题上所遇到的挑战。 不同岗位对应聘者的要求有所不同,因此华为OD机试真题2024 PDF的具体内容需要根据具体岗位要求来进行推测。无论题目如何,应聘者需要充分准备,提前了解华为的业务和岗位要求,加强对编程和算法的学习,并注重实践操作能力的培养。只有通过充分准备,才能在机试中取得好成绩,增加被录用的机会。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值