ACM 第七天

水题

B - Minimum’s Revenge

There is a graph of n vertices which are indexed from 1 to n. For any pair of different vertices, the weight of the edge between them is the least common multiple of their indexes.

Mr. Frog is wondering about the total weight of the minimum spanning tree. Can you help him?

InputThe first line contains only one integer T (T100), which indicates the number of test cases.

For each test case, the first line contains only one integer n (2n109), indicating the number of vertices.
OutputFor each test case, output one line "Case #x:y",where x is the case number (starting from 1) and y is the total weight of the minimum spanning tree.
Sample Input
2
2
3
Sample Output
Case #1: 2
Case #2: 5

        
 
Hint
In the second sample, the graph contains 3 edges which are (1, 2, 2), (1, 3, 3) and (2, 3, 6). Thus the answer is 5.
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int t;
 5     long long ans;
 6    scanf("%d",&t);
 7     int v=0;
 8     while(t--)
 9     {
10         long long n;
11         scanf("%lld",&n);
12 
13         ans=(n+2)*(n-1)/2;
14 
15         printf("Case #%d: %lld\n",++v,ans);
16 
17     }
18     return 0;
19 }

 

C - Mr. Frog’s Problem

One day, you, a clever boy, feel bored in your math class, and then fall asleep without your control. In your dream, you meet Mr. Frog, an elder man. He has a problem for you.

He gives you two positive integers A and B, and your task is to find all pairs of integers (C, D), such that ACB,ADB and AB+BACD+DC

 

Inputfirst line contains only one integer T (T125), which indicates the number of test cases. Each test case contains two integers A and B (1AB1018).OutputFor each test case, first output one line "Case #x:", where x is the case number (starting from 1).

Then in a new line, print an integer s indicating the number of pairs you find.

In each of the following s lines, print a pair of integers C and D. pairs should be sorted by C, and then by D in ascending order.
Sample Input
2
10 10
9 27
Sample Output
Case #1:
1
10 10
Case #2:
2
9 27
27 9

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int t;
 5     scanf("%d",&t);
 6     long long a,b;
 7     long long cas=1;
 8     while(t--)
 9     {
10         scanf("%lld%lld",&a,&b);
11         printf("Case #%lld:\n",cas++);
12         if(a==b)
13         {
14              printf("1\n",a,b);
15             printf("%lld %lld\n",a,b);
16         }
17         else
18         {
19              printf("2\n",a,b);
20             printf("%lld %lld\n",a,b);
21             printf("%lld %lld\n",b,a);
22         }
23 
24     }
25 }

 


D - Mr. Frog’s Game

One day, Mr. Frog is playing Link Game (Lian Lian Kan in Chinese).



In this game, if you can draw at most three horizontal or vertical head-and-tail-connected lines over the empty grids(the lines can be out of the whole board) to connect two non-empty grids with the same symbol or the two non-empty grids with the same symbol are adjacent, then you can change these two grids into empty and get several more seconds to continue the game.

Now, Mr. Frog starts a new game (that means there is no empty grid in the board). If there are no pair of grids that can be removed together,Mr. Frog will say ”I’m angry” and criticize you.

Mr. Frog is battle-scarred and has seen many things, so he can check the board in a very short time, maybe one second. As a Hong Kong Journalist, what you should do is to check the board more quickly than him, and then you can get out of the room before Mr. Frog being angry.

InputThe first line contains only one integer T (T500), which indicates the number of test cases.

For each test case, the first line contains two integers n and m (1n,m30).

In the next n lines, each line contains m integers,  j-th number in the i-th line means the symbol on the grid(the same number means the same symbol on the grid).
OutputFor each test case, there should be one line in the output.

You should output “Case #x: y”,where x is the case number(starting from 1), and y is a string representing the answer of the question. If there are at least one pair of grids that can be removed together, the y is “Yes”(without quote), else y is “No”.Sample Input
2
3 3
1 2 1
2 1 2
1 2 1
3 3
1 2 3
2 1 2
3 2 1
Sample Output
Case #1: Yes
Case #2: No


        
 
