Cards CodeForces 399C

Description

User ainta loves to play with cards. He has a cards containing letter "o" and b cards containing letter "x". He arranges the cards in a row, and calculates the score of the deck by the formula below.

  1. At first, the score is 0.
  2. For each block of contiguous "o"s with length x the score increases by x2.
  3. For each block of contiguous "x"s with length y the score decreases by y2.
 

For example, if a = 6, b = 3 and ainta have arranged the cards in the order, that is described by string "ooxoooxxo", the score of the deck equals 22 - 12 + 32 - 22 + 12 = 9. That is because the deck has 5 blocks in total: "oo", "x", "ooo", "xx", "o".

User ainta likes big numbers, so he wants to maximize the score with the given cards. Help ainta make the score as big as possible. Note, that he has to arrange all his cards.

Input

The first line contains two space-separated integers a and b (0 ≤ a, b ≤ 105a + b ≥ 1) — the number of "o" cards and the number of "x" cards.

Output

In the first line print a single integer v — the maximum score that ainta can obtain.

In the second line print a + b characters describing the deck. If the k-th card of the deck contains "o", the k-th character must be "o". If the k-th card of the deck contains "x", the k-th character must be "x". The number of "o" characters must be equal to a, and the number of "x " characters must be equal to b. If there are many ways to maximize v, print any.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Sample Input

Input
2 3
Output
-1
xoxox
Input
4 0
Output
16
oooo
Input
0 4
Output
-16
xxxx

 1 #include<cstring>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstdio>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     long long n,m;
11     while(scanf("%I64d%I64d",&n,&m)!=EOF)
12     {
13         long long  ans,k;
14         long long  mi;
15         if(n<m) mi=n;
16         else mi=m;
17         if(n==0)
18         {
19             ans=-m*m;
20             cout<<ans<<endl;
21             for(long long  i=0;i<m;i++) printf("x");
22             printf("
");
23             continue;
24         }
25         else if(m==0)
26         {
27             ans=n*n;
28             cout<<ans<<endl;
29             for(long long  i=0;i<n;i++) printf("o");
30             printf("
");
31             continue;
32         }
33         for(long long  i=1;i<=mi;i++)
34         {
35             long long x=m/(i+1);
36             long long le=m-x*(i+1);
37             long long ansa=(n-i+1)*(n-i+1)+i-1;
38             long long ansb=le*(x+1)*(x+1)+(i+1-le)*x*x;
39             long long sum=ansa-ansb;
40            // cout<<sum<<endl;
41             if(i==1)
42                 ans=sum,k=i;
43             else if(sum>ans)
44             {
45                 ans=sum;
46                 k=i;
47             }
48         }
49 
50         printf("%I64d
",ans);
51         long long  p=0;
52         long long  x=m/(k+1);
53         long long  le=m-x*(k+1);
54         for(long long  i=0;i<le;i++)
55         {
56             for(long long  j=0;j<x+1;j++) printf("x");
57             if(!i) for(long long j=0;j<n-k+1;j++) printf("o"),p++;
58             else printf("o"),p++;
59         }
60 
61         for(long long  i=0;i<k+1-le;i++)
62         {
63             for(long long  j=0;j<x;j++) printf("x");
64             if(!p) for(long long j=0;j<n-k+1;j++) printf("o"),p++;
65             else if(p<n) printf("o"),p++;
66         }
67         printf("
");
68     }
69 }
原文地址:https://www.cnblogs.com/wsaaaaa/p/4520114.html