• 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区分开)

    1. import java.io.*;
    2. import java.math.BigInteger;
    3. import java.util.Arrays;
    4. import java.util.BitSet;
    5. import java.util.HashMap;
    6. import java.util.HashSet;
    7. import java.util.Iterator;
    8. import java.util.LinkedList;
    9. import java.util.PriorityQueue;
    10. import java.util.Queue;
    11. import java.util.Random;
    12. import java.util.Stack;
    13. import java.util.StringTokenizer;
    14. import java.util.Vector;
    15. public class Main{
    16. static long md=(long)998244353;
    17. static long Linf=Long.MAX_VALUE/2;
    18. static int inf=Integer.MAX_VALUE/2;
    19. public static void main(String[] args) throws IOException{
    20. AReader input=new AReader();
    21. PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    22. int n=input.nextInt();
    23. int[] a=new int[n+1];
    24. HashMap hs=new HashMap();
    25. long ans=0;
    26. for(int i=1;i<=n;++i) {
    27. a[i]=input.nextInt();
    28. if(a[i]==0) {
    29. continue;
    30. }
    31. int t=a[i];
    32. int y=(int)Math.sqrt(a[i]);
    33. while(y>0) {
    34. int z=y*y;
    35. if(t%z==0) {
    36. t/=z;
    37. break;
    38. }
    39. y--;
    40. }
    41. if(hs.get(t)!=null) {
    42. int p=hs.get(t);
    43. ans+=p;
    44. hs.put(t, p+1);
    45. }else {
    46. hs.put(t, 1);
    47. }
    48. }
    49. int zero=0;
    50. for(int i=1;i<=n;++i) {
    51. if(a[i]==0) {
    52. ans+=i-1;
    53. zero++;
    54. }else {
    55. ans+=zero;
    56. }
    57. }
    58. out.print(ans);
    59. out.flush();
    60. out.close();
    61. }
    62. //System.out.println();
    63. //out.println();
    64. //String o="abcdefghijklmnopqrstuvwxyz";
    65. //char[] op=o.toCharArray();
    66. static
    67. class AReader{
    68. BufferedReader bf;
    69. StringTokenizer st;
    70. BufferedWriter bw;
    71. public AReader(){
    72. bf=new BufferedReader(new InputStreamReader(System.in));
    73. st=new StringTokenizer("");
    74. bw=new BufferedWriter(new OutputStreamWriter(System.out));
    75. }
    76. public String nextLine() throws IOException{
    77. return bf.readLine();
    78. }
    79. public String next() throws IOException{
    80. while(!st.hasMoreTokens()){
    81. st=new StringTokenizer(bf.readLine());
    82. }
    83. return st.nextToken();
    84. }
    85. public char nextChar() throws IOException{
    86. //确定下一个token只有一个字符的时候再用
    87. return next().charAt(0);
    88. }
    89. public int nextInt() throws IOException{
    90. return Integer.parseInt(next());
    91. }
    92. public long nextLong() throws IOException{
    93. return Long.parseLong(next());
    94. }
    95. public double nextDouble() throws IOException{
    96. return Double.parseDouble(next());
    97. }
    98. public float nextFloat() throws IOException{
    99. return Float.parseFloat(next());
    100. }
    101. public byte nextByte() throws IOException{
    102. return Byte.parseByte(next());
    103. }
    104. public short nextShort() throws IOException{
    105. return Short.parseShort(next());
    106. }
    107. public BigInteger nextBigInteger() throws IOException{
    108. return new BigInteger(next());
    109. }
    110. public void println() throws IOException {
    111. bw.newLine();
    112. }
    113. public void println(int[] arr) throws IOException{
    114. for (int value : arr) {
    115. bw.write(value + " ");
    116. }
    117. println();
    118. }
    119. public void println(int l, int r, int[] arr) throws IOException{
    120. for (int i = l; i <= r; i ++) {
    121. bw.write(arr[i] + " ");
    122. }
    123. println();
    124. }
    125. public void println(int a) throws IOException{
    126. bw.write(String.valueOf(a));
    127. bw.newLine();
    128. }
    129. public void print(int a) throws IOException{
    130. bw.write(String.valueOf(a));
    131. }
    132. public void println(String a) throws IOException{
    133. bw.write(a);
    134. bw.newLine();
    135. }
    136. public void print(String a) throws IOException{
    137. bw.write(a);
    138. }
    139. public void println(long a) throws IOException{
    140. bw.write(String.valueOf(a));
    141. bw.newLine();
    142. }
    143. public void print(long a) throws IOException{
    144. bw.write(String.valueOf(a));
    145. }
    146. public void println(double a) throws IOException{
    147. bw.write(String.valueOf(a));
    148. bw.newLine();
    149. }
    150. public void print(double a) throws IOException{
    151. bw.write(String.valueOf(a));
    152. }
    153. public void print(char a) throws IOException{
    154. bw.write(String.valueOf(a));
    155. }
    156. public void println(char a) throws IOException{
    157. bw.write(String.valueOf(a));
    158. bw.newLine();
    159. }
    160. }
    161. }

    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的时刻
    • 由于有最晚出发时间的限制,所以不会有走环的情况
    1. import java.io.*;
    2. import java.math.BigInteger;
    3. import java.util.Arrays;
    4. import java.util.BitSet;
    5. import java.util.HashMap;
    6. import java.util.HashSet;
    7. import java.util.Iterator;
    8. import java.util.LinkedList;
    9. import java.util.PriorityQueue;
    10. import java.util.Queue;
    11. import java.util.Random;
    12. import java.util.Stack;
    13. import java.util.StringTokenizer;
    14. import java.util.Vector;
    15. public class Main{
    16. static long md=(long)998244353;
    17. static long Linf=Long.MAX_VALUE/2;
    18. static int inf=Integer.MAX_VALUE/2;
    19. static
    20. class Edge{
    21. int fr,to,nxt;
    22. long l,d,k,c;
    23. public Edge(int u,int v,long L,long D,long K,long C) {
    24. fr=u;
    25. to=v;
    26. l=L;
    27. d=D;
    28. c=C;
    29. k=K;
    30. }
    31. }
    32. static Edge[] e;
    33. static int[] head;
    34. static int cnt;
    35. static void addEdge(int fr,int to,long l,long d,long k,long c) {
    36. cnt++;
    37. e[cnt]=new Edge(fr, to, l, d, k, c);
    38. e[cnt].nxt=head[fr];
    39. head[fr]=cnt;
    40. }
    41. static
    42. class Node{
    43. int x;
    44. long dis;
    45. public Node(int X,long D) {
    46. x=X;
    47. dis=D;
    48. }
    49. }
    50. public static void main(String[] args) throws IOException{
    51. AReader input=new AReader();
    52. PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    53. int n=input.nextInt();
    54. int m=input.nextInt();
    55. e=new Edge[m+1];
    56. head=new int[n+1];
    57. cnt=0;
    58. for(int i=1;i<=m;++i) {
    59. long l=input.nextLong();
    60. long d=input.nextLong();
    61. long k=input.nextLong();
    62. long c=input.nextLong();
    63. int u=input.nextInt();
    64. int v=input.nextInt();
    65. addEdge(v, u, l, d, k, c);
    66. }
    67. PriorityQueue q=new PriorityQueue((o1,o2)->{
    68. if(o2.dis-o1.dis>0)return 1;
    69. else if(o2.dis-o1.dis<0)return -1;
    70. else return 0;
    71. });
    72. long[] dis=new long[n+1];
    73. Arrays.fill(dis, -1);
    74. dis[n]=Linf;
    75. q.add(new Node(n, Linf));
    76. while(!q.isEmpty()) {
    77. Node now=q.peek();
    78. q.poll();
    79. int x=now.x;
    80. if(x!=n&&dis[x]>=now.dis)continue;
    81. dis[x]=now.dis;
    82. for(int i=head[x];i>0;i=e[i].nxt) {
    83. int v=e[i].to;
    84. long c=e[i].c;
    85. long may=dis[x]-c;
    86. long l=e[i].l;
    87. long d=e[i].d;
    88. long k=e[i].k;
    89. if(may>=l) {
    90. may=l+Math.min((may-l)/d, k-1)*d;
    91. q.add(new Node(v, may));
    92. }
    93. }
    94. }
    95. for(int i=1;i
    96. if(dis[i]==-1) {
    97. out.println("Unreachable");
    98. }else out.println(dis[i]);
    99. }
    100. out.flush();
    101. out.close();
    102. }
    103. //System.out.println();
    104. //out.println();
    105. static
    106. class AReader{
    107. BufferedReader bf;
    108. StringTokenizer st;
    109. BufferedWriter bw;
    110. public AReader(){
    111. bf=new BufferedReader(new InputStreamReader(System.in));
    112. st=new StringTokenizer("");
    113. bw=new BufferedWriter(new OutputStreamWriter(System.out));
    114. }
    115. public String nextLine() throws IOException{
    116. return bf.readLine();
    117. }
    118. public String next() throws IOException{
    119. while(!st.hasMoreTokens()){
    120. st=new StringTokenizer(bf.readLine());
    121. }
    122. return st.nextToken();
    123. }
    124. public char nextChar() throws IOException{
    125. //确定下一个token只有一个字符的时候再用
    126. return next().charAt(0);
    127. }
    128. public int nextInt() throws IOException{
    129. return Integer.parseInt(next());
    130. }
    131. public long nextLong() throws IOException{
    132. return Long.parseLong(next());
    133. }
    134. public double nextDouble() throws IOException{
    135. return Double.parseDouble(next());
    136. }
    137. public float nextFloat() throws IOException{
    138. return Float.parseFloat(next());
    139. }
    140. public byte nextByte() throws IOException{
    141. return Byte.parseByte(next());
    142. }
    143. public short nextShort() throws IOException{
    144. return Short.parseShort(next());
    145. }
    146. public BigInteger nextBigInteger() throws IOException{
    147. return new BigInteger(next());
    148. }
    149. public void println() throws IOException {
    150. bw.newLine();
    151. }
    152. public void println(int[] arr) throws IOException{
    153. for (int value : arr) {
    154. bw.write(value + " ");
    155. }
    156. println();
    157. }
    158. public void println(int l, int r, int[] arr) throws IOException{
    159. for (int i = l; i <= r; i ++) {
    160. bw.write(arr[i] + " ");
    161. }
    162. println();
    163. }
    164. public void println(int a) throws IOException{
    165. bw.write(String.valueOf(a));
    166. bw.newLine();
    167. }
    168. public void print(int a) throws IOException{
    169. bw.write(String.valueOf(a));
    170. }
    171. public void println(String a) throws IOException{
    172. bw.write(a);
    173. bw.newLine();
    174. }
    175. public void print(String a) throws IOException{
    176. bw.write(a);
    177. }
    178. public void println(long a) throws IOException{
    179. bw.write(String.valueOf(a));
    180. bw.newLine();
    181. }
    182. public void print(long a) throws IOException{
    183. bw.write(String.valueOf(a));
    184. }
    185. public void println(double a) throws IOException{
    186. bw.write(String.valueOf(a));
    187. bw.newLine();
    188. }
    189. public void print(double a) throws IOException{
    190. bw.write(String.valueOf(a));
    191. }
    192. public void print(char a) throws IOException{
    193. bw.write(String.valueOf(a));
    194. }
    195. public void println(char a) throws IOException{
    196. bw.write(String.valueOf(a));
    197. bw.newLine();
    198. }
    199. }
    200. }

  • 相关阅读:
    第四章redis配置文件的介绍
    服务器推送数据之SSE (server send event)
    一分钟搞定基于Saltstack集群批量安装部署Docker
    SpringMVC
    农业信息学复习资料
    [附源码]Python计算机毕业设计jspm郫县兼职信息系统
    php服装商城网站毕业设计源码241505
    如何查找无物流信息单号
    C# 查找[m,n]范围内的所有素数
    FFmpeg音视频复用器----为啥大多数视频只有一个视频流和一个音频流
  • 原文地址:https://blog.csdn.net/Xing_ke/article/details/136287962