Codeforces Round #640 (Div. 4)题解

Sum of Round Numbers

A positive (strictly greater than zero) integer is called round if it is of the form d00…0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from $ 1 1 1$ to $ 9 9 9$ (inclusive) are round.For example, the following numbers are round: $ 4000 4000 4000$, $ 1 1 1$, $ 9 9 9$, $ 800 800 800$, $ 90 90 90$. The following numbers are not round: $ 110 110 110$, $ 707 707 707$, $ 222 222 222$, $ 1001 1001 1001$.You are given a positive integer $ n n n$ ($ 1 ≤ n ≤ 1 0 4 1 \le n \le 10^4 1n104$). Represent the number $ n n n$ as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number $ n n n$ as a sum of the least number of terms, each of which is a round number.InputThe first line contains an integer $ t t t$ ($ 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104$) — the number of test cases in the input. Then $ t t t$ test cases follow.Each test case is a line containing an integer $ n n n$ ($ 1 ≤ n ≤ 1 0 4 1 \le n \le 10^4 1n104$).OutputPrint $ t t t$ answers to the test cases. Each answer must begin with an integer $ k k k$ — the minimum number of summands. Next, $ k k k$ terms must follow, each of which is a round number, and their sum is $ n n n$. The terms can be printed in any order. If there are several answers, print any of them.Exampleinput5
5009
7
9876
10000
10
output2
5000 9
1
7
4
800 70 6 9000
1
10000
1
10

思路:最多1e4,所以依次除以1000,100,10,存储到一个数组里输出即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> 
 using namespace std;
 int cnt[10];
int a[] = {1000, 100, 10, 1};
 int main(){
 int T;
 cin >> T;
 while(T --){
  int cnt1 = 0;
  int idx = 0;
  int n;
  scanf("%d", &n);
  for (int i = 0; i <4; i ++){
  if (n / a[i])    cnt[idx ++] = n / a[i] * a[i];
  n %= a[i];
     }
          cout << idx << endl;
     for (int i = 0; i < idx; i ++)
     if (i != idx - 1)   printf("%d ", cnt[i]);
     else                printf("%d\n", cnt[i]);
 }
  return 0;
}

Same Parity Summands

You are given two positive integers nn (1≤n≤1091≤n≤109) and kk (1≤k≤1001≤k≤100). Represent the number nn as the sum of kk positive integers of the same parity (have the same remainder when divided by 22).In other words, find a1,a2,…,aka1,a2,…,ak such that all ai>0ai>0, n=a1+a2+…+akn=a1+a2+…+ak and either all aiai are even or all aiai are odd at the same time.If such a representation does not exist, then report it.InputThe first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the input. Next, tt test cases are given, one per line.Each test case is two positive integers nn (1≤n≤1091≤n≤109) and kk (1≤k≤1001≤k≤100).OutputFor each test case print:YES and the required values aiai, if the answer exists (if there are several answers, print any of them);NO if the answer does not exist.The letters in the words YES and NO can be printed in any case.Exampleinput8
10 3
100 4
8 7
97 2
8 8
3 10
5 3
1000000000 9
outputYES
4 2 4
YES
55 5 5 35
NO
NO
YES
1 1 1 1 1 1 1 1
NO
YES
3 1 1
YES
111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120

思路: 把一个数分解成都是偶数或者都是奇数的和,如果能分解成
奇数,前面可以都是1,只需要判断最后一个数是否是奇数且大于0,如果都分解成偶数
只需要 判断最后一个是否是偶数且大于0

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> 
using namespace std;
long long ans[1010], ans2[1010];
int main(){
     int T;
     cin >> T;
     while(T --){
      int a, b;
      int idx = 0, edx = 0;
      scanf("%d%d", &a, &b);
      int flag1 = 0, flag2 = 0;
          for (int i = 0; i < b - 1; i ++){
         ans[idx ++] = 1;
  }
  ans[idx] = a - b + 1;
  if (ans[idx] > 0 && ans[idx] % 2 == 1)   flag1 = 1;
    edx = 0;
  for (int i = 0; i < b - 1; i ++)
  ans2[edx ++] = 2;
  ans2[edx] = a - (b - 1) * 2;
  if (ans2[edx] > 0 && ans2[edx] % 2 == 0)  flag2 = 1;
            if (!flag1 && !flag2)  puts("NO");
   else{
  puts("YES");
  if (flag1){
   for (int i = 0; i <= idx; i ++)
   if (i != idx)    printf("%lld ", ans[i]);
   else             printf("%lld\n", ans[i]);
     }
     else{
      for (int i = 0; i <= edx; i ++)
   if (i != edx)    printf("%lld ", ans2[i]);
   else             printf("%lld\n", ans2[i]);
  }
  }
  }
  return 0;
}

K-th Not Divisible by n

