题目链接:http://abc070.contest.atcoder.jp/assignments
A - Palindromic Number
Time limit : 2sec / Memory limit : 256MB
Score : 100 points
Problem Statement
You are given a three-digit positive integer N.
Determine whether N is a palindromic number.
Here, a palindromic number is an integer that reads the same backward as forward in decimal notation.
Constraints
- 100≤N≤999
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
If N is a palindromic number, print Yes
; otherwise, print No
.
Sample Input 1
575
Sample Output 1
Yes
N=575 is also 575 when read backward, so it is a palindromic number. You should print Yes
.
Sample Input 2
123
Sample Output 2
No
N=123 becomes 321 when read backward, so it is not a palindromic number. You should print No
.
Sample Input 3
812
Sample Output 3
No
题解:水水
1 #include2 #include 3 #include 4 #include 5 #include <string> 6 #include 7 using namespace std; 8 #define ll long long 9 const int N=10005; 10 const int mod=1e9+7; 11 const int maxn=1e7; 12 int main() 13 { 14 int n; 15 while(cin>>n){ 16 int a=n/100; 17 int b=(b-a*100)/10; 18 int c=n%10; 19 if(a==c) cout<<"Yes"<<endl; 20 else cout<<"No"<<endl; 21 } 22 return 0; 23 }
B - Two Switches
Time limit : 2sec / Memory limit : 256MB
Score : 200 points
Problem Statement
Alice and Bob are controlling a robot. They each have one switch that controls the robot.
Alice started holding down her button A second after the start-up of the robot, and released her button B second after the start-up.
Bob started holding down his button C second after the start-up, and released his button D second after the start-up.
For how many seconds both Alice and Bob were holding down their buttons?
Constraints
- 0≤A<B≤100
- 0≤C<D≤100
- All input values are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the length of the duration (in seconds) in which both Alice and Bob were holding down their buttons.
Sample Input 1
0 75 25 100
Sample Output 1
50
Alice started holding down her button 0 second after the start-up of the robot, and released her button 75 second after the start-up.
Bob started holding down his button 25 second after the start-up, and released his button 100 second after the start-up.
Therefore, the time when both of them were holding down their buttons, is the 50 seconds from 25 seconds after the start-up to 75 seconds after the start-up.
Sample Input 2
0 33 66 99
Sample Output 2
0
Alice and Bob were not holding their buttons at the same time, so the answer is zero seconds.
Sample Input 3
10 90 20 80
Sample Output 3
60
题解:分情况讨论即可
1 #include2 #include 3 #include 4 #include 5 #include <string> 6 #include 7 using namespace std; 8 #define ll long long 9 const int N=10005; 10 const int mod=1e9+7; 11 const int maxn=1e7; 12 int main() 13 { 14 int a,b,c,d; 15 while(cin>>a>>b>>c>>d){ 16 if(c>b||a>d) cout<<0<<endl; 17 else if(a<=c&&d<=b){ 18 cout< endl; 19 } 20 else if(c<=a&&b<=d){ 21 cout< endl; 22 } 23 else if(a<=c&&c<=b&&b<=d){ 24 cout< endl; 25 } 26 else if(c<=a&&a<=d&&d<=b){ 27 cout< endl; 28 } 29 } 30 return 0; 31 }
C - Multiple Clocks
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
We have N clocks. The hand of the i-th clock (1≤i≤N) rotates through 360° in exactly Ti seconds.
Initially, the hand of every clock stands still, pointing directly upward.
Now, Dolphin starts all the clocks simultaneously.
In how many seconds will the hand of every clock point directly upward again?
Constraints
- 1≤N≤100
- 1≤Ti≤1018
- All input values are integers.
- The correct answer is at most 1018 seconds.
Input
Input is given from Standard Input in the following format:
N T1 : TN
Output
Print the number of seconds after which the hand of every clock point directly upward again.
Sample Input 1
2 2 3
Sample Output 1
6
We have two clocks. The time when the hand of each clock points upward is as follows:
- Clock 1: 2, 4, 6, … seconds after the beginning
- Clock 2: 3, 6, 9, … seconds after the beginning
Therefore, it takes 6 seconds until the hands of both clocks point directly upward.
Sample Input 2
5 2 5 10 1000000000000000000 1000000000000000000
Sample Output 2
1000000000000000000
题解:就是求最小公倍数 (如果只有一个数直接输出)
1 #include2 #include 3 #include 4 #include 5 #include <string> 6 #include 7 using namespace std; 8 #define ll long long 9 const int N=10005; 10 const int mod=1e9+7; 11 const int maxn=1e7; 12 ll a[101]; 13 ll gcd(ll x,ll y) 14 { 15 ll c; 16 ll m=x,n=y; 17 while(y!=0){ 18 c=x%y; 19 x=y; 20 y=c; 21 } 22 return m/x*n; 23 } 24 int main() 25 { 26 int n; 27 cin>>n; 28 ll m,s,l; 29 cin>>m; 30 if(n==1) cout< endl; 31 else if(n>1){ 32 cin>>l; 33 s=gcd(l,m); 34 for(int i=0;i 2;i++){ 35 cin>>m; 36 s=gcd(s,m); 37 } 38 cout< endl; 39 } 40 return 0; 41 }
D - Transit Tree Path
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
You are given a tree with N vertices.
Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N−1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N−1) connects Vertices ai and bi, and has a length of ci.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
- find the length of the shortest path from Vertex xj and Vertex yj via Vertex K.
Constraints
- 3≤N≤105
- 1≤ai,bi≤N(1≤i≤N−1)
- 1≤ci≤109(1≤i≤N−1)
- The given graph is a tree.
- 1≤Q≤105
- 1≤K≤N
- 1≤xj,yj≤N(1≤j≤Q)
- xj≠yj(1≤j≤Q)
- xj≠K,yj≠K(1≤j≤Q)
Input
Input is given from Standard Input in the following format:
N a1 b1 c1 : aN−1 bN−1 cN−1 Q K x1 y1 : xQ yQ
Output
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
Sample Input 1
5 1 2 1 1 3 1 2 4 1 3 5 1 3 1 2 4 2 3 4 5
Sample Output 1
3 2 4
The shortest paths for the three queries are as follows:
- Query 1: Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3
- Query 2: Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2
- Query 3: Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4
Sample Input 2
7 1 2 1 1 3 3 1 4 5 1 5 7 1 6 9 1 7 11 3 2 1 3 4 5 6 7
Sample Output 2
5 14 22
The path for each query must pass Vertex K=2.
Sample Input 3
10 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 5 6 1000000000 6 7 1000000000 7 8 1000000000 8 9 1000000000 9 10 1000000000 1 1 9 10
Sample Output 3
17000000000
题解:dfs
1 #include2 #include 3 #include 4 #include <string> 5 #include 6 using namespace std; 7 const int maxn = 1e5+5; 8 const int N = 1000; 9 typedef long long ll; 10 int t,head[maxn]; 11 ll dist[maxn],w; 12 bool vis[maxn]; 13 struct Edge 14 { 15 int from,to,nxt; 16 ll cost; 17 }e[2*maxn]; 18 void addedge(int u,int v,int w) 19 { 20 e[t].from=u; 21 e[t].to=v; 22 e[t].cost=w; 23 e[t].nxt=head[u]; 24 head[u]=t++; 25 } 26 void dfs(int u,int fa) 27 { 28 for(int i=head[u];i!=-1;i=e[i].nxt){ 29 int to=e[i].to; 30 if(to==fa) continue; 31 dist[to]=dist[u]+e[i].cost; 32 dfs(to,u); 33 } 34 } 35 int main() 36 { 37 int n; 38 while(cin>>n){ 39 t=0; 40 memset(head,-1,sizeof(head)); 41 memset(dist,0,sizeof(dist)); 42 int u,v; 43 for(int i=1;i ){ 44 cin>>u>>v>>w; 45 addedge(u,v,w); 46 addedge(v,u,w); 47 } 48 int q,k; 49 cin>>q>>k; 50 dfs(k,-1); 51 int x,y; 52 for(int i=0;i ){ 53 cin>>x>>y; 54 cout<endl; 55 } 56 } 57 return 0; 58 }