B - Break Standard Weight
The balance was the first mass measuring instrument invented. In its traditional form, it consists of a pivoted horizontal lever of equal length arms, called the beam, with a weighing pan, also called scale, suspended from each arm (which is the origin of the originally plural term "scales" for a weighing instrument). The unknown mass is placed in one pan, and standard masses are added to this or the other pan until the beam is as close to equilibrium as possible. The standard weights used with balances are usually labeled in mass units, which are positive integers.
With some standard weights, we can measure several special masses object exactly, whose weight are also positive integers in mass units. For example, with two standard weights 1 and 5, we can measure the object with mass 1, 4, 5 or 6 exactly.
In the beginning of this problem, there are 2 standard weights, which masses are xand y. You have to choose a standard weight to break it into 2 parts, whose weights are also positive integers in mass units. We assume that there is no mass lost. For example, the origin standard weights are 4 and 9, if you break the second one into 4and 5, you could measure 7 special masses, which are 1, 3, 4, 5, 8, 9, 13. While if you break the first one into 1 and 3, you could measure 13 special masses, which are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13! Your task is to find out the maximum number of possible special masses.
Input
There are multiple test cases. The first line of input is an integer T < 500 indicating the number of test cases. Each test case contains 2 integers x and y. 2 ≤ x, y ≤ 100
Output
For each test case, output the maximum number of possible special masses.
Sample Input
2
4 9
10 10
Sample Output
13
9
//Full of love and hope for life
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
using namespace std;
int ans;
int via[1005];
int cnt;
int n,m;
void findAns( int a, int b, int c )
{
memset(via,0,sizeof(via));
int sum;
sum = a+b+c;
if ( sum>0 ) {
via[sum] ++;
}
sum = a+b-c;
if ( sum>0 ) {
via[sum] ++;
}
sum = a+c-b;
if ( sum>0 ) {
via[sum] ++;
}
sum = b+c-a;
if ( sum>0 ) {
via[sum] ++;
}
sum = a-b-c;
if ( sum>0 ) {
via[sum] ++;
}
sum = b-a-c;
if ( sum>0 ) {
via[sum] ++;
}
sum = c-a-b;
if ( sum>0 ) {
via[sum] ++;
}
sum = a+b;
if ( sum>0 ) {
via[sum] ++;
}
sum = a+c;
if ( sum>0 ) {
via[sum] ++;
}
sum = b+c;
if ( sum>0 ) {
via[sum] ++;
}
sum = a;
if ( sum>0 ) {
via[sum] ++;
}
sum = b;
if ( sum>0 ) {
via[sum] ++;
}
sum = c;
if ( sum>0 ) {
via[sum] ++;
}
sum = a-b;
if ( sum>0 ) {
via[sum] ++;
}
sum = a-c;
if ( sum>0 ) {
via[sum] ++;
}
sum = b-a;
if ( sum>0 ) {
via[sum] ++;
}
sum = b-c;
if ( sum>0 ) {
via[sum] ++;
}
sum = c-a;
if ( sum>0 ) {
via[sum] ++;
}
sum = c-b;
if ( sum>0 ) {
via[sum] ++;
}
int tot = 0;
int i;
for ( i=1; i<=400; i++ ) {
if ( via[i]>0 ) {
tot ++;
}
}
if ( tot>ans ) {
ans = tot;
}
}
int main()
{
int listt;
int i,j;
cin >> listt;
while ( listt-- ) {
ans = 0;
cnt = 0;
cin >> n >> m;
// 分n
for ( i=1; i<n; i++ ) {
for ( j=1; j<n; j++ ) {
if ( i+j==n && i<=j ) {
findAns(m,i,j);
}
}
}
// 分m
for ( i=1; i<m; i++ ) {
for ( j=1; j<m; j++ ) {
if ( i+j==m && i<=j ) {
findAns(n,i,j);
}
}
}
cout << ans << endl;
}
return 0;
}
D - Density of Power Network
The vast power system is the most complicated man-made system and the greatest engineering innovation in the 20th century. The following diagram shows a typical 14 bus power system. In real world, the power system may contains hundreds of buses and thousands of transmission lines.
Network topology analysis had long been a hot topic in the research of power system. And network density is one key index representing the robustness of power system. And you are asked to implement a procedure to calculate the network density of power system.
The network density is defined as the ratio between number of transmission lines and the number of buses. Please note that if two or more transmission lines connecting the same pair of buses, only one would be counted in the topology analysis.
Input
The first line contains a single integer T (T ≤ 1000), indicating there are T cases in total.
Each case begins with two integers N and M (2 ≤ N, M ≤ 500) in the first line, representing the number of buses and the number of transmission lines in the power system. Each Bus would be numbered from 1 to N.
The second line contains the list of start bus number of the transmission lines, separated by spaces.
The third line contains the list of corresponding end bus number of the transmission lines, separated by spaces. The end bus number of the transmission lines would not be the same as the start bus number.
Output
Output the network density of the power system in a single line, as defined in above. The answer should round to 3 digits after decimal point.
Sample Input
3
3 2
1 2
2 3
2 2
1 2
2 1
14 20
2 5 3 4 5 4 5 7 9 6 11 12 13 8 9 10 14 11 13 13
1 1 2 2 2 3 4 4 4 5 6 6 6 7 7 9 9 10 12 14
Sample Output
0.667
0.500
1.429
//Full of love and hope for life
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
int n[110][110];
using namespace std;
int main()
{
int b,c,a,i,k;
int n[510],m[510],s[510][510];
cin >> a;
while(a--)
{
k=0;
memset(s,0,sizeof(s));
cin >> b >> c;
for(i=1; i<=c; i++)
{
cin >> n[i];
}
for(i=1; i<=c; i++)
{
cin >> m[i];
}
for(i=1; i<=c; i++)
{
s[n[i]][m[i]]=1;
}
for(i=1; i<=c; i++)
{
if(s[n[i]][m[i]]==1)
{
if(s[m[i]][n[i]]==1)
{
k++;
s[m[i]][n[i]]=0;
s[n[i]][m[i]]=0;
}
else
{
k++;
s[n[i]][m[i]]=0;
}
}
}
printf("%.3lf\n",k*1.000/b);
}
return 0;
}
F - Friends
Alice lives in the country where people like to make friends. The friendship is bidirectional and if any two person have no less than k friends in common, they will become friends in several days. Currently, there are totally n people in the country, and m friendship among them. Assume that any new friendship is made only when they have sufficient friends in common mentioned above, you are to tell how many new friendship are made after a sufficiently long time.
Input
There are multiple test cases.
The first lien of the input contains an integer T (about 100) indicating the number of test cases. Then T cases follow. For each case, the first line contains three integers n, m, k (1 ≤ n ≤ 100, 0 ≤ m ≤ n×(n-1)/2, 0 ≤ k ≤ n, there will be no duplicated friendship) followed by m lines showing the current friendship. The ithfriendship contains two integers ui, vi (0 ≤ ui, vi < n, ui ≠ vi) indicating there is friendship between person ui and vi.
Note: The edges in test data are generated randomly.
Output
For each case, print one line containing the answer.
Sample Input
3
4 4 2
0 1
0 2
1 3
2 3
5 5 2
0 1
1 2
2 3
3 4
4 0
5 6 2
0 1
1 2
2 3
3 4
4 0
2 0
Sample Output
2
0
4
//Full of love and hope for life
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
int n[110][110];
int main()
{
int a,b,c,d,i,x,y,g,h,k,j,t;
cin >> a;
while(a--)
{
t=1;
h=0;
memset(n,0,sizeof(n));
cin >> b >> c >> d;
for(i=1; i<=c; i++)
{
cin >> x >> y;
n[x][y]=1;
n[y][x]=1;
}
while(t)
{
t=0;
for(i=0; i<b-1; i++)
{
for(j=i+1; j<b; j++)
{
k=0;
if(n[i][j]==0)
{
for(g=0; g<b; g++)
{
if(n[i][g]==1&&n[j][g]==1)
{
k++;
}
}
if(k>=d)
{
t=1;
h++;
n[i][j]=1;
n[j][i]=1;
}
}
}
}
}
cout << h << endl;
}
return 0;
}
H - Hard to Play
MightyHorse is playing a music game called osu!.
After playing for several months, MightyHorse discovered the way of calculating score in osu!:
1. While playing osu!, player need to click some circles following the rhythm. Each time a player clicks, it will have three different points: 300, 100 and 50, deciding by how clicking timing fits the music.
2. Calculating the score is quite simple. Each time player clicks and gets P points, the total score will add P, which should be calculated according to following formula:
P = Point * (Combo * 2 + 1)
Here Point is the point the player gets (300, 100 or 50) and Combo is the number of consecutive circles the player gets points previously - That means if the player doesn't miss any circle and clicks the ith circle, Combo should be i - 1.
Recently MightyHorse meets a high-end osu! player. After watching his replay, MightyHorse finds that the game is very hard to play. But he is more interested in another problem: What's the maximum and minimum total score a player can get if he only knows the number of 300, 100 and 50 points the player gets in one play?
As the high-end player plays so well, we can assume that he won't miss any circle while playing osu!; Thus he can get at least 50 point for a circle.
Input
There are multiple test cases.
The first line of input is an integer T (1 ≤ T ≤ 100), indicating the number of test cases.
For each test case, there is only one line contains three integers: A (0 ≤ A ≤ 500) - the number of 300 point he gets, B (0 ≤ B ≤ 500) - the number of 100 point he gets and C (0 ≤ C ≤ 500) - the number of 50 point he gets.
Output
For each test case, output a line contains two integers, describing the minimum and maximum total score the player can get.
Sample Input
1
2 1 1
Sample Output
2050 3950
//Full of love and hope for life
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
int a,b,c,d,e;
int main()
{
while(cin>>a)
{
while(a--)
{
cin>>b>>c>>d;
int preb=b;
int prec=c;
int pred=d;
int maxx,minn;
maxx=minn=0;
int sum=b+c+d;
for(int i=1; i<=sum; i++)
{
if(d>0)
{
maxx+=50*((i-1)*2+1);
d--;
}
else if(c>0)
{
maxx+=100*((i-1)*2+1);
c--;
}
else if(b>0)
{
maxx+=300*((i-1)*2+1);
b--;
}
}
for(int i=1; i<=sum; i++)
{
if(preb>0)
{
minn+=300*((i-1)*2+1);
preb--;
}
else if(prec>0)
{
minn+=100*((i-1)*2+1);
prec--;
}
else if(pred>0)
{
minn+=50*((i-1)*2+1);
pred--;
}
}
cout<<minn<<" "<<maxx<<endl;
}
}
return 0;
}
J - Java Beans
There are N little kids sitting in a circle, each of them are carrying some java beans in their hand. Their teacher want to select M kids who seated in M consecutive seats and collect java beans from them.
The teacher knows the number of java beans each kids has, now she wants to know the maximum number of java beans she can get from M consecutively seated kids. Can you help her?
Input
There are multiple test cases. The first line of input is an integer T indicating the number of test cases.
For each test case, the first line contains two integers N (1 ≤ N ≤ 200) and M (1 ≤ M ≤ N). Here N and M are defined in above description. The second line of each test case contains N integers Ci (1 ≤ Ci ≤ 1000) indicating number of java beans the ith kid have.
Output
For each test case, output the corresponding maximum java beans the teacher can collect.
Sample Input
2
5 2
7 3 1 3 9
6 6
13 28 12 10 20 75
Sample Output
16
158
//Full of love and hope for life
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
using namespace std;
int main()
{
int n[410],sum,t;
int a,b,c,i,j;
cin >> a;
while(a--)
{
t=0;
sum=0;
cin >> b >> c;
for(i=1; i<=b; i++)
{
cin >> n[i];
}
for(i=b+1; i<=2*b; i++)
{
n[i]=n[i-b];
}
for(i=1; i<=b;i++)
{
t=0;
for(j=i; j<=i+c-1; j++)
{
t=t+n[j];
}
sum=max(t,sum);
}
cout << sum << endl;
}
return 0;
}
I - In 7-bit
Very often, especially in programming contests, we treat a sequence of non-whitespace characters as a string. But sometimes, a string may contain whitespace characters or even be empty. We can have such strings quoted and escaped to handle these cases. However, a different approach is putting the length of the string before it. As most strings are short in practice, it would be a waste of space to encode the length as a 64-bit unsigned integer or add a extra separator between the length and the string. That's why a 7-bit encoded integer is introduced here.
To store the string length by 7-bit encoding, we should regard the length as a binary integer. It should be written out by seven bits at a time, starting with the seven least-significant (i.e. 7 rightmost) bits. The highest (i.e. leftmost) bit of a byte indicates whether there are more bytes to be written after this one. If the integer fits in seven bits, it takes only one byte of space. If the integer does not fit in seven bits, the highest bit is set to 1 on the first byte and written out. The integer is then shifted by seven bits and the next byte is written. This process is repeated until the entire integer has been written.
With the help of 7-bit encoded integer, we can store each string as a length-prefixed string by concatenating its 7-bit encoded length and its raw content (i.e. the original string).
Input
There are multiple test cases. The first line of input is an integer T indicating the number of test cases.
Each test case is simply a string in a single line with at most 3000000 characters.
Output
For each test case, output the corresponding length-prefixed string in uppercase hexadecimal. See sample for more details.
Sample Input
3 42 yukkuri shiteitte ne!!! https://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Everything#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe_and_Everything_.2842.29
Sample Output
023432 1779756B6B75726920736869746569747465206E65212121 9A0168747470733A2F2F656E2E77696B6970656469612E6F72672F77696B692F416E737765725F746F5F4C6966652C5F7468655F556E6976657273652C5F616E645F45766572797468696E6723416E737765725F746F5F7468655F556C74696D6174655F5175657374696F6E5F6F665F4C6966652E32435F7468655F556E6976657273655F616E645F45766572797468696E675F2E323834322E3239
//Full of love and hope for life
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
char s[3000010];
int main()
{
int a,i,len;
cin >> a;
getchar();
while(a--){
gets(s);
len=strlen(s);
if(len==0){
cout << "00" << endl;
continue;
}
int t=len;
while(t){
int sum=0;
sum=sum+t%(1<<7);
t=t/(1<<7);
if(t){
sum=sum+(1<<7);
}
printf("%02X",sum);
}
for(i=0; i<len; i++){
printf("%02X",s[i]);
}
cout << endl;
}
return 0;
}