Hint
first sample can be explained as below.

         
  
 1 #include<stdio.h>
 2 #include<string.h>
 3 using namespace std;
 4 int n,m,g[40][40];
 5 int f[4][2]= {{0,1},{1,0},{-1,0},{0,-1}};
 6 bool dfs()
 7 {
 8     for(int i=1; i<=n; i++)
 9         for(int j=i+1; j<=n; j++)
10             if(g[i][1]==g[j][1])
11                 return true;
12     for(int i=1; i<=n; i++)
13         for(int j=i+1; j<=n; j++)
14             if(g[i][m]==g[j][m])
15                 return true;
16     for(int i=1; i<=m; i++)
17         for(int j=i+1; j<=m; j++)
18             if(g[1][i]==g[1][j])
19                 return true;
20     for(int i=1; i<=m; i++)
21         for(int j=i+1; j<=m; j++)
22             if(g[n][i]==g[n][j])
23                 return true;
24     for(int i=2; i<n; i++)
25     {
26         for(int j=2; j<m; j++)
27         {
28             for(int k=0; k<4; k++)
29             {
30                 int x=f[k][0]+i,y=f[k][1]+j;
31                 if(g[i][j]==g[x][y])
32                     return true;
33             }
34         }
35     }
36     return false;
37 }
38 int main()
39 {
40     int T;
41     scanf("%d",&T);
42     int t=1;
43     while(T--)
44     //for(int t=1; t<=T; t++)
45     {
46         scanf("%d %d",&n,&m);
47         for(int i=1; i<=n; i++)
48             for(int j=1; j<=m; j++)
49                 scanf("%d",&g[i][j]);
50         printf("Case #%d: ",t++);
51         if(dfs())
52             printf("Yes\n");
53         else
54             printf("No\n");
55     }
56     return 0;
57 }

G - Left-handers, Right-handers and Ambidexters

You are at a water bowling training. There are l people who play with their left hand, r people, who play with their right hand, and a ambidexters, who can play with left or right hand.

The coach decided to form a team of even number of players, exactly half of the players should play with their right hand, and exactly half of the players should play with their left hand. One player should use only on of his hands.

Ambidexters play as well with their right hand as with their left hand. In the team, an ambidexter can play with their left hand, or with their right hand.

Please find the maximum possible size of the team, where equal number of players use their left and right hands, respectively.


Input

The only line contains three integers l, r and a (0 ≤ l, r, a ≤ 100) — the number of left-handers, the number of right-handers and the number of ambidexters at the training.

Output

Print a single even integer — the maximum number of players in the team. It is possible that the team can only have zero number of players.

Examples
Input
1 4 2
Output
6
Input
5 5 5
Output
14
Input
0 2 0
Output
0
Note

In the first example you can form a team of 6 players. You should take the only left-hander and two ambidexters to play with left hand, and three right-handers to play with right hand. The only person left can't be taken into the team.

In the second example you can form a team of 14 people. You have to take all five left-handers, all five right-handers, two ambidexters to play with left hand and two ambidexters to play with right hand.

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int l,r,m;
 5     scanf("%d%d%d",&l,&r,&m);
 6     if(l==r)
 7     {
 8         if(m%2==0) printf("%d\n",l+r+m);
 9         else printf("%d\n",l+r+m-1);
10     }
11     else
12     {
13         if(l>r)
14         {
15             int temp=l;
16             l=r;
17             r=temp;
18         }
19         if(m<=r-l)
20         {
21             printf("%d\n",(m+l)*2);
22         }
23         else
24         {
25             m=m-(r-l);
26             if(m&1)
27                 printf("%d\n",2*r+m-1);
28             else printf("%d\n",2*r+m);
29         }
30         return 0;
31     }
32 
33 }

I - Zebras

Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.

Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days.


Input

In the only line of input data there is a non-empty string s consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as |s|) does not exceed 200 000 characters.

Output

If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer k (1 ≤ k ≤ |s|), the resulting number of subsequences. In the i-th of following k lines first print the integer li (1 ≤ li ≤ |s|), which is the length of the i-th subsequence, and then li indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to n must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.

Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of k.

