2018中国大学生程序设计竞赛 - 网络选拔赛
Find Integer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6597 Accepted Submission(s): 1999
Special Judge
Problem Description
people in USSS love math very much, and there is a famous math problem .
give you two integers n,a,you are required to find 2 integers b,c such that an+bn=cn.
Input
one line contains one integer T;(1≤T
next T lines contains two integers n,a;(0≤n≤1000,000,000,3≤a≤40000)
Output
print two integers b,c if b,c exits;(1≤b,c≤1000,000,000);
else print two integers -1 -1 instead.
Sample Input
Sample Output
题解:费马大定理,又被称为“费马最后的定理”,它断言当整数n >2时,关于x, y, z的方程 x^n + y^n = z^n 没有正整数解。
1 #include<cstdio>
2 #include<iostream>
3 using namespace std;
4 int main()
5 {
6 int t,n,a;
7 scanf("%d",&t);
8 while(t--)
9 {
10 scanf("%d %d",&n,&a);
11 if(n>2||n==0)
12 printf("-1 -1\n");
13 else if(n==1)
14 printf("1 %d",a+1);
15 else
16 {
17 if(a%2)
18 {
19 int n = (a-1)/2;
20 int b = 2*n*n+2*n;
21 int c = b+1;
22 printf("%d %d\n",b,c);
23 }else
24 {
25 int n = a/2;
26 int b = n*n-1;
27 int c = b+2;
28 printf("%d %d\n",b,c);
29 }
30 }
31 }
32 return 0;
33 }
Tree and Permutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3086 Accepted Submission(s): 816
The set { 1,2,3,…,N } contains a total of N unique permutations, let’s say the i-th permutation is P and P is its j-th number.
For the i-th permutation, it can be a traverse sequence of the tree with N vertices, which means we can go from the P-th vertex to the P-th vertex by the shortest path, then go to the P-th vertex ( also by the shortest path ) , and so on. Finally we’ll reach the P-th vertex, let’s define the total distance of this route as D(P , so please calculate the sum of D(P for all N permutations.
The first line of each test case contains one integer N ( 1≤N ) .
For the next N lines, each line contains three integer X, Y and L, which means there is an edge between X-th vertex and Y-th of length L ( 1≤X ) .
每条边两端的点数的计算,实际上是可以用一次dfs解决的。任取一点为根,在dfs的过程中,对每个点k记录其子树包含的点数(包括其自身),设点数为a[k],则k的父亲一侧的点数即为N-a[k]。这个统计可以和遍历同时进行。故时间复杂度为O(n)。