You are given two positive integers n and k. Print the
k-th positive integer that is not divisible by n.
For example, if n=3, and k=7, then all numbers that are no
t divisible by 3 are: 1,2,4,5,7,8,10,11,13…. The 7-th
number among them is 10.
Input
The first line contains an integer t (1≤t≤1000) — the
number of test cases in the input. Next, t test cases are
given, one per line.Each test case is two positive integers
n (2≤n≤109) and k (1≤k≤109).
Output
For each test case print the k-th positive integer that is not divisible by n.
Example
input
6
3 7
4 12
2 1000000000
7 97
1000000000 1000000000
2 1
output
10
15
1999999999
113
1000000001
1

思路: 数据范围很大,用ULL, 第y个数本来是y,现在因为一些数不能取
我们现在需要求的是删去了几个数,y + 删去数的个数 - 1就是结果,发现每x个数删去一个数,即每个
周期是x-1, Y/x-1上取整+1即为删去数的个数
把数列写在纸上找规律即可

#include <iostream>
using namespace std;
typedef unsigned long long ull;
int main()
{
 int T;
 ull x, y;
 cin >> T;
 while (T--)
 {
  cin >> x >> y;  //讨论x和y的大小关系 
  if (y > x)
  {
       cout << y + (y + x - 1) / (x - 1) - 1 << endl;
  }
  else if (x > y)        // 如果x大于y,即为y个数 
  {
   cout << y << endl;
  }
  else
   cout << y + 1 << endl;   // 如果X和y相等就x越过去 
 }
  return 0;
}

Alice, Bob and Candies
There are nn candies in a row, they are numbered from left to right from 11 to nn. The size of the ii-th candy is aiai.Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).Alice makes the first move. During the first move, she will eat 11 candy (its size is a1a1). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.For example, if n=11n=11 and a=[3,1,4,1,5,9,2,6,5,3,5]a=[3,1,4,1,5,9,2,6,5,3,5], then:move 1: Alice eats one candy of size 33 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3,5][1,4,1,5,9,2,6,5,3,5].move 2: Alice ate 33 on the previous move, which means Bob must eat 44 or more. Bob eats one candy of size 55 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3][1,4,1,5,9,2,6,5,3].move 3: Bob ate 55 on the previous move, which means Alice must eat 66 or more. Alice eats three candies with the total size of 1+4+1=61+4+1=6 and the sequence of candies becomes [5,9,2,6,5,3][5,9,2,6,5,3].move 4: Alice ate 66 on the previous move, which means Bob must eat 77 or more. Bob eats two candies with the total size of 3+5=83+5=8 and the sequence of candies becomes [5,9,2,6][5,9,2,6].move 5: Bob ate 88 on the previous move, which means Alice must eat 99 or more. Alice eats two candies with the total size of 5+9=145+9=14 and the sequence of candies becomes [2,6][2,6].move 6 (the last): Alice ate 1414 on the previous move, which means Bob must eat 1515 or more. It is impossible, so Bob eats the two remaining candies and the game ends.Print the number of moves in the game and two numbers:aa — the total size of all sweets eaten by Alice during the game;bb — the total size of all sweets eaten by Bob during the game.InputThe first line contains an integer tt (1≤t≤50001≤t≤5000) — the number of test cases in the input. The following are descriptions of the tt test cases.Each test case consists of two lines. The first line contains an integer nn (1≤n≤10001≤n≤1000) — the number of candies. The second line contains a sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — the sizes of candies in the order they are arranged from left to right.It is guaranteed that the sum of the values of nn for all sets of input data in a test does not exceed 2⋅1052⋅105.OutputFor each set of input data print three integers — the number of moves in the game and the required values aa and bb.Exampleinput7
11
3 1 4 1 5 9 2 6 5 3 5
1
1000
3
1 1 1
13
1 2 3 4 5 6 7 8 9 10 11 12 13
2
2 1
6
1 1 1 1 1 1
7
1 1 1 1 1 1 1
output6 23 21
1 1000 0
2 1 2
6 45 46
2 2 1
3 4 2
4 4 3

思路:直接模拟,拿两根指针控制

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2010;
int a[N];
int main(){
 int T;
 cin >> T;
 while(T --){
  int l, r, temp = 0, cur, A = 0, B = 0;
  int cnt = 0;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i ++)   scanf("%d", &a[i]);
  l = 1, r = n;
  int flag = 0;
  cur = a[l];
  l ++; 
  cnt ++;
  A += cur;
  flag = 1;
  while(l <= r){
    while(l <= r && a[r]  + temp <= cur){
     temp += a[r];
     r --;
    }
    if (l > r)    break;
    if (l <= r){
     cur = temp + a[r];
     temp = 0;
     cnt ++;
     B += cur;
     flag = 2;
     r --;
    }
    while(l <= r && a[l] + temp <= cur){
     temp += a[l];
     l ++;
    } 
        if (l > r)   break;
    if (l <= r){
     cur = temp + a[l];
     temp = 0;
     cnt ++;
     A += cur;
     flag = 1;
     l ++;
    }
  }
    //最后如果有剩余,也得加进去 
  if (temp)      cnt ++;
        if (flag == 1)    B += temp;
        else              A += temp;
  cout << cnt << " " << A << " " << B << endl;
 }
  return 0;
} 

后面的以后有时间再写

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值