Examples
Input
0010100
Output
3
3 1 3 4
3 2 5 6
1 7
Input
111
Output
-1
 1 #include<stdio.h>
 2 #include<vector>
 3 #include<string.h>
 4 using namespace std;
 5 
 6 #define N 200100
 7 int a[N];
 8 char str[N];
 9 int cnt;
10 int n,k;
11 vector <int> ans[N];
12 int main()
13 {
14     scanf("%s",str);
15     n=strlen(str);
16     for(int i=0;i<n;i++)
17     {
18         a[i+1]=str[i]-'0';
19     }
20     cnt=0;
21     for(int i=1;i<=n;i++)
22     {
23         if(a[i]==0) ans[cnt++].push_back(i);
24         else if(a[i]==1)
25         {
26             if(cnt==0)
27                 {printf("-1\n");
28             return 0;}
29             ans[--cnt].push_back(i);
30         }
31         k=max(k,cnt);
32     }
33     if(k!=cnt)printf("-1\n");
34     else
35     {
36         printf("%d\n",cnt);
37         for(int i=0;i<cnt;i++)
38         {
39             printf("%d",ans[i].size());
40             for(int j=0;j<ans[i].size();j++)
41             {
42                 printf(" %d",ans[i][j]);
43             }
44             printf("\n");
45         }
46     }
47     return 0;
48 }

多校练习赛3

Problem D. Euler Function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1340    Accepted Submission(s): 1008


Problem Description
In number theory, Euler's totient function φ(n) counts the positive integers up to a given integer n that are relatively prime to n. It can be defined more formally as the number of integers k in the range 1kn for which the greatest common divisor gcd(n,k) is equal to 1.
For example, φ(9)=6 because 1,2,4,5,7 and 8 are coprime with 9. As another example, φ(1)=1 since for n=1 the only integer in the range from 1 to n is 1 itself, and gcd(1,1)=1.
A composite number is a positive integer that can be formed by multiplying together two smaller positive integers. Equivalently, it is a positive integer that has at least one divisor other than 1 and itself. So obviously 1 and all prime numbers are not composite number.
In this problem, given integer k, your task is to find the k-th smallest positive integer n, that φ(n) is a composite number.
 


Input
The first line of the input contains an integer T(1T100000), denoting the number of test cases.
In each test case, there is only one integer k(1k109).
 


Output
For each test case, print a single line containing an integer, denoting the answer.
 


Sample Input
2 1 2
 


Sample Output
5 7
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int t;
 5     long long k;
 6     scanf("%d",&t);
 7     while(t--)
 8     {
 9         scanf("%lld",&k);
10         if(k==1) printf("5\n");
11         else
12         {
13             printf("%lld\n",k+5);
14         }
15     }
16     return 0;
17 }

 

Problem F. Grab The Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1581    Accepted Submission(s): 762


Problem Description
Little Q and Little T are playing a game on a tree. There are n vertices on the tree, labeled by 1,2,...,n, connected by n1 bidirectional edges. The i-th vertex has the value of wi.
In this game, Little Q needs to grab some vertices on the tree. He can select any number of vertices to grab, but he is not allowed to grab both vertices that are adjacent on the tree. That is, if there is an edge between x and y, he can't grab both x and y. After Q's move, Little T will grab all of the rest vertices. So when the game finishes, every vertex will be occupied by either Q or T.
The final score of each player is the bitwise XOR sum of his choosen vertices' value. The one who has the higher score will win the game. It is also possible for the game to end in a draw. Assume they all will play optimally, please write a program to predict the result.
 

 

Input
The first line of the input contains an integer T(1T20), denoting the number of test cases.
In each test case, there is one integer n(1n100000) in the first line, denoting the number of vertices.
In the next line, there are n integers w1,w2,...,wn(1wi109), denoting the value of each vertex.
For the next n1 lines, each line contains two integers u and v, denoting a bidirectional edge between vertex u and v.
 

 

Output
For each test case, print a single line containing a word, denoting the result. If Q wins, please print Q. If T wins, please print T. And if the game ends in a draw, please print D.
 

 

Sample Input
1 3 2 2 2 1 2 1 3
 

 

Sample Output
Q
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int t,n,a,b;
 5     int aq[100010];
 6     scanf("%d",&t);
 7 
 8     while(t--)
 9     {
10         int ans=0;
11         scanf("%d",&n);
12 
13         for(int i=0; i<n; i++)
14         {
15             scanf("%d",&aq[i]);
16             ans=ans^aq[i];
17 
18         }
19         for(int i=0; i<n-1; i++)
20         {
21             scanf("%d %d",&a,&b);
22         }
23         if(ans==0)
24             printf("D\n");
25         else printf("Q\n");
26 
27     }
28     return 0;
29 }

 

预定义

 1 #include <cstdio>
 2 #include <vector>
 3 #include <queue>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <map>
 7 #include <string>
 8 #include <iostream>
 9 
10 using namespace std;
11 #define sd(n) scanf("%d",&n)
12 #define sdd(n,m) scanf("%d%d",&n,&m)
13 #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
14 #define pd(n) printf("%d\n", (n))
15 #define pdd(n,m) printf("%d %d", n, m)
16 #define pld(n) printf("%lld\n", n)
17 #define pldd(n,m) printf("%lld %lld\n", n, m)
18 #define sld(n) scanf("%lld",&n)
19 #define sldd(n,m) scanf("%lld%lld",&n,&m)
20 #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
21 #define sf(n) scanf("%lf",&n)
22 #define sff(n,m) scanf("%lf%lf",&n,&m)
23 #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
24 #define ss(str) scanf("%s",str)
25 #define rep(i,a,n) for (int i=a;i<n;i++)
26 #define per(i,a,n) for (int i=n-1;i>=a;i--)
27 #define mm(a,n) memset(a, n, sizeof(a))
28 #define debug(x) cout<<#x<<": "<<x<<endl
29 #define pb push_back
30 #define all(x) (x).begin(),(x).end()
31 #define fi first
32 #define se second
33 typedef pair<int,int> PII;
34 typedef long long ll;
35 typedef unsigned long long ull;
36 typedef long double ld;
37 const ll mod = 1000000007;
38 const double eps = 1e-6;
39 const int inf = 0x3f3f3f3f;
40 
41 //head

 

Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.

Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.

Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.

You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.


Input

The first line contains two integers n, m (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages.

The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message.

The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message.

It is guaranteed that x1 + ... + xn = y1 + ... + ym. Also, it is guaranteed that x1 + ... + xn ≤ 106.

Output

Print the maximum number of files the intercepted array could consist of.

Examples
Input
7 6
2 5 3 1 11 4 4
7 8 2 4 1 8
Output
3
Input
3 3
1 10 100
1 100 10
Output
2
Input
1 4
4
1 1 1 1
Output
1
Note

In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.

In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.

In the third example the only possibility is that the archive contains a single file of size 4.

 1 #include<stdio.h>
 2 int a[100010];
 3 int b[100010];
 4 int main()
 5 {
 6     int n,m;
 7     scanf("%d%d",&n,&m);
 8     for(int i=1; i<=n; i++)
 9     {
10         scanf("%d",&a[i]);
11     }
12     for(int i=1; i<=m; i++)
13     {
14         scanf("%d",&b[i]);
15     }
16     int i=1,j=1;
17     int sum=0;
18     int sum1=a[1];
19     int sum2=b[1];
20     while(i<=n &&j<=m)
21     {
22         if(sum1>sum2)
23         {
24             j++;
25             sum2+=b[j];
26         }
27         else if(sum1<sum2)
28         {
29             i++;
30             sum1+=a[i];
31         }
32         else
33         {
34             sum++;
35             i++;
36             j++;
37             if(i<=n &&j<=m)
38             {
39                 sum1=a[i];
40                 sum2=b[j];
41             }
42 
43         }
44 
45 
46     }
47     printf("%d\n",sum);
48 }

 

posted @ 2018-07-30 14:52  白雪儿  Views(179)  Comments(0Edit  收藏  举报