exec_outcome stringclasses 1 value | code_uid stringlengths 32 32 | file_name stringclasses 111 values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18 values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1 value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27 values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k ⌀ | lang stringclasses 5 values | prob_desc_input_from stringclasses 3 values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k ⌀ | difficulty int64 -1 3.5k ⌀ | prob_desc_output_spec stringlengths 17 1.47k ⌀ | prob_desc_output_to stringclasses 3 values | hidden_unit_tests stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | e910918169d3e8b8c7f7fad0a9d82266 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class Main {
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static int cnt;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) {
int mod = 1000000007;
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt() + 1;
long a[] = new long[n];
for(int i=0;i<n;i++){
a[i] = (long)Math.pow(10,sc.nextInt());
}
long ans = 0;
for(int i=0;i<n-1;i++){
long x = a[i+1]/a[i]-1;
if(x>=k){
ans += a[i]*k;
k = 0;
break;
}
k -= x;
ans += a[i]*x;
}
if(k>0){
ans += a[n-1]*k;
}
out.println(ans);
}
out.close();
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 1666a902e1b55803952ff801e78818c5 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
int len=obj.nextInt();
while(len--!=0)
{
int n=obj.nextInt();
int k=obj.nextInt()+1;
long ans=0;
int[] a=new int[n];
for(int i=0;i<n;i++)a[i]=obj.nextInt();
for(int i=0;i<n-1;i++)
{
long a1=(long)(Math.pow(10,a[i+1]));
long a2=(long)(Math.pow(10,a[i]));
long val=(a1/a2)-1;
val=Math.min(k,val);
ans+=(long)a2*(long)val;
k-=val;
// System.out.println(val+" "+i+" "+val);
}
ans+=k*((long)Math.pow(10,a[n-1]));
System.out.println(ans);
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 44a851a1769bd68b7ffc0e8ce5be063a | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C {
public static void main(String[] args) throws IOException {
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(bf.readLine());
while (t --> 0) {
String[] line=bf.readLine().split(" ");
int n=Integer.parseInt(line[0]);
int k=Integer.parseInt(line[1])+1;
int[] a=new int[n];
String[] line2=bf.readLine().split(" ");
for (int i=0;i<n;i++) a[i]=Integer.parseInt(line2[i]);
int[] binsSize=new int[n];
for (int i=0;i<n-1;i++) {
binsSize[i] = ((int)Math.pow(10, a[i+1]-a[i]))-1;
}
binsSize[n-1] = Integer.MAX_VALUE;
int[] bins=new int[n];
for (int i=0;i<n;i++) {
int amnt = Math.min(binsSize[i], k);
bins[i] = amnt;
k -= amnt;
}
StringBuilder out=new StringBuilder();
for (int i=n-1;i>=0;i--) {
if (bins[i]==0) continue;
out.append(bins[i]);
}
System.out.println(out);
}
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | b5d930105ed0fd6d5d9020a810a6fea1 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
public class Q1606C {
static int mod = (int) (1e9 + 7);
static void solve() {
int n = i();
long k = l() + 1;
long ans = 0;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i();
}
for (int i = 0; i < n && k != 0; i++) {
if (i == n - 1) {
ans+=k*powerof10.get(arr[i]);
} else {
long val = (powerof10.get(arr[i + 1]) / powerof10.get(arr[i])) - 1;
long min = Math.min(val, k);
k -= min;
ans += min * powerof10.get(arr[i]);
}
// System.out.println(ans);
// System.out.println(k);
}
System.out.println(ans);
}
static ArrayList<Long> powerof10 = new ArrayList<>();
public static void main(String[] args) {
powerof10.add((long) 1);
long val = 10;
for (int i = 1; i < 19; i++) {
powerof10.add(powerof10.get(powerof10.size() - 1) * val);
}
int test = i();
while (test-- > 0) {
solve();
}
}
// ----->segmentTree--> segTree as class
// ----->lazy_Seg_tree --> lazy_Seg_tree as class
// -----> Trie --->Trie as class
// ----->fenwick_Tree ---> fenwick_Tree
// -----> POWER ---> long power(long x, long y) <----
// -----> LCM ---> long lcm(long x, long y) <----
// -----> GCD ---> long gcd(long x, long y) <----
// -----> SIEVE --> ArrayList<Integer> sieve(int N) <-----
// -----> NCR ---> long ncr(int n, int r) <----
// -----> BINARY_SEARCH ---> int binary_Search(long[]arr,long x) <----
// -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<--
// ----> DFS ---> void dfs(ArrayList<ArrayList<Integer>>edges,int child,int
// parent)<---
// ---> NODETOROOT --> ArrayList<Integer>
// node2Root(ArrayList<ArrayList<Integer>>edges,int child,int parent,int tofind)
// <--
// ---> LEVELS_TREE -->levels_Trees(ArrayList<HashSet<Integer>> edges, int
// child, int parent,int[]level,int currLevel) <--
// ---> K_THPARENT --> int k_thparent(int node,int[][]parent,int k) <---
// ---> TWOPOWERITHPARENTFILL --> void twoPowerIthParentFill(int[][]parent) <---
// -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<-
// tempstart
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static InputReader in = new InputReader(System.in);
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | e211410e0790aefa36765f674a284296 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.TreeSet;
// public class Solution{
class Solution{
public static void main(String[] args){
FS sc = new FS();
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
// int t = 1;
while(t-->0){
long n = sc.nextLong();
long k = sc.nextLong();
long[] a = sc.nextLongArray((int)n);
k++;
for (int i=0; i<n; i++) {
a[i] = (long)(Math.pow(10, a[i]));
}
// pw.println(Arrays.toString(a));
long ans = 0;
for (int i=0; i<n-1; i++) {
long curr = (a[i+1]/a[i]) - 1;
curr = Math.min(curr, k);
ans += a[i]*curr;
k -= curr;
}
if (k>0) {
ans += a[(int)(n)-1]*k;
}
pw.println(ans);
}
pw.flush();
pw.close();
}
static class FS{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next(){
while(!st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
} catch(Exception ignored){
}
}
return st.nextToken();
}
int[] nextArray(int n){
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(int n){
long[] a = new long[n];
for(int i = 0; i < n; i++){
a[i] = nextLong();
}
return a;
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 0ccedc0cd3089eef3bd111ea726facc6 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | //package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
public class Rough_Work {
public static void main(String[] args) throws IOException {
new Rough_Work().TestCases();
}
void TestCases() throws IOException{
sieveOfEratosthenes(1000000);
int t = nextInt();
while (t-->0) {
solve();
}
out.flush();
} // Put t = 1, for NO testcases // out.flush() is written in this method
void solve () throws IOException {
int n = nextInt();
int k = nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = (long) Math.pow(10,nextInt());
long ans = 0;
for (int i = 1; i < n; i++) {
long diff = arr[i]/arr[i-1];
diff--;
if (i == 1) diff--;
if (k > diff) {
k -= diff;
ans += diff*arr[i-1];
}
else {
ans += k*arr[i-1];
k = 0;
break;
}
}
if (k > 0) ans += k*arr[n-1];
out.println(ans+1);
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st;
void printIntArray (int[] arr) {
for (int z : arr) out.print(z+" ");
out.println();
}
void printIntList (ArrayList<Integer> arr) {
for (int z : arr) out.print(z+" ");
out.println();
}
void printLongArray (long[] arr) {
for (long z : arr) out.print(z+" ");
out.println();
}
void printLongList (ArrayList<Long> arr) {
for (long z : arr) out.print(z+" ");
out.println();
}
String next () throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
int nextInt () throws IOException {
return Integer.parseInt(next());
}
int[] nextIntArray (int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = nextInt();
return arr;
}
long nextLong () throws IOException {
return Long.parseLong(next());
}
long[] nextLongArray (int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = nextLong();
return arr;
}
ArrayList<Long> nextLongList (int n) throws IOException {
ArrayList<Long> lis = new ArrayList<>(n);
for (int i = 0; i < n; i++) lis.add(nextLong());
return lis;
}
double nextDouble () throws IOException {
return Double.parseDouble(next());
}
char nextChar () throws IOException {
return next().charAt(0);
}
String nextLine () throws IOException {
return br.readLine().trim();
}
boolean is_Sorted_int (int[] arr) {
for (int i = 0; i < arr.length-1; i++) if (arr[i] > arr[i+1]) return false;
return true;
}
boolean is_Sorted_long (long[] arr) {
for (int i = 0; i < arr.length-1; i++) if (arr[i] > arr[i+1]) return false;
return true;
}
long gcd(long a, long b) { return (b==0?a:gcd(b,a%b)); }
long lcm(long a, long b) { return (a / gcd(a, b)) * b; }
ArrayList<Integer> Factors(int n) {
ArrayList<Integer> ret = new ArrayList<>();
for (int i=1; i<=Math.sqrt(n); i++) {
if (n%i==0) {
// If divisors are equal, print only one
if (n/i != i) ret.add(n/i);
ret.add(i);
}
}
return ret;
}
boolean check_Integer (double a) {return Math.ceil(a) == Math.floor(a);}
static class Pair implements Comparable<Pair> {
long f;
long s;
public Pair (long f, long s) {
this.f = f;
this.s = s;
}
@Override
public int compareTo(Pair o) {
if (this.f == o.f) return Long.compare(this.s,o.s);
else return Long.compare(this.f,o.f);
}
} // Comparable on basis of first then second.
static class Triplet {
long f;
long s;
long t;
public Triplet (long f, long s, long t) {
this.f = f;
this.s = s;
this.t = t;
}
} // Implement comparable accordingly.
long mod = 1_000_000_007;
long mod_Multiply(long a,long b) {
return (a*b)%mod;
}
long mod_factorial (long n) {
long fact =1;
for(int i=1; i<=n; i++) {
fact = mod_Multiply(fact,i);
}
return fact%mod;
}
long mod_power(long x, long y) {
long temp;
if (y == 0)
return 1;
temp = mod_power(x, y / 2);
if (y % 2 == 0) return mod_Multiply(temp,temp);
else {
if (y > 0) return mod_Multiply(x,mod_Multiply(temp,temp));
else return (mod_Multiply(temp,temp)) / x;
}
}
void Print_all_subsequence (int i, int[] x) {
if (i >= x.length) {
printIntArray(x);
return;
}
for (int j = i; j < x.length; j++) {
XOR_Swap(i,j,x);
Print_all_subsequence(i+1,x);
XOR_Swap(i,j,x);
}
}
void XOR_Swap (int i, int j, int[] x) {
if (i == j) return;
x[i] = x[i]^x[j];
x[j] = x[i]^x[j];
x[i] = x[i]^x[j];
} // works only for numbers..
boolean[] prime;
void sieveOfEratosthenes(int n) {
prime = new boolean[n + 1];
Arrays.fill(prime,true);
prime[0] = prime[1] = false;
//for (int i = 2; i <= n; i++) prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p])
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
} // Gives prime numbers less than equal to n in boolean[] array prime.
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | df8777409573e4d94062b58aa0d8504a | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | // import static java.lang.System.out;
import static java.lang.Math.*;
import static java.lang.System.*;
import java.lang.reflect.Array;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
static FastReader sc;
static long mod = ((long) 1e9) + 7;
static long lmax=Long.MAX_VALUE;
static long lmin=Long.MIN_VALUE;
static int imax=Integer.MAX_VALUE;
static int imin=Integer.MIN_VALUE;
// static FastWriter out;
static FastWriter out;
public static void main(String hi[]) throws IOException {
// initializeIO();
out = new FastWriter();
sc = new FastReader();
long startTimeProg = System.currentTimeMillis();
long endTimeProg = Long.MAX_VALUE;
int t = sc.nextInt();
// int t=1;
// boolean[] seave=sieveOfEratosthenes((int)(1e5));
// int[] seave2=sieveOfEratosthenesInt((long)sqrt(1e9));
// debug(seave2);
while (t-- != 0) {
int n=sc.nextInt();
long k=sc.nextLong();
int[] arr=readIntArray(n);
long ans=0;
k++;
for(int i=0;i<n-1;i++){
long can=(power(10,arr[i+1])/power(10,arr[i]))-1;
long v=min(can,k);
ans+=power(10,arr[i])*v;
k-=v;
}
print(ans+(power(10,arr[n-1])*k));
}
endTimeProg = System.currentTimeMillis();
debug("[finished : " + (endTimeProg - startTimeProg) + ".ms ]");
// System.out.println(String.format("%.9f", max));
}
private static long find(int[] arr, long k) {
int n=arr.length;
long s=0;
for(int i=n-1;i>=0;i--){
long v=power(10,arr[i]);
s+=k/v;
k=k%v;
}
return s;
}
private static boolean isPeak(int[] arr,int i,int l,int r){
if(i==l||i==r)return false;
return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]);
}
private static boolean isPeak(long[] arr,int i,int l,int r){
if(i==l||i==r)return false;
return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]);
}
private static long kadens(List<Long> li, int l, int r) {
long max = Long.MIN_VALUE;
long ans = 0;
for (int i = l; i <= r; i++) {
ans += li.get(i);
max = max(ans, max);
if (ans < 0) ans = 0;
}
return max;
}
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
private static boolean isSorted(List<Integer> li) {
int n = li.size();
if (n <= 1) return true;
for (int i = 0; i < n - 1; i++) {
if (li.get(i) > li.get(i + 1)) return false;
}
return true;
}
static boolean isPowerOfTwo(long x) {
return x != 0 && ((x & (x - 1)) == 0);
}
private static boolean ispallindromeList(List<Integer> res) {
int l = 0, r = res.size() - 1;
while (l < r) {
if (res.get(l) != res.get(r)) return false;
l++;
r--;
}
return true;
}
private static class Pair {
int first = 0;
int sec = 0;
int[] arr;
char ch;
String s;
Map<Integer, Integer> map;
Pair(int first, int sec) {
this.first = first;
this.sec = sec;
}
Pair(int[] arr) {
this.map = new HashMap<>();
for (int x : arr) this.map.put(x, map.getOrDefault(x, 0) + 1);
this.arr = arr;
}
Pair(char ch, int first) {
this.ch = ch;
this.first = first;
}
Pair(String s, int first) {
this.s = s;
this.first = first;
}
}
private static int sizeOfSubstring(int st, int e) {
int s = e - st + 1;
return (s * (s + 1)) / 2;
}
private static Set<Long> factors(long n) {
Set<Long> res = new HashSet<>();
// res.add(n);
for (long i = 1; i * i <= (n); i++) {
if (n % i == 0) {
res.add(i);
if (n / i != i) {
res.add(n / i);
}
}
}
return res;
}
private static long fact(long n) {
if (n <= 2) return n;
return n * fact(n - 1);
}
private static long ncr(long n, long r) {
return fact(n) / (fact(r) * fact(n - r));
}
private static int lessThen(long[] nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = 0;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums[mid] <= val) {
i = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return i;
}
private static int lessThen(List<Long> nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = 0;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) <= val) {
i = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return i;
}
private static int lessThen(List<Integer> nums, int val, boolean work, int l, int r) {
int i = -1;
if (work) i = 0;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) <= val) {
i = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return i;
}
private static int greaterThen(List<Long> nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = r;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) >= val) {
i = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return i;
}
private static int greaterThen(List<Integer> nums, int val, boolean work) {
int i = -1, l = 0, r = nums.size() - 1;
if (work) i = r;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums.get(mid) >= val) {
i = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return i;
}
private static int greaterThen(long[] nums, long val, boolean work, int l, int r) {
int i = -1;
if (work) i = r;
while (l <= r) {
int mid = l + ((r - l) / 2);
if (nums[mid] >= val) {
i = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return i;
}
private static long gcd(long[] arr) {
long ans = 0;
for (long x : arr) {
ans = gcd(x, ans);
}
return ans;
}
private static int gcd(int[] arr) {
int ans = 0;
for (int x : arr) {
ans = gcd(x, ans);
}
return ans;
}
private static long sumOfAp(long a, long n, long d) {
long val = (n * (2 * a + ((n - 1) * d)));
return val / 2;
}
//geometrics
private static double areaOftriangle(double x1, double y1, double x2, double y2, double x3, double y3) {
double[] mid_point = midOfaLine(x1, y1, x2, y2);
// debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3);
double height = distanceBetweenPoints(mid_point[0], mid_point[1], x3, y3);
double wight = distanceBetweenPoints(x1, y1, x2, y2);
// debug(height+" "+wight);
return (height * wight) / 2;
}
private static double distanceBetweenPoints(double x1, double y1, double x2, double y2) {
double x = x2 - x1;
double y = y2 - y1;
return (Math.pow(x, 2) + Math.pow(y, 2));
}
public static boolean isPerfectSquareByUsingSqrt(long n) {
if (n <= 0) {
return false;
}
double squareRoot = Math.sqrt(n);
long tst = (long) (squareRoot + 0.5);
return tst * tst == n;
}
private static double[] midOfaLine(double x1, double y1, double x2, double y2) {
double[] mid = new double[2];
mid[0] = (x1 + x2) / 2;
mid[1] = (y1 + y2) / 2;
return mid;
}
private static long sumOfN(long n) {
return (n * (n + 1)) / 2;
}
private static long power(long x, long y, long p) {
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
/* Function to calculate x raised to the power y in O(logn)*/
static long power(long x, long y) {
long temp;
if (y == 0)
return 1l;
temp = power(x, y / 2);
if (y % 2 == 0)
return (temp * temp);
else
return (x * temp * temp);
}
private static StringBuilder reverseString(String s) {
StringBuilder sb = new StringBuilder(s);
int l = 0, r = sb.length() - 1;
while (l <= r) {
char ch = sb.charAt(l);
sb.setCharAt(l, sb.charAt(r));
sb.setCharAt(r, ch);
l++;
r--;
}
return sb;
}
private static void swap(List<Integer> li, int i, int j) {
int t = li.get(i);
li.set(i, li.get(j));
li.set(j, t);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static String decimalToString(int x) {
return Integer.toBinaryString(x);
}
private static String decimalToString(long x) {
return Long.toBinaryString(x);
}
private static boolean isPallindrome(String s, int l, int r) {
while (l < r) {
if (s.charAt(l) != s.charAt(r)) return false;
l++;
r--;
}
return true;
}
private static boolean isSubsequence(String s, String t) {
if (s == null || t == null) return false;
Map<Character, List<Integer>> map = new HashMap<>(); //<character, index>
//preprocess t
for (int i = 0; i < t.length(); i++) {
char curr = t.charAt(i);
if (!map.containsKey(curr)) {
map.put(curr, new ArrayList<Integer>());
}
map.get(curr).add(i);
}
int prev = -1; //index of previous character
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.get(c) == null) {
return false;
} else {
List<Integer> list = map.get(c);
prev = lessThen(list, prev, false, 0, list.size() - 1);
if (prev == -1) {
return false;
}
prev++;
}
}
return true;
}
private static StringBuilder removeLeadingZero(StringBuilder sb) {
int i = 0;
while (i < sb.length() && sb.charAt(i) == '0') i++;
// debug("remove "+i);
if (i == sb.length()) return new StringBuilder();
return new StringBuilder(sb.substring(i, sb.length()));
}
private static StringBuilder removeEndingZero(StringBuilder sb) {
int i = sb.length() - 1;
while (i >= 0 && sb.charAt(i) == '0') i--;
// debug("remove "+i);
if (i < 0) return new StringBuilder();
return new StringBuilder(sb.substring(0, i + 1));
}
private static int stringToDecimal(String binaryString) {
// debug(decimalToString(n<<1));
return Integer.parseInt(binaryString, 2);
}
private static int stringToInt(String s) {
return Integer.parseInt(s);
}
private static String toString(long val) {
return String.valueOf(val);
}
private static void debug(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(long[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(List<int[]> arr) {
for (int[] a : arr) {
err.println(Arrays.toString(a));
}
}
private static void debug(float[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(double[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void debug(boolean[][] arr) {
for (int i = 0; i < arr.length; i++) {
err.println(Arrays.toString(arr[i]));
}
}
private static void print(String s) throws IOException {
out.println(s);
}
private static void debug(String s) throws IOException {
err.println(s);
}
private static int charToIntS(char c) {
return ((((int) (c - '0')) % 48));
}
private static void print(double s) throws IOException {
out.println(s);
}
private static void debug(char[] s1) throws IOException {
debug(Arrays.toString(s1));
}
private static void print(float s) throws IOException {
out.println(s);
}
private static void print(long s) throws IOException {
out.println(s);
}
private static void print(int s) throws IOException {
out.println(s);
}
private static void debug(double s) throws IOException {
err.println(s);
}
private static void debug(float s) throws IOException {
err.println(s);
}
private static void debug(long s) {
err.println(s);
}
private static void debug(int s) {
err.println(s);
}
private static boolean isPrime(long n) {
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return false;
}
return true;
}
private static List<List<Integer>> readUndirectedGraph(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readUndirectedGraph(int[][] intervals, int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < intervals.length; i++) {
int x = intervals[i][0];
int y = intervals[i][1];
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int[][] intervals, int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < intervals.length; i++) {
int x = intervals[i][0];
int y = intervals[i][1];
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
static String[] readStringArray(int n) {
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.next();
}
return arr;
}
private static Map<Character, Integer> freq(String s) {
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
return map;
}
private static Map<Long, Integer> freq(long[] arr) {
Map<Long, Integer> map = new HashMap<>();
for (long x : arr) {
map.put(x, map.getOrDefault(x, 0) + 1);
}
return map;
}
private static Map<Integer, Integer> freq(int[] arr) {
Map<Integer, Integer> map = new HashMap<>();
for (int x : arr) {
map.put(x, map.getOrDefault(x, 0) + 1);
}
return map;
}
static boolean[] sieveOfEratosthenes(long n) {
boolean prime[] = new boolean[(int) n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
static int[] sieveOfEratosthenesInt(long n) {
boolean prime[] = new boolean[(int) n + 1];
Set<Integer> li = new HashSet<>();
for (int i = 1; i <= n; i++) {
prime[i] = true;
li.add(i);
}
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) {
li.remove(i);
prime[i] = false;
}
}
}
int[] arr = new int[li.size()];
int i = 0;
for (int x : li) {
arr[i++] = x;
}
return arr;
}
static boolean isMemberAC(int a, int d, int x) {
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
static boolean isMemberAC(long a, long d, long x) {
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> li = new ArrayList<>();
for (int x : arr) {
li.add(x);
}
Collections.sort(li);
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sortReverse(int[] arr) {
int n = arr.length;
List<Integer> li = new ArrayList<>();
for (int x : arr) {
li.add(x);
}
Collections.sort(li, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sort(double[] arr) {
int n = arr.length;
List<Double> li = new ArrayList<>();
for (double x : arr) {
li.add(x);
}
Collections.sort(li);
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sortReverse(double[] arr) {
int n = arr.length;
List<Double> li = new ArrayList<>();
for (double x : arr) {
li.add(x);
}
Collections.sort(li, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sortReverse(long[] arr) {
int n = arr.length;
List<Long> li = new ArrayList<>();
for (long x : arr) {
li.add(x);
}
Collections.sort(li, Collections.reverseOrder());
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> li = new ArrayList<>();
for (long x : arr) {
li.add(x);
}
Collections.sort(li);
for (int i = 0; i < n; i++) {
arr[i] = li.get(i);
}
}
private static long sum(int[] arr) {
long sum = 0;
for (int x : arr) {
sum += x;
}
return sum;
}
private static long sum(long[] arr) {
long sum = 0;
for (long x : arr) {
sum += x;
}
return sum;
}
private static void initializeIO() {
try {
System.setIn(new FileInputStream("input"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
System.setErr(new PrintStream(new FileOutputStream("error.txt")));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
private static int maxOfArray(int[] arr) {
int max = Integer.MIN_VALUE;
for (int x : arr) {
max = Math.max(max, x);
}
return max;
}
private static long maxOfArray(long[] arr) {
long max = Long.MIN_VALUE;
for (long x : arr) {
max = Math.max(max, x);
}
return max;
}
private static int[][] readIntIntervals(int n, int m) {
int[][] arr = new int[n][m];
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
arr[j][i] = sc.nextInt();
}
}
return arr;
}
private static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
private static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
private static int[] readIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
return arr;
}
private static double[] readDoubleArray(int n) {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextDouble();
}
return arr;
}
private static long[] readLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
return arr;
}
private static void debug(String[] arr) {
err.println(Arrays.toString(arr));
}
private static void debug(Boolean[][] arr) {
for (int i = 0; i < arr.length; i++) err.println(Arrays.toString(arr[i]));
}
private static void debug(int[] arr) {
err.println(Arrays.toString(arr));
}
private static void debug(long[] arr) {
err.println(Arrays.toString(arr));
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
BufferedWriter bw;
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
FastWriter() {
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
<T> void print(T obj) throws IOException {
bw.write(obj.toString());
bw.flush();
}
void println() throws IOException {
print("\n");
}
<T> void println(T obj) throws IOException {
print(obj.toString() + "\n");
}
void print(int[] arr) throws IOException {
for (int x : arr) {
print(x + " ");
}
println();
}
void print(long[] arr) throws IOException {
for (long x : arr) {
print(x + " ");
}
println();
}
void print(double[] arr) throws IOException {
for (double x : arr) {
print(x + " ");
}
println();
}
void printCharN(char c, int n) throws IOException {
for (int i = 0; i < n; i++) {
print(c);
}
}
}
static class Dsu {
int[] parent, size;
Dsu(int n) {
parent = new int[n + 1];
size = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
private int findParent(int u) {
if (parent[u] == u) return u;
return parent[u] = findParent(parent[u]);
}
private boolean union(int u, int v) {
// System.out.println("uf "+u+" "+v);
int pu = findParent(u);
// System.out.println("uf2 "+pu+" "+v);
int pv = findParent(v);
// System.out.println("uf3 " + u + " " + pv);
if (pu == pv) return false;
if (size[pu] <= size[pv]) {
parent[pu] = pv;
size[pv] += size[pu];
} else {
parent[pv] = pu;
size[pu] += size[pv];
}
return true;
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 7ed52cec535f0a532be8758eef7d0cb7 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes |
import java.io.*;
import java.util.*;
public class Solution{
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static class Pair{
int a,b;
Pair(int x, int y) {
a=x;
b=y;
}
}
static long mod = 998244353;
static StringBuffer sb = new StringBuffer("");
static int ans=0;
public static void main(String[] args) throws Exception
{
//Read input from user
//Scanner scn = new Scanner(System.in);
FastReader scn = new FastReader();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t = scn.nextInt();
while(t>0) {
int n = scn.nextInt();
long k = scn.nextLong();
long[] a= new long[n];
for(int i=0;i<n;i++) {
a[i] = (long)(Math.pow(10,scn.nextLong()));
//System.out.println(i+" "+a[i]);
}
int i=0;
long sum = 0;
long amt=0;
while(i<n-1) {
if(sum+((a[i+1]-a[i])/a[i])<=k) {
sum=sum+ ((a[i+1]-a[i])/a[i]);
amt = amt+(a[i+1]-a[i]);
}else {
//System.out.println("rem "+(k+1-sum)+" "+a[i]);
amt = amt +a[i]*(k+1-sum);
sum = sum+(k+1-sum);
// System.out.println(amt);
break;
}
// System.out.println(i+" "+sum);
i++;
}
if(sum<=k) {
// System.out.println("here");
amt = amt +a[i]*(k+1-sum);
sum = sum+(k+1-sum);
}
output.write(amt+"\n");
t--;
output.flush();
}
}
public static int findpow2(long n) {
int c=0;
while(n>0 && n%2==0) {
n = n/2;
c++;
}
return c;
}
public static void countLeaves(int curr, HashMap<Integer, List<Integer>> m1, List<Integer> p, List<List<Integer>> f) {
if(!m1.containsKey(curr)) {
f.add(new ArrayList<>(p));
return;
}
for(int next: m1.get(curr)) {
p.add(next);
countLeaves(next,m1,p,f);
p = new ArrayList<>();
}
}
public static long canmake(long v) {
long r =2;
long k = 2*2*2;
long x = 0;
while(v>=k) {
x = x+(v/k);
r++;
k = r*r*r;
}
return x;
}
public static int dfs(int curr, HashMap<Integer, List<Integer>> m1, int[] v) {
if(!m1.containsKey(curr)) {
return v[curr];
}
int val = v[curr];
for(int next: m1.get(curr)) {
val = val+dfs(next,m1,v);
}
// System.out.println("val "+curr+" "+val);
if(val==0) {
ans++;
}
return val;
}
public static long fact(long n) {
if(n<=1) {
return 1L;
}
return ((fact(n-1)%mod)*n)%mod;
}
public static long gcd(long a,long b){
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long lcm( long a,long b)
{
long g = gcd(a,b);
return (a*b)/g;
}
public static void find(long a, List<Long> div) {
for(long i=1;i<=Math.sqrt(a);i++) {
if(a%i==0) {
div.add(i);
if((a/i)!=i) {
div.add(a/i);
}
}
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 067bbf874f097637ab52405143745fa8 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private FastWriter wr;
private Reader rd;
public final int MOD = 1000000007;
/************************************************** FAST INPUT IMPLEMENTATION *********************************************/
class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
public Reader(String path) throws FileNotFoundException {
br = new BufferedReader(
new InputStreamReader(new FileInputStream(path)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public int ni() throws IOException {
return rd.nextInt();
}
public long nl() throws IOException {
return rd.nextLong();
}
public void yOrn(boolean flag){
if(flag){
wr.println("YES");
}else {
wr.println("NO");
}
}
char nc() throws IOException {
return rd.next().charAt(0);
}
public String ns() throws IOException {
return rd.nextLine();
}
public Double nd() throws IOException {
return rd.nextDouble();
}
public int[] nai(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
public long[] nal(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
/************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter() {
out = null;
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastWriter write(char c) {
return write((byte) c);
}
public FastWriter write(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastWriter write(int x) {
if (x == Integer.MIN_VALUE) {
return write((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter write(double x, int precision) {
if (x < 0) {
write('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
write((long) x).write(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
write((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastWriter writeln(char c) {
return write(c).writeln();
}
public FastWriter writeln(int x) {
return write(x).writeln();
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln(double x, int precision) {
return write(x, precision).writeln();
}
public FastWriter write(int... xs) {
boolean first = true;
for (int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs) {
boolean first = true;
for (long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln() {
return write((byte) '\n');
}
public FastWriter writeln(int... xs) {
return write(xs).writeln();
}
public FastWriter writeln(long... xs) {
return write(xs).writeln();
}
public FastWriter writeln(char[] line) {
return write(line).writeln();
}
public FastWriter writeln(char[]... map) {
for (char[] line : map) write(line).writeln();
return this;
}
public FastWriter writeln(String s) {
return write(s).writeln();
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter print(byte b) {
return write(b);
}
public FastWriter print(char c) {
return write(c);
}
public FastWriter print(char[] s) {
return write(s);
}
public FastWriter print(String s) {
return write(s);
}
public FastWriter print(int x) {
return write(x);
}
public FastWriter print(long x) {
return write(x);
}
public FastWriter print(double x, int precision) {
return write(x, precision);
}
public FastWriter println(char c) {
return writeln(c);
}
public FastWriter println(int x) {
return writeln(x);
}
public FastWriter println(long x) {
return writeln(x);
}
public FastWriter println(double x, int precision) {
return writeln(x, precision);
}
public FastWriter print(int... xs) {
return write(xs);
}
public FastWriter print(long... xs) {
return write(xs);
}
public FastWriter println(int... xs) {
return writeln(xs);
}
public FastWriter println(long... xs) {
return writeln(xs);
}
public FastWriter println(char[] line) {
return writeln(line);
}
public FastWriter println(char[]... map) {
return writeln(map);
}
public FastWriter println(String s) {
return writeln(s);
}
public FastWriter println() {
return writeln();
}
}
/********************************************************* USEFUL CODE **************************************************/
boolean[] SAPrimeGenerator(int n) {
// TC-N*LOG(LOG N)
//Create Prime Marking Array and fill it with true value
boolean[] primeMarker = new boolean[n + 1];
Arrays.fill(primeMarker, true);
primeMarker[0] = false;
primeMarker[1] = false;
for (int i = 2; i <= n; i++) {
if (primeMarker[i]) {
// we start from 2*i because i*1 must be prime
for (int j = 2 * i; j <= n; j += i) {
primeMarker[j] = false;
}
}
}
return primeMarker;
}
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
class Pair<F, S> {
private F first;
private S second;
Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<F, S> pair = (Pair<F, S>) o;
return first == pair.first && second == pair.second;
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
}
class PairThree<F, S, X> {
private F first;
private S second;
private X third;
PairThree(F first, S second,X third) {
this.first = first;
this.second = second;
this.third=third;
}
public F getFirst() {
return first;
}
public void setFirst(F first) {
this.first = first;
}
public S getSecond() {
return second;
}
public void setSecond(S second) {
this.second = second;
}
public X getThird() {
return third;
}
public void setThird(X third) {
this.third = third;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PairThree<?, ?, ?> pair = (PairThree<?, ?, ?>) o;
return first.equals(pair.first) && second.equals(pair.second) && third.equals(pair.third);
}
@Override
public int hashCode() {
return Objects.hash(first, second, third);
}
}
public static void main(String[] args) throws IOException {
new Main().run();
}
public void run() throws IOException {
if (oj) {
rd = new Reader();
wr = new FastWriter(System.out);
} else {
File input = new File("input.txt");
File output = new File("output.txt");
if (input.exists() && output.exists()) {
rd = new Reader(input.getPath());
wr = new FastWriter(output.getPath());
} else {
rd = new Reader();
wr = new FastWriter(System.out);
oj = true;
}
}
long s = System.currentTimeMillis();
solve();
wr.flush();
tr(System.currentTimeMillis() - s + "ms");
}
/***************************************************************************************************************************
*********************************************************** MAIN CODE ******************************************************
****************************************************************************************************************************/
boolean[] sieve;
public void solve() throws IOException {
int t = 1;
t = ni();
while (t-- > 0) {
go();
}
}
/********************************************************* MAIN LOGIC HERE ****************************************************/
long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lower_bound(long array[], long key)
{
int low = 0, high = array.length;
int mid;
while (low < high) {
mid = low + (high - low) / 2;
if (key <= array[mid]) {
high = mid;
}
else {
low = mid + 1;
}
}
if (low < array.length && array[low] < key) {
low++;
}
return low;
}
public boolean getHealth(long[] arr,long k,long h){
long health=0;
health+=k;
for(int i=1;i<arr.length;i++){
if(arr[i]-arr[i-1]<=k){
health+=arr[i]-arr[i-1];
}else {
health+=k;
}
}
return health>=h;
}
public int Substr(String s2, String s1){
int counter = 0; //pointing s2
int i = 0;
for(;i<s1.length();i++){
if(counter==s2.length())
break;
if(s2.charAt(counter)==s1.charAt(i)){
counter++;
}else{
//Special case where character preceding the i'th character is duplicate
if(counter>0){
i -= counter;
}
counter = 0;
}
}
return counter < s2.length()?-1:i-counter;
}
public void go() throws IOException {
int n=ni();
long k=nl();
long[] arr=nal(n);
k++;
long ans=0;
for(int i=0;i<n-1 && k>0;i++){
long cnt=arr[i+1]-arr[i];
long val=(long)Math.min(Math.pow(10,cnt)-1,k);
ans+=Math.pow(10,arr[i])*val;
k-=val;
}
// System.out.println(k);
ans+=((long)Math.pow(10,arr[n-1]))*k;
wr.println(ans);
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 1a00dc2be8234dcff27c8ad4c13bea60 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.BufferedReader;
import java.io.*;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.*;
public class Yoo
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader sc=new FastReader();
int i,j=0;
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
long k =sc.nextLong();
long a[]=new long[n];
for(i=0;i<n;i++)
a[i]=(long)Math.pow(10,sc.nextInt());
i=0;
long b[]=new long[n];
for(i=0;i<n;i++)
b[i]=0;
i=0;
k++;
while(k>0 && i<n-1)
{
long notes=(a[i+1]/a[i])-1;
//System.out.println(notes+"notes "+i);
if(notes>k)
{
b[i]=k;
k=k-notes;
// System.out.println(k+"notes pp "+i);
break;
}
else
{
b[i]=notes;
k=k-notes;
}
i++;
}
if(k>0)
{
b[n-1]=k;
//System.out.println(k+"hgjk");
}
long sum = 0;
/*for(i=0;i<n;i++)
System.out.println(b[i]+" ");
System.out.println();*/
for(i=0;i<n;i++)
{
sum+=b[i]*a[i];
}
System.out.println(sum);
/*System.out.println("jjjjk");
System.out.println();
System.out.println();
*/
}
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 9b184d197748ad0790cec29cf158eb35 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | //package codeforces;
import java.util.*;
public class BankNotes {
public static void main(String [] args)
{
Scanner in =new Scanner (System.in);
int t=in.nextInt();
while(t>0)
{
int n=in.nextInt();
long k=in.nextLong();
long ar[]= new long [n];
long min[]= new long [n];
for(int i=0;i<n;i++)
{
ar[i]=in.nextLong();
ar[i]=(int)Math.pow(10,ar[i]);
// System.out.println(ar[i]);
}
//n=n+1;
for(int i=0;i<n-1;i++)
{
min[i]=(ar[i+1]/ar[i])-1;
// System.out.println(min[i]);
}
k++;
long ans=0;
for(int i=0;i<n&&k>0;i++)
{
//long x=k;
k=k-min[i];
if(k>0)
{
ans+=(min[i]*ar[i]);
}
else {
ans+=((k+min[i])*ar[i]);
}
if(i==n-1)
{
//System.out.println("yes");
ans+=((k*ar[i]));
}
//System.out.println(ans+"k "+k);
}
System.out.println(ans);
t--;
}
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 98e88fb0e1011a6d8b9b174c7d782e5e | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.lang.Exception;
public class Main{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null||!st.hasMoreElements()){
try{
st=new StringTokenizer(br.readLine());
}catch(IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){return Integer.parseInt(next());}
long nextLong(){return Long.parseLong(next());}
double nextDouble(){return Double.parseDouble(next());}
String nextLine(){
String str="";
try{
if(st.hasMoreTokens()) str=st.nextToken("\n");
else str=br.readLine();
}catch(IOException e){
e.printStackTrace();
}
return str;
}
}
static FastReader sc=new FastReader();
static int power(int a,int b){
int result=1;
while(b>0){
if((b&1)==1) result*=a;
a*=a;
b/=2;
}
return result;
}
static void solve(){
int n=sc.nextInt();
int k=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
if(n==1){
System.out.println((long)(k+1)*power(10,a[0]));
return;
}
int sum=power(10,a[1]-a[0])-1;
int m=k+1;
long num=0;
for(int i=0;i<n-1;i++){
if(m>sum){
if(i==n-2){
num+=power(10,a[i+1])-power(10,a[i]);
num+=(long)(m-sum)*power(10,a[i+1]);
System.out.println(num);
return;
}else{
num+=power(10,a[i+1])-power(10,a[i]);
sum+=power(10,a[i+2]-a[i+1])-1;
}
}else if(m==sum){
num+=power(10,a[i+1])-power(10,a[i]);
System.out.println(num);
return;
}else{
sum-=power(10,a[i+1]-a[i])-1;
num+=(long)(m-sum)*power(10,a[i]);
System.out.println(num);
return;
}
}
}
public static void main(String args[])throws java.lang.Exception{
int t=sc.nextInt();
while(t-->0){
solve();
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 9a7950e19795c68905e285770e325c0e | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | /*
*** author: cypher70
*/
import java.io.*;
import java.util.*;
import javax.management.Query;
import javax.sound.sampled.SourceDataLine;
public class sol {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
}
static class pair<E, T, K> {
E val1;
T val2;
K val3;
public pair(E d1, T d2, K d3) {
val1 = d1;
val2 = d2;
val3 = d3;
}
}
static String reverse(String s) {
StringBuilder rev = new StringBuilder();
for (int i = s.length()-1; i >= 0; i--) {
rev.append(s.charAt(i));
}
return rev.toString();
}
static void reverse(int[] arr, int l, int r) {
while (l < r) {
int h = arr[l];
arr[l] = arr[r];
arr[r] = h;
l++; r--;
}
}
static void debug(int[] arr) {
for (int e: arr) {
System.out.print(e + " ");
}
System.out.println();
}
static void sort(int[] arr, int l, int r) {
ArrayList<Integer> a = new ArrayList<>();
for (int i = l; i <= r; i++) {
a.add(arr[i]);
}
Collections.sort(a);
for (int i = l; i <= r; i++) {
arr[i] = a.get(i-l);
}
}
/*
------------------------------- Implementation Begins ------------------------------
*/
public static void main(String[] args) {
FastReader f = new FastReader();
int t = f.nextInt();
while (t-->0) {
int n = f.nextInt();
int k = f.nextInt();
++k;
int[] c = new int[n];
for (int i = 0; i < n; i++) c[i] = f.nextInt();
int[] s = new int[n];
for (int i = 0; i < n; i++) {
if (i == n-1) {
s[i] = k;
} else {
int m = 0, y = c[i+1] - c[i];
while (y-->0) m = (10 * m + 9);
if (k <= m) {
s[i] = k;
break;
} else {
s[i] = m;
k -= m;
}
}
}
long ans = 0;
for (int i = 0; i < n; i++) {
ans += (long)Math.pow(10, c[i]) * s[i];
}
System.out.println(ans);
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 5bc2292713f4b663a65ee145ea938721 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.lang.Math;
import java.lang.reflect.Array;
import java.util.*;
import javax.swing.text.DefaultStyledDocument.ElementSpec;
public final class Solution {
static BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
static BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(System.out)
);
static StringTokenizer st;
/*write your constructor and global variables here*/
static class sortCond implements Comparator<Pair<Integer, Integer>> {
@Override
public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
if (p1.a <= p2.a) {
return -1;
} else {
return 1;
}
}
}
static class Rec {
int a;
int b;
long c;
Rec(int a, int b, long c) {
this.a = a;
this.b = b;
this.c = c;
}
}
static class Pair<f, s> {
f a;
s b;
Pair(f a, s b) {
this.a = a;
this.b = b;
}
}
interface modOperations {
int mod(int a, int b, int mod);
}
static int findBinaryExponentian(int a, int pow, int mod) {
if (pow == 1) {
return a;
} else if (pow == 0) {
return 1;
} else {
int retVal = findBinaryExponentian(a, (int) pow / 2, mod);
int val = (pow % 2 == 0) ? 1 : a;
return modMul.mod(modMul.mod(retVal, retVal, mod), val, mod);
}
}
static int findPow(int a, int b, int mod) {
if (b == 1) {
return a % mod;
} else if (b == 0) {
return 1;
} else {
int res = findPow(a, (int) b / 2, mod);
return modMul.mod(res, modMul.mod(res, (b % 2 == 1 ? a : 1), mod), mod);
}
}
static int bleft(long ele, ArrayList<Long> sortedArr) {
int l = 0;
int h = sortedArr.size() - 1;
int ans = -1;
while (l <= h) {
int mid = l + (int) (h - l) / 2;
if (sortedArr.get(mid) < ele) {
l = mid + 1;
} else if (sortedArr.get(mid) >= ele) {
ans = mid;
h = mid - 1;
}
}
return ans;
}
static long gcd(long a, long b) {
long div = b;
long rem = a % b;
while (rem != 0) {
long temp = rem;
rem = div % rem;
div = temp;
}
return div;
}
static int log(int no) {
int i = 0;
while ((1 << i) <= no) {
i++;
}
if ((1 << (i - 1)) == no) {
return i - 1;
} else {
return i;
}
}
static modOperations modAdd = (int a, int b, int mod) -> {
return (a % mod + b % mod) % mod;
};
static modOperations modSub = (int a, int b, int mod) -> {
return (int) ((1l * a % mod - 1l * b % mod + 1l * mod) % mod);
};
static modOperations modMul = (int a, int b, int mod) -> {
return (int) ((1l * (a % mod) * 1l * (b % mod)) % (1l * mod));
};
static modOperations modDiv = (int a, int b, int mod) -> {
return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod);
};
static HashSet<Integer> primeList(int MAXI) {
int[] prime = new int[MAXI + 1];
HashSet<Integer> obj = new HashSet<>();
for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) {
if (prime[i] == 0) {
obj.add(i);
for (int j = i * i; j <= MAXI; j += i) {
prime[j] = 1;
}
}
}
for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) {
if (prime[i] == 0) {
obj.add(i);
}
}
return obj;
}
static int[] factorialList(int MAXI, int mod) {
int[] factorial = new int[MAXI + 1];
factorial[2] = 1;
for (int i = 3; i < MAXI + 1; i++) {
factorial[i] = modMul.mod(factorial[i - 1], i, mod);
}
return factorial;
}
static void put(HashMap<Integer, Integer> cnt, int key) {
if (cnt.containsKey(key)) {
cnt.replace(key, cnt.get(key) + 1);
} else {
cnt.put(key, 1);
}
}
static long arrSum(ArrayList<Long> arr) {
long tot = 0;
for (int i = 0; i < arr.size(); i++) {
tot += arr.get(i);
}
return tot;
}
static int ord(char b) {
return (int) b - (int) 'a';
}
static int optimSearch(int[] cnt, int lower_bound, int pow, int n) {
int l = lower_bound + 1;
int h = n;
int ans = 0;
while (l <= h) {
int mid = l + (h - l) / 2;
if (cnt[mid] - cnt[lower_bound] == pow) {
return mid;
} else if (cnt[mid] - cnt[lower_bound] < pow) {
ans = mid;
l = mid + 1;
} else {
h = mid - 1;
}
}
return ans;
}
static Pair<Long, Integer> ret_ans(ArrayList<Integer> ans) {
int size = ans.size();
int mini = 1000000000 + 1;
long tit = 0l;
for (int i = 0; i < size; i++) {
tit += 1l * ans.get(i);
mini = Math.min(mini, ans.get(i));
}
return new Pair<>(tit - mini, mini);
}
static int factorList(
HashMap<Integer, Integer> maps,
int no,
int maxK,
int req
) {
int i = 1;
while (i * i <= no) {
if (no % i == 0) {
if (i != no / i) {
put(maps, no / i);
}
put(maps, i);
if (maps.get(i) == req) {
maxK = Math.max(maxK, i);
}
if (maps.get(no / i) == req) {
maxK = Math.max(maxK, no / i);
}
}
i++;
}
return maxK;
}
static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getKey());
}
return vals;
}
static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getValue());
}
return vals;
}
/*write your methods here*/
static int getMax(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) > max) {
max = arr.get(i);
}
}
return max;
}
static int getMin(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) < max) {
max = arr.get(i);
}
}
return max;
}
public static void main(String[] args) throws IOException {
int cases = Integer.parseInt(br.readLine()), n, i, j, k;
while (cases-- != 0) {
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int arr[] = new int[n];
for (i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
long tot = 0l;
for (i = 0; i < n; i++) {
int val = (int) Math.pow(10, arr[i]);
if (i == n - 1) {
tot += 1l * (k + 1) * val;
} else {
int val1 = (int) Math.pow(10, arr[i + 1]);
if (tot + 1l * (k + 1) * val < val1) {
tot += 1l * (k + 1) * val;
break;
} else {
tot += 1l * ((val1 / val) - 1) * val;
k -= ((val1 / val) - 1);
}
}
}
bw.write(Long.toString(tot) + "\n");
}
bw.flush();
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 915d5f7edc65ed2b5f59a8bfac2d78fe | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.*;
import java.io.*;
public class Practice {
static boolean multipleTC = true;
final static int mod2 = 1000000007;
final static int mod = 998244353;
final double E = 2.7182818284590452354;
final double PI = 3.14159265358979323846;
int MAX = 200001;
void pre() throws Exception {
}
// All the best
void solve(int TC) throws Exception {
int n = ni();
long k = ni();
int arr[] = readArr(n);
k++;
long ans = 0;
for (int i = 1; i < n; i++) {
long curDenomination = pow(10, arr[i]);
long prevDenomination = pow(10, arr[i - 1]);
long take = min(k, (curDenomination / prevDenomination) - 1);
ans += (prevDenomination * take);
k -= take;
}
ans += (k * pow(10, arr[n - 1]));
pn(ans);
}
public long pow(long base, long exp) {
long res = 1;
while (exp > 0) {
if ((exp & 1) == 1)
res = res * base;
base = base * base;
exp >>= 1;
}
return res;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
double dist(int x1, int y1, int x2, int y2) {
double a = x1 - x2, b = y1 - y2;
return Math.sqrt((a * a) + (b * b));
}
int[] readArr(int n) throws Exception {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
void sort(int arr[], int left, int right) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = left; i <= right; i++)
arr[i] = list.get(i - left);
}
void sort(int arr[]) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < arr.length; i++)
arr[i] = list.get(i);
}
public long max(long... arr) {
long max = arr[0];
for (long itr : arr)
max = Math.max(max, itr);
return max;
}
public int max(int... arr) {
int max = arr[0];
for (int itr : arr)
max = Math.max(max, itr);
return max;
}
public long min(long... arr) {
long min = arr[0];
for (long itr : arr)
min = Math.min(min, itr);
return min;
}
public int min(int... arr) {
int min = arr[0];
for (int itr : arr)
min = Math.min(min, itr);
return min;
}
public long sum(long... arr) {
long sum = 0;
for (long itr : arr)
sum += itr;
return sum;
}
public long sum(int... arr) {
long sum = 0;
for (int itr : arr)
sum += itr;
return sum;
}
String bin(long n) {
return Long.toBinaryString(n);
}
String bin(int n) {
return Integer.toBinaryString(n);
}
static int bitCount(int x) {
return x == 0 ? 0 : (1 + bitCount(x & (x - 1)));
}
static void dbg(Object... o) {
System.err.println(Arrays.deepToString(o));
}
int bit(long n) {
return (n == 0) ? 0 : (1 + bit(n & (n - 1)));
}
int abs(int a) {
return (a < 0) ? -a : a;
}
long abs(long a) {
return (a < 0) ? -a : a;
}
void p(Object o) {
out.print(o);
}
void pn(Object o) {
out.println(o);
}
void pni(Object o) {
out.println(o);
out.flush();
}
void pn(int[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
void pn(long[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
String n() throws Exception {
return in.next();
}
String nln() throws Exception {
return in.nextLine();
}
int ni() throws Exception {
return Integer.parseInt(in.next());
}
long nl() throws Exception {
return Long.parseLong(in.next());
}
double nd() throws Exception {
return Double.parseDouble(in.next());
}
public static void main(String[] args) throws Exception {
new Practice().run();
}
FastReader in;
PrintWriter out;
void run() throws Exception {
in = new FastReader();
out = new PrintWriter(System.out);
int T = (multipleTC) ? ni() : 1;
pre();
for (int t = 1; t <= T; t++)
solve(t);
out.flush();
out.close();
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception {
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | b12f201807272b85e76c2def1ce39e9a | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces {
static long M = 1000000007;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
o:while(cases-- > 0) {
StringBuffer buf = new StringBuffer();
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int[] a = new int[n];
long[] coins = new long[n];
str = br.readLine().split(" ");
for(int i=0; i<n; i++) {
a[i] = Integer.parseInt(str[i]);
coins[i] = (long)Math.pow(10, a[i]);
}
long maxnum[] = new long[n];
for(int i=0; i<n-1; i++) {
maxnum[i] = (coins[i+1]/coins[i]) - 1;
}
maxnum[n-1] = Long.MAX_VALUE;
long ans = 0;
int target = k+1;
int ind = 0;
while(target > 0) {
if(maxnum[ind] >= target) {
ans += target * coins[ind];
break;
}else {
target -= maxnum[ind];
ans += maxnum[ind] * coins[ind];
ind++;
}
}
System.out.println(ans);
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 1bed3d88c5d6f49305be71662927fffa | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static class JoinSet {
int[] fa;
JoinSet(int n) {
fa = new int[n];
for (int i = 0; i < n; i++) fa[i] = i;
}
int find(int t) {
if (t != fa[t]) fa[t] = find(fa[t]);
return fa[t];
}
void join(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
}
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int mod = 998244353;
static int[][] dir1 = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
static int[][] dir2 = new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
static boolean[] prime = new boolean[10];
static {
for (int i = 2; i < prime.length; i++) prime[i] = true;
for (int i = 2; i < prime.length; i++) {
if (prime[i]) {
for (int k = 2; i * k < prime.length; k++) {
prime[i * k] = false;
}
}
}
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static int get() throws Exception {
String ss = bf.readLine();
if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" "));
return Integer.parseInt(ss);
}
static long getx() throws Exception {
String ss = bf.readLine();
if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" "));
return Long.parseLong(ss);
}
static int[] getint() throws Exception {
String[] s = bf.readLine().split(" ");
int[] a = new int[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(s[i]);
}
return a;
}
static long[] getlong() throws Exception {
String[] s = bf.readLine().split(" ");
long[] a = new long[s.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(s[i]);
}
return a;
}
static String getstr() throws Exception {
return bf.readLine();
}
static void println() throws Exception {
bw.write("\n");
}
static void print(int a) throws Exception {
bw.write(a + "\n");
}
static void print(long a) throws Exception {
bw.write(a + "\n");
}
static void print(String a) throws Exception {
bw.write(a + "\n");
}
static void print(int[] a) throws Exception {
for (int i : a) {
bw.write(i + " ");
}
println();
}
static void print(long[] a) throws Exception {
for (long i : a) {
bw.write(i + " ");
}
println();
}
static void print(int[][] a) throws Exception {
for (int i[] : a) print(i);
}
static void print(long[][] a) throws Exception {
for (long[] i : a) print(i);
}
static void print(char[] a) throws Exception {
for (char i : a) {
bw.write(i +"");
}
println();
}
static long pow(long a, long b) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
static int powmod(long a, long b, int mod) {
long ans = 1;
while (b > 0) {
if ((b & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return (int) ans;
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++) b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++) a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++) b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++) a[i] = b[i];
}
static int max(int a, int b) {
return Math.max(a,b);
}
static int min(int a, int b) {
return Math.min(a,b);
}
static long max(long a, long b) {
return Math.max(a,b);
}
static long min(long a, long b) {
return Math.min(a,b);
}
static int max(int[] a) {
int max = a[0];
for(int i : a) max = max(max,i);
return max;
}
static int min(int[] a) {
int min = a[0];
for(int i : a) min = min(min,i);
return min;
}
static long max(long[] a) {
long max = a[0];
for(long i : a) max = max(max,i);
return max;
}
static long min(long[] a) {
long min = a[0];
for(long i : a) min = min(min,i);
return min;
}
static int abs(int a) {
return Math.abs(a);
}
static long abs(long a) {
return Math.abs(a);
}
public static void main(String[] args) throws Exception {
int t = get();
while (t-- > 0){
int p[] = getint();
int n = p[0], k = p[1];
int[] a = getint();
long pow[] = new long[n];
long ans = 0;
k++;
for(int i = 0;i < n;i++) pow[i] = pow(10,a[i]);
for(int i = 0;i < n-1;i++){
if(k > 0){
long x = min(k, pow[i+1]/pow[i]-1);
ans += x*pow[i];
k -= x;
}
}
if(k > 0){
ans += pow[n-1]*k;
}
print(ans);
}
bw.flush();
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 2f74ada9e177936134d64a317b82127f | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class a {
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static HashMap<Long,Integer> map;
public static void main (String[] args) throws java.lang.Exception {
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt()+1;
int arr[] = new int[n];
for(int i = 0;i < n; i++){
arr[i] = sc.nextInt();
}
long sum = 0;
for(int i = 0;i < n-1 && k > 0 ; i++){
long maxNotes = (long)Math.pow(10,arr[i+1]-arr[i])-1;
if(k > maxNotes){
sum +=(long)maxNotes*(long)Math.pow(10,arr[i]);
k -= maxNotes;
}else{
sum += (long)k*(long)Math.pow(10,arr[i]);
k = 0;
}
}
//out.println(sum + " "+k);
if(k > 0){
sum += (long)k*(long)Math.pow(10,arr[n-1]);
}
out.println(sum);
}
// discipline is doing what needs to be done even if you don't want to do it.
out.flush();
}
}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 099b710a3ab5728154952df0e19b1433 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 0; tc < t; ++tc) {
int n = sc.nextInt();
int k = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < a.length; ++i) {
a[i] = sc.nextInt();
}
System.out.println(solve(a, k));
}
sc.close();
}
static long solve(int[] a, int k) {
long result = 0;
int rest = k + 1;
for (int i = 0; i < a.length; ++i) {
int noteNum =
Math.min((i == a.length - 1) ? Integer.MAX_VALUE : pow10(a[i + 1] - a[i]) - 1, rest);
result += (long) pow10(a[i]) * noteNum;
rest -= noteNum;
}
return result;
}
static int pow10(int exponent) {
return IntStream.range(0, exponent).reduce(1, (x, y) -> x * 10);
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | e17b9fb74601a235a74d08cf0fb11115 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class cses{
public static void main(String[] args) {
int t = io.nextInt();
BigInteger[] answers = new BigInteger[t];
for (int i = 0; i < t; i++) {
int n = io.nextInt();
int k = io.nextInt()+1;
boolean[] arr = new boolean[10];
for (int j = 0; j < n; j++) {
arr[io.nextInt()] = true;
}
int[] amt = new int[10];
for (int j = 0; j < 10; j++) {
if(!arr[j])
continue;
int def = Integer.MAX_VALUE;
for (int l = j+1; l < 10; l++) {
if(arr[l]){
def = (int)Math.pow(10, l - j);
break;
}
}
amt[j] = def-1;
}
BigInteger b = BigInteger.ZERO;
while(k > 0){
for (int l = 0; l < 10; l++) {
if(arr[l]){
arr[l]=false;
b = b.add(new BigInteger(Integer.toString((int) Math.pow(10, l))).multiply(new BigInteger(Integer.toString((int) Math.min(k, amt[l])))));
k -= Math.min(k, amt[l]);
}
}
}
answers[i] = b;
}
for (int i = 0; i < t; i++) {
System.out.print(answers[i]);
if(i != t-1)
System.out.println();
}
/*out.deleteCharAt(out.length() - 1);*/
}
static Kattio29 io = new Kattio29();
static class Kattio29 extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio29() {
this(System.in, System.out);
}
public Kattio29(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio29(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) {
}
return null;
}
public int nextInt() { return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | b7ac4f2357bdca69b2c49aecdbedcf85 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes |
// Working program with FastReader
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.lang.*;
public class C_Banknotes {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
boolean flag=true;
long arr[]=new long[n];
for(int i=0;i<n;i++){
int x=sc.nextInt();
arr[i]=(long)Math.pow(10,x);
}
long req=k+1;
long ans=0;
for(int i=0;i<n-1;i++){
long lim=arr[i+1]/arr[i];
long max=lim-1;
if(req<=max){
ans+=(req*arr[i]);
req=0;
}else{
ans+=(max*arr[i]);
req-=max;
}
}
if(req>0){
ans+=(req*arr[n-1]);
}
System.out.println(ans);
// 1000000000
// 999999999
// 999999920999999999
}
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 77b5123f0916e9663d3ba078b376ccf4 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class firstpunch {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);int t=in.nextInt();
while(t--!=0){
int n=in.nextInt();int k= in.nextInt();
int [] a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=(int)Math.pow(10,in.nextInt());
}
long sum=0;
long ans=0;
for(int i=0;i<=n-1;i++)
{
int val=(i==n-1)?Integer.MAX_VALUE:(a[i+1]-a[i])/a[i];
if(sum+val>k){
ans+=a[i]*(k-sum+1);break;
}
sum+=val;ans+=val*a[i];
}
if(t!=0)
{System.out.println(ans);
}else{
System.out.print(ans);
}
}
}}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | b2f2f03081398ad1f99a9f1caafb9c57 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes |
import java.io.*;
import java.util.StringTokenizer;
public class c1606C {
private SimpleInputReader in;
private SimpleOutputWriter out;
public c1606C(Reader r, Writer w) {
in = new SimpleInputReader(r);
out = new SimpleOutputWriter(w);
}
public c1606C() {
in = null;
out = null;
}
public void closeIO() throws Exception {
if(in != null) {
in.close();
}
if(out != null){
out.close();
}
}
public void takeInput() throws Exception {
int t = in.nextInt();
final int[] pow = new int[10];
pow[0] = 1;
for(int i = 0; i < 9; i++) {
pow[i+1] = pow[i] * 10;
}
for(int i = 0; i < t; i++) {
int n = in.nextInt();
int k = in.nextInt();
int[] a = new int[n];
for(int j = 0; j < n; j++) {
a[j] = in.nextInt();
}
long ans = solve(a, n, k, pow);
out.writeLine(ans);
}
}
private long solve(int[] a, int n, int k, int[] pow) throws IOException {
k++;
long res = 0;
int i = 0;
// System.out.println(n);
// System.out.println(k);
while(i < n && k > 0) {
int denomToUse = pow[a[i]];
int notesAv = (i == n-1)?k:Integer.min(pow[a[i+1]]/pow[a[i]]-1,k);
k -= notesAv;
res += (long) denomToUse * notesAv;
// out.writeTokens(denomToUse, " ", notesAv, "\n");
i++;
}
return res;
}
public static void main(String[] args) throws Exception {
Reader r = new InputStreamReader(System.in);
Writer w = new PrintWriter(System.out);
c1606C sol = new c1606C(r, w);
sol.takeInput();
sol.closeIO();
}
}
class SimpleInputReader implements AutoCloseable{
private BufferedReader reader;
private StringTokenizer tokenizer;
public SimpleInputReader(Reader reader) {
this.reader = new BufferedReader(reader);
}
public String readLine() throws IOException {
return reader.readLine();
}
public String nextString() throws IOException {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
String nextLine = readLine();
if(nextLine == null) {
throw new IOException("no more lines left!");
}
tokenizer = new StringTokenizer(nextLine);
}
return tokenizer.nextToken();
}
public Integer nextInt() throws IOException {
return Integer.parseInt(nextString());
}
public Long nextLong() throws IOException {
return Long.parseLong(nextString());
}
public float nextFloat() throws IOException {
return Float.parseFloat(nextString());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextString());
}
@Override
public void close() throws Exception {
reader.close();
}
}
class SimpleOutputWriter implements AutoCloseable{
private final BufferedWriter bufferedWriter;
public SimpleOutputWriter(Writer writer) {
bufferedWriter = new BufferedWriter(writer);
}
public void writeLine(Object s, Object... extras) throws IOException {
writeTokens(s, (Object[]) extras);
bufferedWriter.newLine();
}
public void newLine() throws IOException {
bufferedWriter.newLine();
}
public void flush() throws IOException {
bufferedWriter.flush();
}
public void writeTokens(Object o, Object... extras) throws IOException {
bufferedWriter.write(o.toString());
for (Object extra : extras) {
bufferedWriter.write(extra.toString());
}
}
@Override
public void close() throws Exception {
bufferedWriter.close();
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 74e9a033d09caa6fccabe47781474b4a | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
public class C_Banknotes {
static final int INT_MOD = (int) 1e9 + 7;
static final long LONG_MOD = (long) 1e9 + 7;
static final int INT_POSITIVE_INFINITY = Integer.MAX_VALUE;
static final long LONG_POSITIVE_INFINITY = Long.MAX_VALUE;
static final int INT_NEGATIVE_INFINITY = Integer.MIN_VALUE;
static final long LONG_NEGATIVE_INFINITY = Long.MIN_VALUE;
static StringBuilder result = new StringBuilder();
public static void main(String args[]) throws IOException {
FastReader fr = new FastReader();
FastWriter fw = new FastWriter();
// FastFileReader ffr = new FastFileReader("input.txt");
// FastFileWriter ffw = new FastFileWriter("output.txt");
int tc;
tc = fr.nextInt();
// tc = ffr.nextInt();
while (tc-- > 0) {
int n = fr.nextInt();
long k = fr.nextLong() + 1;
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = fr.nextInt();
Arrays.sort(arr);
long res = 0;
for (int i = 0; i < n - 1; i++) {
long t = Math.min(k, pow(10, arr[i + 1]) / pow(10, arr[i]) - 1);
res += t * pow(10, arr[i]);
k -= t;
}
res += k * pow(10, arr[n - 1]);
result.append(res + "\n");
}
fw.write(result.toString());
// ffw.write(result.toString());
}
static void helper() {
}
static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i++, j--);
}
}
static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
static boolean isPrime(long x) {
if (x <= 1)
return false;
for (long i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
}
static boolean[] sieve(int n) {
boolean[] sieve = new boolean[n + 1];
Arrays.fill(sieve, true);
sieve[0] = sieve[1] = false;
for (int i = 2; i * i <= n; i++) {
if (sieve[i]) {
for (int j = i * i; j <= n; j += i) {
sieve[j] = false;
}
}
}
return sieve;
}
static boolean isFibonacci(long x) {
return isPerfectSquare(5 * x * x + 4);
}
static boolean isPerfectSquare(long x) {
if (x <= 1)
return true;
long low = 1;
long high = x;
long mid = 0;
while (low <= high) {
mid = low + (high - low) / 2l;
if (mid * mid == x)
return true;
else if (mid * mid < x)
low = mid + 1;
else
high = mid - 1;
}
return false;
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static long gcd(long a, long b) {
if (b > a)
return gcd(b, a);
if (b == 0)
return a;
return gcd(b, a % b);
}
static long pow(long b, long e) {
long curr = b;
long res = 1;
while (e != 0) {
if ((e & 1) != 0) {
res = (res * curr);
}
curr = (curr * curr);
e >>= 1;
}
return res;
}
static double log(double x, double base) {
return Math.log(x) / Math.log(base);
}
}
/* user-defined data structures */
class Pair {
long a;
long b;
public Pair(long a, long b) {
this.a = a;
this.b = b;
}
}
class Tair {
int a;
int b;
int c;
public Tair(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
class Fair {
int a;
int b;
int c;
int d;
public Fair(int a, int b, int c, int d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
}
class Point {
int x;
int y;
int z;
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
/* User defined data structures ends here */
/* IO class */
class FastReader {
InputStreamReader isr;
BufferedReader br;
StringTokenizer st;
public FastReader() {
isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
class FastWriter {
OutputStreamWriter osw;
BufferedWriter bw;
public FastWriter() {
osw = new OutputStreamWriter(System.out);
bw = new BufferedWriter(osw);
}
void write(String text) {
try {
bw.write(text);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class FastFileReader {
FileInputStream fis;
InputStreamReader isr;
BufferedReader br;
StringTokenizer st;
public FastFileReader(String fileName) throws FileNotFoundException {
fis = new FileInputStream(fileName);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
class FastFileWriter {
FileOutputStream fos;
OutputStreamWriter osw;
BufferedWriter bw;
public FastFileWriter(String fileName) throws FileNotFoundException {
fos = new FileOutputStream(fileName);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
}
void write(String text) {
try {
bw.write(text);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* IO class ends here */ | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | c1b6789779a24cf9553a56a7c1f4e86a | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] s=br.readLine().split(" ");
long n=Long.parseLong(s[0]);
long k=Long.parseLong(s[1]);
String[] arr=br.readLine().split(" ");
method(arr,k+1);
}
}
public static void method(String[] arr,long k){
long out=0l;
for (int i = 0; i < arr.length-1; i++) {
// System.out.println(out);
Long x=(long)(Math.pow(10,Integer.parseInt(arr[i+1]))-Math.pow(10,Integer.parseInt(arr[i])));
x/=(long)Math.pow(10,Integer.parseInt(arr[i]));
if (k>x){
k-=x;
out+=Math.pow(10,Integer.parseInt(arr[i]))*x;
}
else
{
out+=Math.pow(10,Integer.parseInt(arr[i]))*k;
System.out.println(out);
return ;
}
}
out+=(long)Math.pow(10,Integer.parseInt(arr[arr.length-1]))*k;
System.out.println(out);
return ;
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 3986d9934ed8e0e79abacdc7deabf435 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
public class k
{ //public static int mod=1000000007;
public static long printDivisors(long n, int k)
{
// Note that this loop runs till square root
long x=(long) Math.sqrt(n);
// int p=0;
long sum=0;
for (long i=1; i<=x; i++)
{
if (n%i == 0)
{
// If divisors are equal, print only one
if (n/i == i)
sum=sum+(long)(i);
else // Otherwise print both
sum=sum+(long)(i)+(long)(n/i);
}
if(sum>k)return -1;
}
return sum;
}
public static ArrayList<Long> Factors(long n)
{
ArrayList<Long> arr=new ArrayList<Long>();
int k=0;
while (n%2==0)
{
k++;
n /=2;
arr.add((long)2);
}
int p=(int) Math.sqrt(n);
for (int i = 3; i <=p; i+= 2)
{ if(n==1)break;
while (n%i == 0)
{
k++;
arr.add((long)i);
n /= i;
}
}
if (n > 2)
{
arr.add(n);
}
return arr;
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static long gcd(long x, long p)
{
if (x == 0)
return p;
return gcd(p%x, x);
}
// method to return LCM of two numbers
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int sieve = 1000000 ;
static boolean[] prime = new boolean[sieve + 1] ;
static ArrayList<Integer> pr=new ArrayList<Integer>();
public static void sieveOfEratosthenes()
{
// FALSE == prime and 1 // TRUE == COMPOSITE
// time complexity = 0(NlogLogN)== o(N)
// gives prime nos bw 1 to N // size - 1e7(at max)
for(int i = 4; i<= sieve ; i++)
{
prime[i] = true ; i++ ;
}
for(int p = 3; p*p <= sieve; p++)
{
if(prime[p] == false)
{
pr.add(p);
for(int i = p*p; i <= sieve; i += p)
prime[i] = true;
}
p++ ;
}
}
public static void arrInpInt(int [] arr, int n) throws IOException
{
Reader reader = new Reader();
for(int i=0;i<n;i++)
{
arr[i]=reader.nextInt();
}
}
public static void arrInpLong(long [] arr, int n) throws IOException
{
Reader reader = new Reader();
for(int i=0;i<n;i++)
{
arr[i]=reader.nextLong();
}
}
public static void printArr(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static int[] decSort(int[] arr)
{
int[] arr1 = Arrays.stream(arr).boxed().sorted(Collections.reverseOrder()).mapToInt(Integer::intValue).toArray();
return arr1;
}
//if present - return the first occurrence of the no
//not present- return the index of next greater value
//if greater than all the values return N(taking high=N-1)
//if smaller than all the values return 0(taking low =0)
static int lower_bound(int arr[], int low,int high, int X)
{
if (low > high) {
return low;
}
int mid = low + (high - low) / 2;
if (arr[mid] >= X) {
return lower_bound(arr, low,
mid - 1, X);
}
return lower_bound(arr, mid + 1,
high, X);
}
//if present - return the index of next greater value
//not present- return the index of next greater value
//if greater than all the values return N(taking high=N-1)
//if smaller than all the values return 0(taking low =0)\
static int upper_bound(int arr[], int low, int high, int X)
{
if (low > high)
return low;
int mid = low + (high - low) / 2;
if (arr[mid] <= X) {
return upper_bound(arr, mid + 1,
high, X);
}
return upper_bound(arr, low,
mid - 1, X);
}
public static class Pair {// comparator with class
int x;
int y;
public Pair(int x, int y)
{
this.x = x;
this.y = y;
}
}
public static void sortbyColumn(int arr[][], int col) // send 2d array and col no
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else if (entry1[col] < entry2[col])
return -1;
else return 0;
}
});
}
public static void sortbyColumn1(int arr[][], int col) // send 2d array and col no
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else if (entry1[col] < entry2[col])
return -1;
else if(entry1[col] == entry2[col])
{
if(entry1[col-1]>entry2[col-1])
return -1;
else if(entry1[col-1]<entry2[col-1])
return 1;
else return 0;
}
else return 0;
}
});
}
public static void print2D(int mat[][])
{
// Loop through all rows
for (int i = 0; i < mat.length; i++)
{ // Loop through all elements of current row
{
for (int j = 0; j < mat[i].length; j++)
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
public static int biggestFactor(int num) {
int result = 1;
for(int i=2; i*i <=num; i++){
if(num%i==0){
result = num/i;
break;
}
}
return result;
}
public static int knapsack(int[] weights,int[] price, int totW)
{
int[] dp1=new int[totW+1];
int[] dp2=new int[totW+1];
int N=totW;
int ans=0;
for(int i=0;i<price.length;i++)
{
for(int j=0;j<=N;j++)
{
if(weights[i]>j)
{
if(i%2==0)
{
dp1[j]=dp2[j];
}
else
{
dp2[j]=dp1[j];
}
}
else
{
if(i%2==0)
{
dp1[j]=Math.max(dp2[j],dp2[j-weights[i]]+price[i]);
}
else
{
dp2[j]=Math.max(dp1[j], dp1[j-weights[i]]+price[i]);
}
}
}
if(i%2==0)ans=dp1[N];
else ans=dp2[N];
}
return ans;
}
public static class p
{
int no;
int h;
public p(int no, long h)
{
this.no=no;
this.h=(int) h;
}
}
static class com implements Comparator<p>{
public int compare(p s1, p s2) {
if (s1.h > s2.h)
return -1;
else if (s1.h < s2.h)
return 1;
else if(s1.h==s2.h)
{
if(s1.no>s2.no)return -1;
else return 1;
}
return 0;
}
}
static long hcf(long a,long b)
{
while (b > 0)
{
long temp = b;
b = a % b;
a = temp;
}
return a;
}
static int lower_bound_arr(ArrayList<Integer> arr, int low,
int high, int X)
{
if (low > high) {
return low;
}
int mid = low + (high - low) / 2;
if (arr.get(mid) >= X) {
return lower_bound_arr(arr, low,
mid - 1, X);
}
return lower_bound_arr(arr, mid + 1,
high, X);
}
public static int func2(int m,int[] arr,int k,int[] arr1)
{
for(int i=0;i<arr.length;i++)
{
int p=arr[i];
int q=arr[i]+m;
int in=(q<=arr.length?arr1[q]:arr.length)-arr1[p-1];
if((in)-(arr.length-in)>=k)return arr[i];
}
return 0;
}
public static boolean func(int m,int[] arr,int k,int[] arr1)
{
for(int i=0;i<arr.length;i++)
{
int p=arr[i];
int q=arr[i]+m;
int in=(q<=arr.length?arr1[q]:arr.length)-arr1[p-1];
if((in)-(arr.length-in)>=k)return true;
}
return false;
}
public static int binarySearch(int min, int max, int[] arr,int k,int[] arr1)
{
int l = min, r = max;
while (l <= r) {
int m = l + (r - l) / 2;
boolean x11=func(m,arr,k,arr1);
boolean x1=func(m-1,arr,k,arr1);
if(x11 && !x1)return m;
//Check if x is present at mid
// if (arr[m] == x)
// return m;
// If x greater, ignore left half
if (!x1 && !x11)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
return max;
}
// public static long func(Integer[] arr, int k)
// {
//
// }
// public static HashMap<String,Integer> map1=new HashMap<String,Integer>();
// public static int function1(int[][] arr1,Integer[] arr, int start, int end)
// {
//// System.out.println("1");
//
// int p=0;
// int n=0;
// p=arr1[end][0]-arr1[start-1][0];
// n=arr1[end][1]-arr1[start-1][1];
//// return Math.abs(n-p);
// if(n==p)return 0;
// if(map1.containsKey(start+" "+end))return map1.get(start+" "+end);
// else
// {
// int min=Integer.MAX_VALUE;
// int n1=0;
// int p1=0;
// for(int i=end-1;i>=start-1;i--)
// {
//// System.out.println("pp");
//// int P=p-(arr[i]==1?1:0)-p1+n1;
//// int N=n-(arr[i]==-1?1:0)-n1+p1;
// int P=(arr[i]==1?1:0)-p1+n1;
// int N=(arr[i]==-1?1:0)-n1+p1;
// if(arr[i]==-1)n1++;
// else p1++;
//
// p=arr1[end][0]-arr1[i+1][0];
// n=arr1[end][1]-arr1[i+1][1];
//
//// min=Math.min(Math.abs(P-N), min);
//// map1.put((i+1)+" "+end,min+1);
// }
//
//
//
// return min;
// }
// }
public static void main(String args[]) throws NumberFormatException, IOException ,java.lang.Exception
{
Reader reader = new Reader();
// power();
long[] pow2 =new long[64];
pow2[0]=1l;
for(int i=1;i<64;i++)
{
// pow2[i]=(int) Math.pow(2, i);
pow2[i]=(long)(10)*pow2[i-1];
// System.out.println(pow2[i]);
}
//Scanner reader=new Scanner(System.in);
// PrintWriter out = new PrintWriter(System.out);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// int cases=Integer.parseInt(br.readLine());
// int cases=1;
int cases=reader.nextInt();
while (cases-->0){
// int N=reader.nextLong();
//
// long M=reader.nextLong();
// long H1=reader.nextLong();
// long D1=reader.nextLong();
// long H2=reader.nextLong();
// long D2=reader.nextLong();
//
// long C=reader.nextLong();
// long W=reader.nextLong();
// long A=reader.nextLong();
int N=reader.nextInt();
int M=reader.nextInt();
// int K=reader.nextInt();
// long M=reader.nextLong();
//
// long X=reader.nextLong();
// String p="";
// while(p.equals(""))p=br.readLine();
////
// String[] first1=br.readLine().split(" ");
// int N=Integer.parseInt(first1[0]);
// int M=Integer.parseInt(first1[1]);
// long K=Long.parseLong(first1[0]);
// long X=Long.parseLong(first1[1]);
// String s2=br.readLine();
// String s3=br.readLine();
// char[] s11=s2.toCharArray();
// char[] s12=new char[s11.length];
int max=Integer.MIN_VALUE;
// int max1=Integer.MIN_VALUE;
int min=Integer.MAX_VALUE;
int min1=Integer.MAX_VALUE;
// int min2=Integer.MAX_VALUE;
long mod=1000000007;
// HashMap<Integer, Integer> map=new HashMap<Integer,Integer>();
// PriorityQueue<Integer> q = new PriorityQueue<Integer>(Collections.reverseOrder());
// PriorityQueue<Long> q = new PriorityQueue<Long>(Collections.reverseOrder());
// HashMap<Integer,TreeSet<Integer>> map=new HashMap<Integer,TreeSet<Integer>>();
// HashMap<Long,Long> map=new HashMap<Long,Long>();
// HashMap<String,String> map1=new HashMap<String,String>();
//HashMap<Character,Integer> path=new HashMap<Character,Integer>();
// List<TreeMap<Integer,Integer>> map = new ArrayList<TreeMap<Integer,Integer>>();
// HashSet<String> set =new HashSet<String>();
// HashSet<String> set1 =new HashSet<String>();
// HashSet<Integer> map =new HashSet<Integer>();
// TreeSet<Integer> a =new TreeSet<Integer>();
//TreeSet<Long> b =new TreeSet<Long>();
// TreeSet<Integer> map=new TreeSet<Integer>();
int[] arr=new int[N];
// int[] arr1=new int[N];
// int[] arr3=new int[N];
// int[] arr4=new int[N];
//
// int[] arr1=new int[N];
// int[] arr2=new int[26];// i00nt[] odd=new int[100001];
// int[] arr=new int[5];
// int[] arr2=new int[5];
// Integer[] arr=new Integer[N];
// Integer[] arr1=new Integer[N];
// long[] arr=new long[N];
// Long[] arr=new Long[N];
// int[][] arr=new int[N][P];
// ArrayList<Long> list=new ArrayList<Long>();
// ArrayList<Long> list3=new ArrayList<Long>();
// ArrayList<Long> list1=new ArrayList<Long>();
// ArrayList<Long> bees=new ArrayList<Long>();
// boolean[]arr1=new boolean[N];
//
// boolean first=true;
// output.append((ind+1)+" "+(N));output.newLine();
// in(ans2, ans3)));
// int[][] arr=new int[N][M];
// System.out.println(N+" "+M);
for(int i=0;i<N;i++)arr[i]=reader.nextInt();
Arrays.sort(arr);
long notes=M+1;
long ans=0;
for(int j=0;j<N-1;j++)
{
long max1=(long) (pow2[arr[j+1]-arr[j]]-1);
if(notes>max1) {notes=notes-max1;ans=(long) (ans+max1*(pow2[arr[j]]));}
else {ans=(long) (ans+notes*(pow2[arr[j]]));notes=0;break;}
}
if(notes>0)
{
ans=(long) (ans+notes*pow2[arr[N-1]]);
}
System.out.println(ans);
// output.flush();
}
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | b82421652925820a10654800c985a66f | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n){
int[] res=new int[n];
for(int i=0;i<n;i++)res[i]=nextInt();
return res;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int testCases=in.nextInt();
//int testCases=1;
while(testCases-- > 0){
solve(in);
}
out.close();
} catch (Exception e) {
return;
}
}
public static void solve( FastReader in){
int n=in.nextInt();
int k=in.nextInt();
//int y=in.nextInt();
//long n=in.nextLong();
//int k=in.nextInt();
//long k=in.nextLong();
StringBuilder res=new StringBuilder();
int[] arr=new int[n];
for(int i=0;i<n;i++){
int x=in.nextInt();
int c=1;
while(x-->0) c*=10;
arr[i]=c;
}
k+=1;
long ans=0;
for(int i=0;i<n;i++){
int c=k;
if(i+1<n)c=Math.min(c,arr[i+1]/arr[i]-1);
ans+=arr[i]*1L*c;
k-=c;
}
res.append(""+(ans));
System.out.println(res.toString());
}
static int gcd(int a,int b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static void debug(String x){
System.out.println(x);
}
static < E > void print(E res)
{
System.out.println(res);
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 25a15a48c9fecc14c52a7ec9e95137e0 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
public class ComdeFormces {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
FastReader sc=new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int t=sc.nextInt();
while(t--!=0) {
int n=sc.nextInt();
int k=sc.nextInt();
long a[]=new long[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
long ans=0;
int nt=0;
long val=0;
for(int i=0;i<n;i++) {
if(i+1<n) {
long up=pow(10,a[i+1]);
long curr=pow(10,a[i]);
long cnt=(up-1)/curr;
if(k-nt<cnt) {
long v=k-nt+1;
ans=curr*v+(val);
break;
}
else {
nt+=cnt;
val+=cnt*curr;
}
}
else {
long v=k-nt+1;
long curr=pow(10,a[i]);
if(v>=1) {
ans=curr*v+(val);
}
else ans=val+1;
}
}
log.write((ans)+"\n");
log.flush();
}
}
static boolean bsch(long k,long hc,long dc,long hm,long dm,long w,long a) {
long s=0;
long e=k;
while(s<=e) {
long b=k-s;
long nhc=hc+s*a;
long ndc=dc+b*w;
long moves=hm%ndc==0?hm/ndc:(hm/ndc)+1;
long moves2=nhc%dm==0?nhc/dm:(nhc/dm)+1;
if(moves2>=moves)return true;
s++;
}
return false;
}
//static ArrayList<Integer> pr(int n){
// boolean vis[]=new boolean[n+1];
// vis[0]=false;
// vis[1]=false;
// for(int i=2;i<=n;i++) {
// if(vis[i]) {
// for(int j=2*i;j<=n;j+=i) {
// vis[j]=false;
// }
// }
// }
//}
static long slv(int a[],int b[],long dp[][],int end,int k,int i) {
if(i<1 ) {
if(k==0) {
return (end-a[0])*b[0];
}
else return Integer.MAX_VALUE;
}
if(k<0)return Integer.MAX_VALUE;
if(k==0) {
return (end-a[0])*b[0];
}
if(dp[i][k]!=0)return dp[i][k];
long ans1=slv(a,b,dp,a[i],k-1,i-1);
long ans2=slv(a,b,dp,end,k,i-1);
long val=(end-a[i])*b[i];
return dp[i][k]=Math.min(val+ans1,ans2);
}
static int bss(int[] a, int k) {
int s=0;
int e=a.length-1;
int max=-1;
while(s<=e) {
int m=s+(e-s)/2;
int val=solv(a,m);
if(val==k) {
max=Math.max(max, m);
s=m+1;
}
else if(val<k)s=m+1;
else e=m-1;
}
return max;
}
static int solv(int[] a,int len) {
if(len%2==0) {
int ans=0;
for(int i=0;i<a.length;i++){
if(a[i]>0){
if(a[i]%2==0) {
ans+=a[i];
}
else if(a[i]%2!=0) {
ans+=(a[i]/2)*2;
}
}
}
int cnt=ans/len;
return cnt;
}
else {
int ans=0,one=0;
for(int i=0;i<a.length;i++){
if(a[i]>0){
if(a[i]%2==0) {
ans+=a[i];
}
else {
ans+=(a[i]/2)*2;
one++;
}
}
}
int n=len-1;
int cnt=ans/n;
int mod=cnt%n+one;
if(cnt>=mod)return cnt;
return mod;
}
}
//debug
static pair bss(ArrayList<pair> a,int el,int ind) {
int s=0;
int e=a.size()-1;
pair ans=new pair(-1,-1);
while(s<=e) {
int m=s+(e-s)/2;
if(a.get(m).a==el) {
ans=a.get(m);
e=m-1;
}
if(a.get(m).a>el)e=m-1;
else s=m+1;
}
return ans;
}
public static <E> void p(E[][] a,String s) {
System.out.println(s);
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[0].length;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void p(int[] a,String s) {
System.out.print(s+"=");
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
public static <E> void p(E a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(ArrayList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(LinkedList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(HashSet<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Stack<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Queue<E> a,String s){
System.out.println(s+"="+a);
}
//utils
static ArrayList<Integer> divisors(int n){
ArrayList<Integer> ar=new ArrayList<>();
for (int i=2; i<=Math.sqrt(n); i++){
if (n%i == 0){
if (n/i == i) {
ar.add(i);
}
else {
ar.add(i);
ar.add(n/i);
}
}
}
return ar;
}
static int primeDivisor(int n){
ArrayList<Integer> ar=new ArrayList<>();
int cnt=0;
boolean pr=false;
while(n%2==0) {
pr=true;
n/=2;
}
if(pr)ar.add(2);
for(int i=3;i*i<=n;i+=2) {
pr=false;
while(n%i==0) {
n/=i;
pr=true;
}
if(pr)ar.add(i);
}
if(n>2) ar.add(n);
return ar.size();
}
static String rev(String s) {
char temp[]=s.toCharArray();
for(int i=0;i<temp.length/2;i++) {
char tp=temp[i];
temp[i]=temp[temp.length-1-i];
temp[temp.length-1-i]=tp;
}
return String.valueOf(temp);
}
static int bs(ArrayList<pair> arr,int el) {
int start=0;
int end=arr.size()-1;
while(start<=end) {
int mid=start+(end-start)/2;
if(arr.get(mid).a==el)return mid;
else if(arr.get(mid).a<el)start=mid+1;
else end=mid-1;
}
if(start>arr.size()-1)return -2;
return -1;
}
static long find(int s,long a[]) {
if(s>=a.length)return -1;
long num=a[s];
for(int i=s;i<a.length;i+=2) {
num=gcd(num,a[i]);
if(num==1 || num==0)return -1;
}
return num;
}
static long gcd(long a,long b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static int gcd(int a,int b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static long factmod(long n,long mod,long img) {
if(n==0)return 1;
long ans=1;
long temp=1;
while(n--!=0) {
if(temp!=img) {
ans=((ans%mod)*((temp)%mod))%mod;
}
temp++;
}
return ans%mod;
}
static int bs(long a[] ,long num) {
int start=0;
int end=a.length-1;
while(start<=end) {
int mid=start+(end-start)/2;
if(a[mid]==num) {
return mid;
}
else if(a[mid]<num)start=mid+1;
else end=mid-1;
}
return start;
}
static int ncr(int n, int r){
if(r>n-r)r=n-r;
int ans=1;
for(int i=0;i<r;i++){
ans*=(n-i);
ans/=(i+1);
}
return ans;
}
public static class trip{
int a,b,c;
public trip(int a,int b,int c) {
this.a=a;
this.b=b;
this.c=c;
}
public int compareTo(trip q) {
return this.c-q.c;
}
}
static void mergesort(ArrayList<pair> a,int start,int end) {
if(start>=end)return ;
int mid=start+(end-start)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,end);
merge(a,start,mid,end);
}
static void merge(ArrayList<pair> a, int start,int mid,int end) {
int ptr1=start;
int ptr2=mid+1;
pair b[]=new pair[end-start+1];
int i=0;
while(ptr1<=mid && ptr2<=end) {
if(a.get(ptr1).a<=a.get(ptr2).a) {
b[i]=a.get(ptr1);
ptr1++;
i++;
}
else {
b[i]=a.get(ptr2);
ptr2++;
i++;
}
}
while(ptr1<=mid) {
b[i]=a.get(ptr1);
ptr1++;
i++;
}
while(ptr2<=end) {
b[i]=a.get(ptr2);
ptr2++;
i++;
}
for(int j=start;j<=end;j++) {
a.set(j, b[j-start]);
}
}
public static class FastReader {
BufferedReader b;
StringTokenizer s;
public FastReader() {
b=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(s==null ||!s.hasMoreElements()) {
try {
s=new StringTokenizer(b.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return s.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=b.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (s != null && s.hasMoreTokens()) {
return true;
}
String tmp;
try {
b.mark(1000);
tmp = b.readLine();
if (tmp == null) {
return false;
}
b.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
public static class pair{
int a;
int b;
public pair(int a,int b) {
this.a=a;
this.b=b;
}
public int compareTo(pair b) {
if(this.b!=b.b) return this.b-b.b;
else return this.a-b.a;
}
public int compareToo(pair b) {
return this.b-b.b;
}
@Override
public String toString() {
return "{"+this.a+" "+this.b+"}";
}
}
static long pow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
static int pow(int a, int pw) {
int temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | d7e263a680d465144e4b8c37eb0a4fc2 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
public class Run {
public static int mod = 1000000007;
public static PrintWriter out = new PrintWriter(System.out);
public static boolean testing = false;
public static FastIO io;
public static String input_file = "./input.txt";
static {
try {
io = new FastIO();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static int[] sort(int[] arr) {
List<Integer> nums = new ArrayList<>();
for (int el : arr) nums.add(el);
Collections.sort(nums);
int[] res = new int[arr.length];
for (int i = 0; i < arr.length; i++) res[i] = nums.get(i);
return res;
}
public static long[] sort(long[] arr) {
List<Long> nums = new ArrayList<>();
for (long el : arr) nums.add(el);
Collections.sort(nums);
long[] res = new long[arr.length];
for (int i = 0; i < arr.length; i++) res[i] = nums.get(i);
return res;
}
public static void reverse(char[] arr, int i, int j) {
while (i < j) {
char t = arr[i];
arr[i] = arr[j];
arr[j] = t;
++i;
--j;
}
}
public static void reverse(int[] arr, int i, int j) {
while (i < j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
++i;
--j;
}
}
public static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static int lowerBound(long[] arr, long x) {
int l = -1;
int r = arr.length;
while (r > l + 1) {
int m = l + (r - l) / 2;
if (arr[m] <= x) l = m;
else r = m;
}
return Math.max(0, l);
}
public static int upperBound(long[] arr, long x) {
int l = -1;
int r = arr.length;
while (r > l + 1) {
int m = l + (r - l) / 2;
if (arr[m] >= x) r = m;
else l = m;
}
return Math.min(arr.length - 1, r);
}
public static int log2(int a) {
return (int) (Math.log(a) / Math.log(2));
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static long kadane(int[] arr, int i, int j) {
long best = Long.MIN_VALUE;
long curr = 0;
while (i <= j) {
int el = arr[i++];
curr = Math.max(curr + el, el);
best = Math.max(best, curr);
}
return best;
}
public static void main(String[] args) {
int t = io.nextInt();
while (t-- > 0) {
int n = io.nextInt();
int k = io.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = io.nextInt();
long ans = 0;
k++;
int i = 0;
while (k > 0 && i + 1 < n) {
int x = (int) (Math.pow(10, arr[i + 1]) / Math.pow(10, arr[i])) - 1;
int y = Math.min(k, x);
ans += y * Math.pow(10, arr[i]);
k -= y;
++i;
}
ans += k * (long) Math.pow(10, arr[n - 1]);
out.write(ans + "\n");
}
out.close();
}
public static void inc(HashMap<Integer, Integer> hm, int el, int x) {
hm.put(el, hm.getOrDefault(el, 0) + x);
if (hm.get(el) == 0) hm.remove(el);
}
static class CC implements Comparator<Pair> {
public int compare(Pair o1, Pair o2) {
if (o1.first == o2.first) return o1.second - o2.second;
return o1.first - o2.first;
}
}
static class Pair {
int first, second;
long d;
public Pair(int first, int second, long d) {
this.first = first;
this.second = second;
this.d = d;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
}
static class FastIO {
InputStreamReader s = new InputStreamReader(testing ? new FileInputStream(input_file) : System.in);
BufferedReader br = new BufferedReader(s);
StringTokenizer st = new StringTokenizer("");
FastIO() throws FileNotFoundException {
}
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | f00c82bfe973d29ad418e4fa7ca9d94b | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.*;
import java.io.*;
import java.time.*;
import static java.lang.Math.*;
@SuppressWarnings("unused")
public class A {
static boolean DEBUG = false;
static Reader fs;
static PrintWriter pw;
static void solve() {
int n = fs.nextInt(), k = fs.nextInt();
int a[] = fs.readArray(n);
// int ans[] = new int[1_000_000_000];
for (int i = 0; i < n; i++) {
a[i] = (int) pow(10, a[i]);
}
lg(a);
k++;
long res = 0;
for (int i = 0; i < n - 1; i++) {
int cnt = k;
// if(i + 1 < n)
cnt = min(cnt, a[i + 1] / a[i] - 1);
k -= cnt;
res += cnt * (long) a[i];
}
pw.println(res +(long)k * a[n-1]);
}
static void lg(Object o) {
if (DEBUG)
System.err.println(o);
}
static void lg(int a[]) {
if (DEBUG)
System.err.println(Arrays.toString(a));
}
static void lg(char a[]) {
if (DEBUG)
System.err.println(Arrays.toString(a));
}
static void lg(String a) {
if (DEBUG)
System.err.print(a + " -> ");
}
static <T> void lg(T a[]) {
if (DEBUG) {
System.err.print("[");
for (T x : a)
System.err.print(x + " ");
System.err.println("]");
}
}
static <T> void lg(ArrayList<T> a) {
if (DEBUG) {
System.err.print("[");
for (T x : a) {
System.err.print(x + " ");
}
System.err.println("]");
}
}
static <T, V> void lg(Map<T, V> mp) {
if (DEBUG) {
System.err.print(mp);
}
}
public static void main(String[] args) throws IOException {
Instant start = Instant.now();
if (args.length == 2) {
System.setIn(new FileInputStream(new File("D:\\program\\javaCPEclipse\\CodeForces\\src\\input.txt")));
// System.setOut(new PrintStream(new File("output.txt")));
System.setErr(new PrintStream(new File("D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt")));
DEBUG = true;
}
fs = new Reader();
pw = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
solve();
}
Instant end = Instant.now();
if (DEBUG) {
pw.println(Duration.between(start, end));
}
pw.close();
}
static void sort(int a[]) {
ArrayList<Integer> l = new ArrayList<Integer>();
for (int x : a)
l.add(x);
Collections.sort(l);
for (int i = 0; i < a.length; i++) {
a[i] = l.get(i);
}
}
public static void print(long a, long b, long c, PrintWriter pw) {
pw.println(a + " " + b + " " + c);
return;
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int[][] read2Array(int n, int m) {
int a[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextInt();
}
}
return a;
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 00afba17d6081eef8e7e827db3dd4dfe | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
/**
* Provide prove of correctness before implementation. Implementation can cost a lot of time.
* Anti test that prove that it's wrong.
* <p>
* Do not confuse i j k g indexes, upTo and length. Do extra methods!!! Write more informative names to simulation
* <p>
* Will program ever exceed limit?
* Try all approaches with prove of correctness if task is not obvious.
* If you are given formula/rule: Try to play with it.
* Analytic solution/Hardcoded solution/Constructive/Greedy/DP/Math/Brute force/Symmetric data
* Number theory
* Game theory (optimal play) that consider local and global strategy.
*/
public class MS_1_F {
/*
4
3 13
0 1 2
2 777
0 4
3 255
0 1 3
10 1000000000
0 1 2 3 4 5 6 7 8 9
----
59
778
148999
1117759999
*/
private void solveOne() {
int n = nextInt();
int k = nextInt();
int[] a = nextIntArr(n);
for (int i = 0; i < n; i++) {
a[i] = pow(10, a[i]);
}
int[] maxCnt = new int[n];
for (int i = 0; i < n - 1; i++) {
maxCnt[i] = (a[i + 1] - a[i]) / a[i];
}
maxCnt[n - 1] = Integer.MAX_VALUE;
System.out.println(rev_f(k + 1, a, maxCnt));
// Map<Integer, List<Integer>> kToS = new TreeMap<>();
//
// for (int i = 1; i < 200_000; i++) {
//
// int curK = f(i, a);
// if (curK > k) {
// if (!kToS.containsKey(curK)) {
// kToS.put(curK, new ArrayList<>());
// }
// kToS.get(curK).add(i);
// }
// }
//
// System.out.println(kToS.toString());
}
//Let's build greedy the min possible sum from given k
long rev_f(int k, int[] a, int[] maxCnt) {
long s = 0;
for (int i = 0; i < a.length; i++) {
long curCnt = Math.min(k, maxCnt[i]);
k -= curCnt;
s += curCnt * a[i];
}
return s;
}
int f(int s, int[] a) {
int k = 0;
for (int i = a.length - 1; i >= 0; i--) {
k += s / a[i];
s %= a[i];
}
return k;
}
Map<Integer, Long> f_(long s, int[] a){
Map<Integer, Long> ans = new TreeMap<>(Comparator.reverseOrder());
for (int i = a.length - 1; i >= 0; i--) {
ans.put(a[i], s / a[i]);
s %= a[i];
}
return ans;
}
int pow(int x, int p) {
int ans = 1;
int mult = x;
while (p > 0) {
if ((p & 1) == 1)
ans *= mult;
mult *= mult;
p >>= 1;
}
return ans;
}
private void solve() {
int t = System.in.readInt();
for (int tt = 0; tt < t; tt++) {
solveOne();
}
}
class AssertionRuntimeException extends RuntimeException {
AssertionRuntimeException(Object expected,
Object actual, Object... input) {
super("expected = " + expected + ",\n actual = " + actual + ",\n " + Arrays.deepToString(input));
}
}
private int nextInt() {
return System.in.readInt();
}
private long nextLong() {
return System.in.readLong();
}
private String nextString() {
return System.in.readString();
}
private int[] nextIntArr(int n) {
return System.in.readIntArray(n);
}
private long[] nextLongArr(int n) {
return System.in.readLongArray(n);
}
public static void main(String[] args) {
new MS_1_F().run();
}
static class System {
private static FastInputStream in;
private static FastPrintStream out;
}
private void run() {
System.in = new FastInputStream(java.lang.System.in);
System.out = new FastPrintStream(java.lang.System.out);
solve();
System.out.flush();
}
private static class FastPrintStream {
private static final int BUF_SIZE = 8192;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastPrintStream() {
this(java.lang.System.out);
}
public FastPrintStream(OutputStream os) {
this.out = os;
}
public FastPrintStream(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastPrintStream print(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerflush();
return this;
}
public FastPrintStream print(char c) {
return print((byte) c);
}
public FastPrintStream print(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
return this;
}
public FastPrintStream print(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
});
return this;
}
//can be optimized
public FastPrintStream print0(char[] s) {
if (ptr + s.length < BUF_SIZE) {
for (char c : s) {
buf[ptr++] = (byte) c;
}
} else {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerflush();
}
}
return this;
}
//can be optimized
public FastPrintStream print0(String s) {
if (ptr + s.length() < BUF_SIZE) {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
}
} else {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
if (ptr == BUF_SIZE) innerflush();
}
}
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastPrintStream print(int x) {
if (x == Integer.MIN_VALUE) {
return print((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerflush();
if (x < 0) {
print((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastPrintStream print(long x) {
if (x == Long.MIN_VALUE) {
return print("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerflush();
if (x < 0) {
print((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastPrintStream print(double x, int precision) {
if (x < 0) {
print('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
print((long) x).print(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
print((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastPrintStream println(int[] a, int from, int upTo, char separator) {
for (int i = from; i < upTo; i++) {
print(a[i]);
print(separator);
}
print('\n');
return this;
}
public FastPrintStream println(int[] a) {
return println(a, 0, a.length, ' ');
}
public FastPrintStream println(char c) {
return print(c).println();
}
public FastPrintStream println(int x) {
return print(x).println();
}
public FastPrintStream println(long x) {
return print(x).println();
}
public FastPrintStream println(String x) {
return print(x).println();
}
public FastPrintStream println(double x, int precision) {
return print(x, precision).println();
}
public FastPrintStream println() {
return print((byte) '\n');
}
public FastPrintStream printf(String format, Object... args) {
return print(String.format(format, args));
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
}
private static class FastInputStream {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastInputStream(InputStream stream) {
this.stream = stream;
}
public double[] readDoubleArray(int size) {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = readDouble();
}
return array;
}
public String[] readStringArray(int size) {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = readString();
}
return array;
}
public char[] readCharArray(int size) {
char[] array = new char[size];
for (int i = 0; i < size; i++) {
array[i] = readCharacter();
}
return array;
}
public void readIntArrays(int[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readInt();
}
}
}
public void readLongArrays(long[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readLong();
}
}
}
public void readDoubleArrays(double[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readDouble();
}
}
}
public char[][] readTable(int rowCount, int columnCount) {
char[][] table = new char[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readCharArray(columnCount);
}
return table;
}
public int[][] readIntTable(int rowCount, int columnCount) {
int[][] table = new int[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = readIntArray(columnCount);
}
return table;
}
public double[][] readDoubleTable(int rowCount, int columnCount) {
double[][] table = new double[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readDoubleArray(columnCount);
}
return table;
}
public long[][] readLongTable(int rowCount, int columnCount) {
long[][] table = new long[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = readLongArray(columnCount);
}
return table;
}
public String[][] readStringTable(int rowCount, int columnCount) {
String[][] table = new String[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readStringArray(columnCount);
}
return table;
}
public String readText() {
StringBuilder result = new StringBuilder();
while (true) {
int character = read();
if (character == '\r') {
continue;
}
if (character == -1) {
break;
}
result.append((char) character);
}
return result.toString();
}
public void readStringArrays(String[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readString();
}
}
}
public long[] readLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = readLong();
}
return array;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int peekNonWhitespace() {
while (isWhitespace(peek())) {
read();
}
return peek();
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return readString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 9af27ab3378af65fbe7bf7f3ad399469 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class firstpunch {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);int t=in.nextInt();
while(t--!=0){
int n=in.nextInt();int k= in.nextInt();
int [] a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=(int)Math.pow(10,in.nextInt());
}
long sum=0;
long ans=0;
for(int i=0;i<=n-1;i++)
{
int val=(i==n-1)?Integer.MAX_VALUE:(a[i+1]-a[i])/a[i];
if(sum+val>k){
ans+=a[i]*(k-sum+1);break;
}
sum+=val;ans+=val*a[i];
}
if(t!=0)
{System.out.println(ans);
}else{
System.out.print(ans);
}
}
}} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | fa9084fa88fb5db94bdaaa6b17184c01 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class firstpunch {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);int t=in.nextInt();
while(t--!=0){
int n=in.nextInt();int k= in.nextInt();
int [] a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=(int)Math.pow(10,in.nextInt());
}
long sum=0;
long ans=0;
for(int i=0;i<=n-1;i++)
{
int val=(i==n-1)?Integer.MAX_VALUE:(a[i+1]-a[i])/a[i];
if(sum+val>k){
ans+=a[i]*(k-sum+1);break;
}
sum+=val;ans+=val*a[i];
}
if(t!=0)
{System.out.println(ans);
}else{
System.out.print(ans);
}
}
}} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | f07b41208ce08a248dae8eb7e8ecb955 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main (String[] args) throws IOException {
Kattio io = new Kattio();
int t = io.nextInt();
for (int ii=0; ii<t; ii++) {
int n = io.nextInt();
long k = io.nextLong()+1;
long ans = 0;
boolean[] has = new boolean[30];
Arrays.fill(has, false);
for (int i=0; i<n; i++) {
int x = io.nextInt();
has[x] = true;
}
int left = 0;
while (true) {
//System.out.println("Current left: " + left);
if (has[left]) {
int consec = 0;
int origin = left;
left++;
while (left < 30 && !has[left]) {
left++;
consec++;
}
//System.out.println("After left: " + left);
//System.out.println(has[left]);
long pow = (long) Math.pow(10, consec+1)-1;
ans += Math.min(k, pow) * (long) Math.pow(10, origin);
k -= Math.min(k, pow);
}
if (k == 0) {
break;
}
}
System.out.println(ans);
//3 255
//1011
//9 + 99(10) + 147(1000)
}
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 8649371c06039ab40c5b1793b020647e | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
while (t-- > 0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int arr[] = new int[n];
for(int i = 0; i < n; i++)
{
int e= Integer.parseInt(st.nextToken());
arr[i] = (int)Math.pow(10, e);
}
k++;
long res = 0;
for(int i = 0; i < n - 1; i++)
{
int cnt = Math.min(k, (arr[i+1] / arr[i]) - 1);
k-=cnt;
res+= cnt * (long)arr[i];
}
res+= k * (long)(arr[n-1]);
output.write(res + "\n");
// int k = Integer.parseInt(st.nextToken());
// char arr[] = br.readLine().toCharArray();
// output.write();
// int n = Integer.parseInt(st.nextToken());
// HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// if
// output.write("YES\n");
// else
// output.write("NO\n");
// long a = Long.parseLong(st.nextToken());
// long b = Long.parseLong(st.nextToken());
// if(flag == 1)
// output.write("NO\n");
// else
// output.write("YES\n" + x + " " + y + " " + z + "\n");
// output.write(n+ "\n");
}
output.flush();
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | c222939d700d54a2497d3c0d461cfd0d | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.*;
import java.util.*;
import javax.sound.midi.SysexMessage;
import java.math.*;
public class Main
{
// class Reader
// {
// final private int BUFFER_SIZE = 1 << 100000000;
// private DataInputStream din;
// private byte[] buffer;
// private int bufferPointer, bytesRead;
// public Reader()
// {
// din = new DataInputStream(System.in);
// if (System.getProperty("ONLINE_JUDGE") == null)
// {
// try
// {
// System.setOut(new PrintStream(new FileOutputStream("output.txt")));
// din = new DataInputStream(new FileInputStream("input.txt"));
// }
// catch (Exception e) {
// }
// }
// buffer = new byte[BUFFER_SIZE];
// bufferPointer = bytesRead = 0;
// }
// public Reader(String file_name) throws IOException
// {
// din = new DataInputStream(
// new FileInputStream(file_name));
// buffer = new byte[BUFFER_SIZE];
// bufferPointer = bytesRead = 0;
// }
// public String readLine() throws IOException
// {
// byte[] buf = new byte[100000000]; // line length
// int cnt = 0, c;
// while ((c = read()) != -1) {
// if (c == '\n') {
// if (cnt != 0) {
// break;
// }
// else {
// continue;
// }
// }
// buf[cnt++] = (byte)c;
// }
// return new String(buf, 0, cnt);
// }
// public int nextInt() throws IOException
// {
// int ret = 0;
// byte c = read();
// while (c <= ' ') {
// c = read();
// }
// boolean neg = (c == '-');
// if (neg)
// c = read();
// do {
// ret = ret * 10 + c - '0';
// } while ((c = read()) >= '0' && c <= '9');
// if (neg)
// return -ret;
// return ret;
// }
// public long nextLong() throws IOException
// {
// long ret = 0;
// byte c = read();
// while (c <= ' ')
// c = read();
// boolean neg = (c == '-');
// if (neg)
// c = read();
// do {
// ret = ret * 10 + c - '0';
// } while ((c = read()) >= '0' && c <= '9');
// if (neg)
// return -ret;
// return ret;
// }
// public double nextDouble() throws IOException
// {
// double ret = 0, div = 1;
// byte c = read();
// while (c <= ' ')
// c = read();
// boolean neg = (c == '-');
// if (neg)
// c = read();
// do {
// ret = ret * 10 + c - '0';
// } while ((c = read()) >= '0' && c <= '9');
// if (c == '.') {
// while ((c = read()) >= '0' && c <= '9') {
// ret += (c - '0') / (div *= 10);
// }
// }
// if (neg)
// return -ret;
// return ret;
// }
// private void fillBuffer() throws IOException
// {
// bytesRead = din.read(buffer, bufferPointer = 0,
// BUFFER_SIZE);
// if (bytesRead == -1)
// buffer[0] = -1;
// }
// private byte read() throws IOException
// {
// if (bufferPointer == bytesRead)
// fillBuffer();
// return buffer[bufferPointer++];
// }
// public void close() throws IOException
// {
// if (din == null)
// return;
// din.close();
// }
// }
public String ns() throws IOException
{
return(read.nextLine());
}
public String wrd() throws IOException
{
return(read.next());
}
public int ni() throws IOException
{
return(read.nextInt());
}
public double nd() throws IOException
{
return(read.nextDouble());
}
public long nl() throws IOException
{
return(read.nextLong());
}
public int[] ai(int n) throws IOException
{
int arr[] = new int[n];
for(int i = 0; i<n; i++)
arr[i] = read.nextInt();
return(arr);
}
public double[] ad(int n) throws IOException
{
double arr[] = new double[n];
for(int i = 0; i<n; i++)
arr[i] = read.nextDouble();
return(arr);
}
public long[] al(int n) throws IOException
{
long arr[] = new long[n];
for(int i = 0; i<n; i++)
arr[i] = read.nextLong();
return(arr);
}
// Reader read;
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
if (System.getProperty("ONLINE_JUDGE") == null)
{
try
{
InputStream inputStream = new FileInputStream("input.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
br = new BufferedReader(inputStreamReader);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble(){return Double.parseDouble(next());}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException
{
Main obj = new Main();
obj.solve();
//obj.read = new Reader();
}
public int getParent(int parent[], int src)
{
if(parent[src] != src)
{
int ultimateparent = getParent(parent, parent[src]);
parent[src] = ultimateparent;
return ultimateparent;
}
return src;
}
public boolean union(int i, int j, int parent[] , int rank[])
{
int root1 = getParent(parent, i);
int root2 = getParent(parent, j);
if(root1 == root2)
return false;
if(rank[root1] == rank[root2])
{
parent[root1] = root2;
rank[root2]++ ;
}
else if(rank[root1] > rank[root2])
{
parent[root2] = root1;
}
else // rank[root2] > rank[root1]
{
parent[root1] = root2;
}
return true;
}
public int log(long x)
{
return (int) (Math.log(x) / Math.log(2) + 1e-10);
}
public int fact(int n)
{
if (n <= 1)
return 1;
return n * fact(n - 1);
}
public int nPr(int n, int r)
{
return fact(n) / fact(n - r);
}
public long binomial1(long n, long r)
{
// when n-r > r
long ans = 1;
for(long i = (n-r+1); i<=n; i++)
ans *= i;
for(long i = 2; i<=r; i++)
ans /= i;
return ans;
}
public long binomial2(long n, long r)
{
//when r > n-r
long ans = 1;
for(long i = r+1; i<=n; i++)
ans *= i;
for(long i = 2; i<=(n-r); i++)
ans /= i;
return ans;
}
public long binomial(int n, int r, long dp[][])
{
// if(n-r > r)
// return(binomial1(n, r));
// else
// return(binomial2(n, r));
if(dp[n][r] != 0l)
return dp[n][r];
if(r == 0)
return 1;
if(n < r)
return 0;
dp[n][r] = ( ((n*1l)* binomial(n-1, r-1, dp) )/ r);
return(dp[n][r]);
}
long gcd (long a, long b)
{
//System.out.print("GCD OF "+a+" , "+b+" = ");
long r, i;
while(b!=0)
{
r = a % b;
a = b;
b = r;
}
//System.out.println(" "+a);
return a;
}
public int getParent(int root, int parent[])
{
if(parent[root] == root)
{
return(root);
}
parent[root] = getParent(parent[root], parent);
return(parent[root]);
}
public void buildtree(long segmentTree[], int idx, int start, int end, long arr[])
{
if(start == end)
{
segmentTree[idx] = arr[start];
return;
}
int mid = (end + start)/2;
buildtree(segmentTree, 2*idx, start, mid, arr);
buildtree(segmentTree, 2*idx + 1, mid+1, end, arr);
segmentTree[idx] = gcd(segmentTree[2*idx] , segmentTree[2*idx +1]);
}
public long query(long segmentTree[] , int idx, int start, int end, int querystart, int queryend)
{
if(start >= querystart && end <= queryend)
return(segmentTree[idx]);
else if(end < querystart || start > queryend)
return 0;
else
{
int mid = (end + start)/2;
long leftQuery = query(segmentTree, 2*idx, start, mid, querystart, queryend);
long rightQuery = query(segmentTree, 2*idx + 1, mid+1, end, querystart, queryend);
return(gcd(leftQuery , rightQuery));
}
}
void reverse(char arr[])
{
int mid = arr.length/2;
int len = arr.length;
for(int i = 0; i<mid; i++)
{
char temp = arr[i];
arr[i] = arr[len - i - 1];
arr[len - i - 1] = temp;
}}
String reverse(String s)
{
StringBuilder sb = new StringBuilder(s);
return(sb.reverse().toString());
}
class Trie
{
Trie children[];
int count;
Trie()
{
children = new Trie[2];
count = 0;
}
}
void insertTrie(Trie head, char arr[])
{
int idx = 0;
Trie curr = head;
while( idx < arr.length)
{
int mod = (arr[idx]-'0')%2;
if(curr.children[mod] == null)
{
curr.children[mod] = new Trie();
}
curr = curr.children[mod];
curr.count++;
idx++;
}
}
void removeTrie(Trie head, char arr[])
{
int idx = 0;
Trie curr = head;
while( idx < arr.length)
{
int mod = (arr[idx]-'0')%2;
curr = curr.children[mod];
curr.count--;
idx++;
}
}
int query(Trie head, char pattern[])
{
int idx = 0;
Trie curr = head;
while( idx < pattern.length)
{
int mod = (pattern[idx]-'0');
if(curr.children[mod] == null)
return 0;
curr = curr.children[mod];
idx++;
}
return (curr == null ? 0 : curr.count);
}
void sieveOfEratosthenes(boolean prime[], int n)
{
//boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
} }
long power(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1) == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
boolean miillerTest(long d, long n)
{
// Pick a random number in [2..n-2]
// Corner cases make sure that n > 4
long a = 2 + (int)(Math.random() % (n - 4));
// Compute a^d % n
long x = power(a, d, n);
if (x == 1 || x == n - 1)
return true;
// Keep squaring x while one of the
// following doesn't happen
// (i) d does not reach n-1
// (ii) (x^2) % n is not 1
// (iii) (x^2) % n is not n-1
while (d != n - 1) {
x = (x * x) % n;
d *= 2;
if (x == 1)
return false;
if (x == n - 1)
return true;
}
// Return composite
return false;
}
boolean isPrime(long n, int k)
{
// Corner cases
if (n <= 1 || n == 4)
return false;
if (n <= 3)
return true;
// Find r such that n = 2^d * r + 1
// for some r >= 1
long d = n - 1;
while (d % 2 == 0)
d /= 2;
// Iterate given nber of 'k' times
for (long i = 0; i < k; i++)
if (!miillerTest(d, n))
return false;
return true;
}
ArrayList<Long> allDivisors(long n)
{
ArrayList<Long> list = new ArrayList<>();
for (long i = 2; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
{
list.add(i);
}
else
{
list.add(i);
list.add(n/i);
}
}
}
return(list);
}
public boolean isPalindrome(char arr[], int startIndex, int endIndex)
{
for(int i = startIndex, j = endIndex; i <= j; i++, j--)
{
if (arr[i] != arr[j])
return false;
}
return true;
}
public int longestPalindrome(char arr[], int left, int right)
{
int n = right - left + 1;
int longestLen = 0;
int longestIndex = 0;
for(int currentIndex = left; currentIndex < n; currentIndex++)
{
if(isPalindrome(arr,currentIndex - longestLen, currentIndex))
{
longestLen += 1;
longestIndex = currentIndex;
}
else if(currentIndex - longestLen - 1 >= 0 && isPalindrome(arr, currentIndex - longestLen - 1, currentIndex))
{
longestLen += 2;
longestIndex = currentIndex;
}
}
longestIndex++;
return(longestLen);
}
//--------------------------------------------------------------------------------------------------------------
long MODULO = 1000000007;
FastReader read;
StringBuilder sb;
public void solve() throws IOException
{
sb = new StringBuilder();
read = new FastReader();
int t = ni();
for(int tt = 0; tt < t; tt++)
{
int n = ni();
long k = nl();
int arr[] = new int[n];
for(int i = 0; i<n; i++)
arr[i] = ni();
int idx = 0;
long sum = 0;
while(k != 0 && idx != arr.length-1)
{
long max = (int) (Math.pow(10, arr[idx+1] - arr[idx]) - 1);
long times = Math.min(k, (int) (Math.pow(10, arr[idx+1] - arr[idx]) - 1));
sum += (long) (times * (Math.pow(10, arr[idx])));
if(max == times)
{
idx++;
}
//System.out.println(" K = "+k+" TIMES = "+times+" MAX = "+max);
k -= times;
//System.out.println("TIMES = "+times);
}
//System.out.println(sum);
//System.out.println(idx);
sum += (long) (k * (Math.pow(10, arr[n-1])));
sum += (long) (Math.pow(10, arr[idx]));
//System.out.println("-----------------------------");
sb.append(sum).append("\n");
}
System.out.println(sb);
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | e839a58712deccfa67744a86304ea84a | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for (int i = 0; i < t; i++) {
ArrayList<Integer> options = new ArrayList<Integer>();
int m = s.nextInt();
int k = s.nextInt();
for (int j = 0; j < m; j++) {
options.add(s.nextInt());
}
long result = 0;
String cur = "";
long temp = 0;
long sum = 0;
k++;
for (int j = 0; j < options.size(); j++) {
cur = "";
if (j != options.size() - 1) {
for (int l = 0; l < options.get(j + 1) - options.get(j); l++) {
cur += "9";
}
temp = Integer.parseInt(cur);
sum += Integer.parseInt(cur);
long mult = (long)(Math.pow(10, options.get(j)));
if (sum > k) {
sum -= k;
result = result + (temp - sum) * mult;
break;
} else {
result = result + (temp) * mult;
}
} else {
result = result + (long)(k - sum) * (long)(Math.pow(10, options.get(j)));
}
}
System.out.println(result);
}
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 11 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 0349357c36b766a43da95df319d7a831 | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
public class main {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringBuilder str = new StringBuilder();
int tc = Integer.parseInt(reader.readLine());
while (tc-- > 0) {
String[] inputs = reader.readLine().split(" ");
int n = Integer.parseInt(inputs[0]);
int k = Integer.parseInt(inputs[1]);
int[] map = new int[n];
int[] arr = new int[n];
inputs = reader.readLine().split(" ");
for (int i = 0; i < n; i++) {
map[i] = (int)Math.pow(10, Integer.parseInt(inputs[i]));
}
Arrays.sort(map);
k++;
for (int i = 0; i < n; i++) {
if (i == n-1) {
arr[i] = k;
} else {
arr[i] = (map[i+1]/map[i])-1;
if (k < arr[i]) {
arr[i] = k;
k = 0;
} else {
k -= arr[i];
}
}
}
BigInteger ans = new BigInteger("0");
for (int i = n-1; i >= 0; i--) {
if (arr[i] == 0) continue;
ans = ans.add(BigInteger.valueOf(map[i]).multiply(BigInteger.valueOf(arr[i])));
}
str.append(ans.toString());
str.append("\n");
}
System.out.println(str.toString());
}
} | Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 17 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 5fd63f23e841b77b42d49e6c425b0f0a | train_108.jsonl | 1635518100 | In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) > k$$$). | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class RoundEdu116C {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// the code for the deep recursion (more than 100k or 3~400k or so)
// Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28);
// t.start();
// t.join();
RoundEdu116C sol = new RoundEdu116C();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ...
int n;
long k;
int[] a;
void getInput() {
n = in.nextInt();
k = in.nextLong();
a = in.nextIntArray(n);
}
void printOutput() {
out.printlnAns(ans);
}
long ans;
void solve(){
long[] denomination = new long[n];
Arrays.fill(denomination, 1);
for(int i=0; i<n; i++) {
for(int j=0; j<a[i]; j++)
denomination[i] *= 10;
}
ans = 0;
k++;
for(int i=0; i<n-1; i++) {
long num = Math.min(k, denomination[i+1]/denomination[i]-1);
ans += num*denomination[i];
k -= num;
}
ans += k*denomination[n-1];
}
// Optional<T> solve()
// return Optional.empty();
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextTransposedMatrix(int n, int m){
return nextTransposedMatrix(n, m, 0);
}
int[][] nextTransposedMatrix(int n, int m, int offset){
int[][] mat = new int[m][n];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[j][i] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
// public <T> void printlnAns(Optional<T> ans) {
// if(ans.isEmpty())
// println(-1);
// else
// printlnAns(ans.get());
// }
public void printlnAns(OptionalInt ans) {
println(ans.orElse(-1));
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"] | 2 seconds | ["59\n778\n148999\n999999920999999999"] | null | Java 17 | standard input | [
"greedy",
"number theory"
] | e25e44b8f300d6be856df50bff8ff244 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 < a_2 < \dots < a_n \le 9$$$). | 1,400 | For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes. | standard output | |
PASSED | 1c7cf861c1d191af8dea8324d4745754 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | // package com.company.Codeforces;
import java.util.*;
import java.lang.*;
import java.io.*;
public class scratch
{
public static void solve(){
String s = sc.next();
str.append(s.charAt(s.length()-1) + s.substring(1));
}
static PrintWriter out;
static StringBuilder str;
static Scanner sc;
public static void main (String[] args) throws java.lang.Exception
{
sc = new Scanner();
str = new StringBuilder();
out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
solve();
str.append('\n');
}
out.println(str);
out.close();
}
private static long power(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1)==1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
private static long power(long a, long b, long m) {
a %= m;
long res = 1;
while (b > 0) {
if ((b & 1)==1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
private static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
private static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
br = new BufferedReader(new
InputStreamReader(System.in));
/*
try {
br = new BufferedReader(new
InputStreamReader(new FileInputStream(new File("file_i_o\\input.txt"))));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
Integer[] nextIntegerArray(int n) {
Integer[] array = new Integer[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++) {
array[i] = nextLong();
}
return array;
}
String[] nextStringArray() {
return nextLine().split(" ");
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++) {
array[i] = next();
}
return array;
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 17 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 4e1678eb28bd052e175122aea1a1b47e | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.HashMap;
public class Solution
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t!=0)
{
String s=sc.next();
if(s.length()==1)
{
System.out.println(s.charAt(0));
}
else if(s.charAt(0)==s.charAt(s.length()-1))
{
System.out.println(s);
}
else
{
for(int i=0;i<s.length()-1;i++)
{
System.out.print(s.charAt(i));
}
if(s.charAt(s.length()-1)=='a')
{
System.out.print('b');
}
else
{
System.out.print('a');
}
System.out.println();
}
t--;
}
sc.close();
}
static int gcd(int a, int b)
{
if (a == 0 || b == 0)
{
return 0;
}
if (a == b)
{
return a;
}
if (a > b)
{
return gcd(a-b, b);
}
return gcd(a, b-a);
}
static boolean coPrime(int a, int b)
{
int min=Math.min(a,b);
for(int i=min;i>=2;i--)
{
if(a%i==0&&b%i==0)
{
return false;
}
}
return true;
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 17 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 9e94832c713b06b20da42b3865536af6 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Question {
public static String fun(String value){
if(value.length() <=1){
return value;
}
if(value.charAt(0) == value.charAt(value.length()-1)){
return value;
}
else{
StringBuilder val = new StringBuilder(value);
val.setCharAt(value.length()-1, value.charAt(0));
return val.toString();
}
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
for(int i=0;i<t;i++){
String val = sc.nextLine();
String out = fun(val);
System.out.println(out);
}
}
}
//next 32
// try more of string manupulation questions | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 17 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | b7d6e60034fdb3d0ee531ee21aa425ed | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes |
import java.util.*;
public class P3 {
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
while(n>0) {
String s = sc.nextLine();
int c1 = 0;
int c2 = 0;
char c []= s.toCharArray();
if(c[0] == c[s.length()-1]) System.out.println(s);
else {
if(c[0] == 'a') c[s.length()-1] = 'a';
else c[s.length()-1] = 'b';
String ans = "";
for(int i = 0;i<s.length();i++) {
ans+=c[i];
}
System.out.println(ans);
}
n--;
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 17 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 708efef6c5baa21c9c2d7ec2e73ae0d3 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class RoundEdu116A {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// the code for the deep recursion (more than 100k or 3~400k or so)
// Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28);
// t.start();
// t.join();
RoundEdu116A sol = new RoundEdu116A();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ...
String s;
void getInput() {
s = in.next();
}
void printOutput() {
out.println(ans);
}
String ans;
void solve(){
// aaaabbbbbbaaaaaaabbbbbb
// up down up down
if(s.charAt(0) == s.charAt(s.length()-1))
ans = s;
else {
ans = s.substring(0, s.length()-1) + s.charAt(0);
}
}
// Optional<T> solve()
// return Optional.empty();
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextTransposedMatrix(int n, int m){
return nextTransposedMatrix(n, m, 0);
}
int[][] nextTransposedMatrix(int n, int m, int offset){
int[][] mat = new int[m][n];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[j][i] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
// public <T> void printlnAns(Optional<T> ans) {
// if(ans.isEmpty())
// println(-1);
// else
// printlnAns(ans.get());
// }
public void printlnAns(OptionalInt ans) {
println(ans.orElse(-1));
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 17 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 38abd115183b39e4d698144e08e2dbe9 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
public class another {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-- > 0){
String s = sc.next();
char[] arr= s.toCharArray();
int n = arr.length;
int ab = 0;
int ba = 0;
for(int i = 0;i < n-1; i++){
char ch = arr[i];
if(ch == 'a' && arr[i+1] == 'b'){
ab++;
}else if(ch == 'b' && arr[i+1] == 'a'){
ba++;
}
}
if(ab > ba){
arr[n-1] = 'a';
}else if(ab < ba){
arr[n-1] = 'b';
}
for(char c: arr){
System.out.print(c);
}
System.out.println();
}
}
static class Pair{
int c,idx;
// public Pair(int u, int v){
// this.c = u;
// this.idx = v;
// }
}
static class Sorting implements Comparator<Pair>{
public int compare(Pair p1, Pair p2){
if(p2.c==p1.c){
return p2.c-p1.c;
}
return p2.c - p1.c;
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 17 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | a64ff094197527626fbf06c1c1a995a4 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
public class Main{
static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
int t = s.nextInt();
while(t-->0){
String str= s.next();
StringBuffer sb = new StringBuffer(str);
int count1 = 0;
int count2 =0;
for(int i=1; i<str.length();i++){
if(str.charAt(i-1) =='a' && str.charAt(i) == 'b') count1++;
else if(str.charAt(i-1)=='b'&& str.charAt(i) =='a') count2++;
}
if(count1!= count2){
if(str.charAt(0) == 'a') sb.setCharAt(0, 'b');
else if(str.charAt(0) == 'b') sb.setCharAt(0,'a');
}
System.out.println(sb);
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 17 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 6e05efee2ba940717454cf80cd68c6ca | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.*;
import java.math.*;
import java.io.*;
import java.text.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int t = 1; t<=T; t++) {
String S = sc.next();
int l = S.length();
String s = "";
s = s + S.charAt(l-1);
for(int i = 1; i<l; i++) {
s = s + S.charAt(i);
}
System.out.println(s);
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 9baff1ff1172aef95bfdab894fd87c0b | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.*;
import java.math.*;
import java.io.*;
import java.text.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) throws IOException
{
Scanner s=new Scanner(System.in);
int op=s.nextInt();
while(op-- >0) {
String str=s.next();
int ab=0;
int ba=0;
for(int i=0;i<str.length()-1;i++) {
String tmp=str.charAt(i)+"";
tmp+=str.charAt(i+1);
if(tmp.equals("ab")) {
ab++;
}
if(tmp.equals("ba")) {
ba++;
}
tmp="";
}
// System.out.println(ab+" "+ba);
if(ab==ba) {
System.out.println(str);
}else {
String ans=str.substring(0,str.length()-1);
if(str.charAt(str.length()-1)=='a') {
ans+='b';
}else {
ans+='a';
}
System.out.println(ans);
}
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 0ed9b0e0a1b0ecbe54283fe9514a56f8 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | /*
ID: abdelra29
LANG: JAVA
PROG: zerosum
*/
/*
TO LEARN
2-euler tour
*/
/*
TO SOLVE
codeforces 722 kavi on pairing duty
*/
/*
bit manipulation shit
1-Computer Systems: A Programmer's Perspective
2-hacker's delight
*/
/*
TO WATCH
*/
import java.util.*;
import java.math.*;
import java.io.*;
import java.util.stream.Collectors;
public class A{
static FastScanner scan=new FastScanner();
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static boolean vis[][];
public static void main(String[] args) throws Exception
{
/*
very important tips
1-just fucking think backwards once in your shitty life
2-consider brute forcing and finding some patterns and observations
3-when you're in contest don't get out because you think there is no enough time
*/
//scan=new FastScanner("binary.in");
///out = new PrintWriter("zerosum.out");
/*
READING
3-Introduction to DP with Bitmasking codefoces
4-Bit Manipulation hackerearth
5-read more about mobious and inculsion-exclusion
*/
/*
*/
int tt=1;
tt=scan.nextInt();
//System.out.println(1^2);
int T=1;
outer:while(tt-->0)
{
String s=scan.next();
int ab=0,ba=0;
for(int i=0;i<s.length()-1;i++)
{
if(s.charAt(i)=='a'&&s.charAt(i+1)=='b')
ab++;
else if(s.charAt(i)=='b'&&s.charAt(i+1)=='a')
ba++;
}
if(ab==ba){
out.println(s);
continue outer;
}
String fin="";
int yep=1000;
for(int i=0;i<s.length();i++)
{
int j=i;
//System.out.println(j);
// char c=s.charAt(i);
StringBuilder tmp=new StringBuilder(s);
if(tmp.charAt(i)=='a')
tmp.setCharAt(i,'b');
else tmp.setCharAt(i,'a');
ab=0;
ba=0;
for(int k=0;k<s.length()-1;k++)
{
if(tmp.charAt(k)=='a'&&tmp.charAt(k+1)=='b')
ab++;
else if(tmp.charAt(k)=='b'&&tmp.charAt(k+1)=='a')
ba++;
}
if(ab==ba)
{
out.println(tmp);
continue outer;
}
//System.out.println(i+" "+j);
i=j;
}
for(int i=0;i<s.length();)
{
int j=i;
//System.out.println(j);
// char c=s.charAt(i);
StringBuilder tmp=new StringBuilder(s);
int ans=0;
while(j<s.length()&&s.charAt(j)==s.charAt(i))
{
ans++;
if(tmp.charAt(j)=='a')
tmp.setCharAt(j,'b');
else tmp.setCharAt(j,'a');
j++;
}
//System.out.println(i);
//System.out.println(tmp);
ab=0;
ba=0;
for(int k=0;k<s.length()-1;k++)
{
if(tmp.charAt(k)=='a'&&tmp.charAt(k+1)=='b')
ab++;
else if(tmp.charAt(k)=='b'&&tmp.charAt(k+1)=='a')
ba++;
}
if(ab==ba&&ans<=yep)
{
yep=ans;
fin=tmp.toString();
}
//System.out.println(i+" "+j);
i=j;
}
out.println(fin);
}
out.close();
}
static class special{
boolean is;
char c;
public special(boolean is,char c)
{
this.is=is;
this.c=c;
}
@Override
public int hashCode() {
return (int)(c+ 31 *(is?1:0));
}
@Override
public boolean equals(Object o){
// System.out.println("FUCK");
if (o == this) return true;
if (o.getClass() != getClass()) return false;
special t = (special)o;
return t.is == is && t.c == c;
}
}
static long binexp(long a,long n)
{
if(n==0)
return 1;
long res=binexp(a,n/2);
if(n%2==1)
return res*res*a;
else
return res*res;
}
static long powMod(long base, long exp, long mod) {
if (base == 0 || base == 1) return base;
if (exp == 0) return 1;
if (exp == 1) return (base % mod+mod)%mod;
long R = (powMod(base, exp/2, mod) % mod+mod)%mod;
R *= R;
R %= mod;
if ((exp & 1) == 1) {
return (base * R % mod+mod)%mod;
}
else return (R %mod+mod)%mod;
}
static double dis(double x1,double y1,double x2,double y2)
{
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long mod(long x,long y)
{
if(x<0)
x=x+(-x/y+1)*y;
return x%y;
}
public static long pow(long b, long e) {
long r = 1;
while (e > 0) {
if (e % 2 == 1) r = r * b ;
b = b * b;
e >>= 1;
}
return r;
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
//Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private static void sort2(long[] arr) {
List<Long> list = new ArrayList<>();
for (Long object : arr) list.add(object);
Collections.sort(list);
Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
static class FastScanner
{
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
static class Pair implements Comparable<Pair>{
public long x, y,z;
public Pair(long x1, long y1,long z1) {
x=x1;
y=y1;
z=z1;
}
public Pair(long x1, long y1) {
x=x1;
y=y1;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
public String toString() {
return x + " " + y+" "+z;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Pair t = (Pair)o;
return t.x == x && t.y == y&&t.z==z;
}
public int compareTo(Pair o)
{
return (int)(x-o.x);
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 97e0ca00b24d13e312f430fc8c8d4330 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
// -- static variables --- //
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static int mod = (int) 1000000007;
public static void main(String[] args) throws Exception {
int t=sc.nextInt();
while(t-->0)
Main.go();
// out.println();
out.flush();
}
// >>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<< //
static class pair{
int x,y;
pair(int x,int y){
this.x=x;
this.y=y;
}
}
static void go() throws Exception {
String s=sc.next();
if(s.charAt(0)==s.charAt(s.length()-1)) {
out.println(s);
}else {
out.println(s.substring(0,s.length()-1)+s.charAt(0));
}
}
// >>>>>>>>>>> Code Ends <<<<<<<<< //
// --For Rounding--//
static double round(double value, int places) {
if (places < 0)
throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
// ----Greatest Common Divisor-----//
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// --- permutations and Combinations ---//
static long fact[];
static long invfact[];
static long ncr(int n, int k) {
if (k < 0 || k > n) {
return 0;
}
long x = fact[n];
long y = fact[k];
long yy = fact[n - k];
long ans = (x / y);
ans = (ans / yy);
return ans;
}
// ---sieve---//
static int prime[] = new int[1000006];
// static void sieve() {
// Arrays.fill(prime, 1);
// prime[0] = 0;
// prime[1] = 0;
// for (int i = 2; i * i <= 1000005; i++) {
// if (prime[i] == 1)
// for (int j = i * i; j <= 1000005; j += i) {
// prime[j] = 0;
// }
// }
// }
// ---- Manual sort ------//
static void sort(long[] a) {
ArrayList<Long> aa = new ArrayList<>();
for (long i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static void sort(int[] a) {
ArrayList<Integer> aa = new ArrayList<>();
for (int i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
// --- Fast exponentiation ---//
static long pow(long x, long y) {
long res = 1l;
while (y != 0) {
if (y % 2 == 1) {
res = (x * res);
}
y /= 2;
x = (x * x);
}
return res;
}
// >>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<< //
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] intArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
return a;
}
long[] longArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | f4a3c4a307a4bf258df07c2e8983135f | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
// -- static variables --- //
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static int mod = (int) 1000000007;
public static void main(String[] args) throws Exception {
int t=sc.nextInt();
while(t-->0)
Main.go();
// out.println();
out.flush();
}
// >>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<< //
static class pair{
int x,y;
pair(int x,int y){
this.x=x;
this.y=y;
}
}
static void go() throws Exception {
char a[]=sc.next().toCharArray();
int n=a.length;
int ab=0;
int ba=0;
for(int i=0;i<n-1;i++) {
if(a[i]=='a' && a[i+1]=='b') {
ab+=1;
}
}
for(int i=n-1;i>0;i--) {
if(a[i]=='a' && a[i-1]=='b') {
ab--;
}
}
if(ab==0) {
out.println(String.valueOf(a));
return;
}
if(ab>0) {
for(int i=0;i<n-1;i++) {
if(a[i]=='a' && a[i+1]=='b'&& ab>0) {
int j=i;
while(j>=0&&a[j]=='a') {
j--;
}
a[j+1]='b';
ab--;
}
}
out.println(String.valueOf(a));
}else {
for(int i=n-1;i>0;i--) {
if(a[i]=='a' && a[i-1]=='b' && ab<0) {
int j=i;
while(j<n&&a[j]=='a') {
j++;
}
a[j-1]='b';
ab++;;
}
}
out.println(String.valueOf(a));
}
}
// >>>>>>>>>>> Code Ends <<<<<<<<< //
// --For Rounding--//
static double round(double value, int places) {
if (places < 0)
throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
// ----Greatest Common Divisor-----//
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// --- permutations and Combinations ---//
static long fact[];
static long invfact[];
static long ncr(int n, int k) {
if (k < 0 || k > n) {
return 0;
}
long x = fact[n];
long y = fact[k];
long yy = fact[n - k];
long ans = (x / y);
ans = (ans / yy);
return ans;
}
// ---sieve---//
static int prime[] = new int[1000006];
// static void sieve() {
// Arrays.fill(prime, 1);
// prime[0] = 0;
// prime[1] = 0;
// for (int i = 2; i * i <= 1000005; i++) {
// if (prime[i] == 1)
// for (int j = i * i; j <= 1000005; j += i) {
// prime[j] = 0;
// }
// }
// }
// ---- Manual sort ------//
static void sort(long[] a) {
ArrayList<Long> aa = new ArrayList<>();
for (long i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
static void sort(int[] a) {
ArrayList<Integer> aa = new ArrayList<>();
for (int i : a) {
aa.add(i);
}
Collections.sort(aa);
for (int i = 0; i < a.length; i++)
a[i] = aa.get(i);
}
// --- Fast exponentiation ---//
static long pow(long x, long y) {
long res = 1l;
while (y != 0) {
if (y % 2 == 1) {
res = (x * res);
}
y /= 2;
x = (x * x);
}
return res;
}
// >>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<< //
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] intArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
return a;
}
long[] longArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 18971804077612db29d413f33d539bb1 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
public class ABBalance {
private static final String ab = "ab";
private static final String ba = "ba";
private static final char a = 'a';
private static final char b = 'b';
private static final int i = 0;
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int testCase = scan.nextInt();
while(testCase+1>0){
String s = scan.nextLine();
int countAB = Count(s, ab);
int countBA = Count(s, ba);
// System.out.println(countAB);
// System.out.println(countBA);
if((countAB == countBA) || (s.length() == 1)){
System.out.println(s);
}
else{
StringBuilder sb = new StringBuilder(s);
if(s.charAt(i) == a){
sb.setCharAt(i, b);
}
else if(s.charAt(i) == b){
sb.setCharAt(i, a);
}
s = sb.toString();
System.out.println(s);
}
testCase--;
}
scan.close();
}
private static int Count(String str, String substr) {
if(str.isEmpty() || substr.isEmpty()){
return 0;
}
int index = 0;
int count = 0;
while(index != -1) {
index = str.indexOf(substr,index);
if(index != -1){
count++;
index += 1;
}
else{
break;
}
}
return count;
}
}
// sample test cases :-
// input
// 4
// b
// aabbbabaa
// abbb
// abbaab
// baa
// baab
// abbab
// abbabb
// aaaaabbbbb
// bbbbbbaaaa
// output
// b
// aabbbabaa
// bbbb
// bbbaab
// aaa
// baab
// bbbab
// bbbabb
// bbbbbbbbbb
// aaaaaaaaaa
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | dcb651bf24ca8d5f3f08cc06ed254154 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
import java.text.DecimalFormat;
public class Main {
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() throws FileNotFoundException {
if (System.getProperty("os.name").equals("Mac OS X")) {
// Input is a file
br = new BufferedReader(new FileReader("input.txt"));
// PrintWriter class prints formatted representations
// of objects to a text-output stream.
PrintStream pw = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(pw);
} else {
// Input is System.in
br = new BufferedReader(new InputStreamReader(System.in), 32768);
st = null;
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int i() {
return Integer.parseInt(next());
}
int[] intArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++)
ret[i] = i();
return ret;
}
long l() {
return Long.parseLong(next());
}
long[] longArray(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++)
ret[i] = l();
return ret;
}
double d() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// **********************************Code Begins From
// Here***************************************
// DecimalFormat df = new DecimalFormat("#.000000000000");
// map.put(key,map.getOrDefault(key,0)+1);
public static void solve() {
String s = c.next(), ans = "";
int n = s.length();
char a[] = s.toCharArray();
if (a[0] != a[n - 1])
a[0] = a[n - 1];
for (char e : a)
ans += e;
pn(ans);
}
public static void main(String[] args) throws FileNotFoundException {
// Scanner sc = new Scanner(System.in);
c = new FastScanner();
pw = new PrintWriter(System.out);
int tc = c.i();
long start = System.currentTimeMillis();
for (int t = 0; t < tc; t++) {
// p("Case #" + (t + 1) + ": ");
solve();
}
long end = System.currentTimeMillis();
if (System.getProperty("os.name").equals("Mac OS X")) {
pn("The Program takes " + (end - start) + "ms");
}
for (long i = 0; i < arr.length; i++) {
if (arr[(int) i]) {
primes.add(i);
}
}
// for(int i=1;i<1000000;i++)
// {
// sumList.add((long)(i*(i+1)/2));
// }
pw.close();
}
// ArrayList<Integer> al = new ArrayList<>();
// Set<Integer> set = new TreeSet<>();
// Map<Integer, Integer> map = new HashMap<>();
// for(Map.Entry<Integer,Integer> e:map.entrySet())pn(e.getKey()+"
// "+e.getValue());
// LinkedList<Integer> ll = new LinkedList<>();
// Stack<Integer>st = new Stack<>();
private static FastScanner c;
private static Pair<Integer, Integer> pair;
private static PrintWriter pw;
private static int mod = (int) (1e9 + 7);
private static final int IBIG = 1000000007;
private static final int IMAX = 2147483647;
private static final int IMIN = -2147483648;
private static ArrayList<String> permute(String str, int l, int r, ArrayList<String> al) {
if (l == r) {
al.add(str.toString());
return al;
} else {
for (int i = l; i <= r; i++) {
str = swap(str, l, i);
permute(str, l + 1, r, al);
str = swap(str, l, i);
}
}
return al;
}
/**
* Swap Characters at position
*
* @param a string value
* @param i position 1
* @param j position 2
* @return swapped string
*/
public static String swap(String a, int i, int j) {
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
static ArrayList<String> printSubSeqRec(String str, int n, int index, String curr, ArrayList<String> al) {
// base case
if (index == n) {
return al;
}
if (curr != null && !curr.trim().isEmpty()) {
if (isStringSorted(curr))
al.add(curr);
}
for (int i = index + 1; i < n; i++) {
curr += str.charAt(i);
printSubSeqRec(str, n, i, curr, al);
// backtracking
curr = curr.substring(0, curr.length() - 1);
}
return al;
}
public static ArrayList<ArrayList<Integer>> printSubsequences(int[] arr, int index, ArrayList<Integer> path,
ArrayList<ArrayList<Integer>> ans) {
// Print the subsequence when reach
// the leaf of recursion tree
if (index == arr.length) {
// Condition to avoid printing
// empty subsequence
if (path.size() > 0)
ans.add(new ArrayList<>(path));
return ans;
}
else {
// Subsequence without including
// the element at current index
printSubsequences(arr, index + 1, path, ans);
path.add(arr[index]);
// Subsequence including the element
// at current index
printSubsequences(arr, index + 1, path, ans);
// Backtrack to remove the recently
// inserted element
path.remove(path.size() - 1);
}
return ans;
}
static int findSmallestDifference(int A[], int B[], int m, int n) {
// Sort both arrays
// using sort function
Arrays.sort(A);
Arrays.sort(B);
int a = 0, b = 0;
// Initialize result as max value
int result = Integer.MAX_VALUE;
// Scan Both Arrays upto
// sizeof of the Arrays
while (a < m && b < n) {
if (Math.abs(A[a] - B[b]) < result)
result = Math.abs(A[a] - B[b]);
// Move Smaller Value
if (A[a] < B[b])
a++;
else
b++;
}
// return final sma result
return result;
}
static long bruteforce(int k, int n, int[] a) {
long max = LMIN;
int l = -1, r = -1;
for (int i = max(n - 500, 0); i >= 0; i--) {
for (int j = i - 1; j >= 1; j--) {
long x = (i + 1) * (j + 1) - (k * (a[i] | a[j]));
if (max < x) {
l = i + 1;
r = j + 1;
max = x;
}
}
}
// pn(l + " " + r + " " + max);
return max;
}
static long mycode(int k, int n, long[] a) {
long max = LMIN;
int l = -1, r = -1;
for (int i = 1; i < n; i++) {
// for (int j = i + 1; j < n; j++) {
// long x = i * (i + 1) - k * (a[i - 1] | a[i]);
long x = i * (i + 1) - k * (a[i - 1] | a[i]);
if (x >= max) {
l = max(l, i);
r = max(r, i + 1);
max = x;
}
// }
}
pn(l + " " + r + " " + max);
return max;
}
private static final long LMAX = 9223372036854775807L;
private static final long LMIN = -9223372036854775808L;
private static boolean arr[] = sieve(1000001);
private static ArrayList<Long> primes = new ArrayList<>();
private static ArrayList<Long> sumList = new ArrayList<>();
private static ArrayList<Integer> divisors = new ArrayList<>();
private static ArrayList<Long> divisorsL = new ArrayList<>();
private static ArrayList<Long> primefactorization = new ArrayList<>();
private static ArrayList<Integer> primedivisors = new ArrayList<>();
private static ArrayList<Integer> perfectSquares = new ArrayList<>();
private static int freq[] = new int[200005];
private static ArrayList<Integer> primefactorsofN = new ArrayList<>();;
// Pair<Integer, Integer> pair;
private static void pn(Object o) {
pw.println(o);
}
private static void p(Object o) {
pw.print(o);
}
private static Random rand = new Random();
// math util
private static int minof(int a, int b, int c) {
return min(a, min(b, c));
}
private static int countDistinct(int arr[], int k) {
int max = 0;
// Creates an empty hashMap hM
HashMap<Integer, Integer> hM = new HashMap<Integer, Integer>();
// Traverse the first window and store count
// of every element in hash map
for (int i = 0; i < k; i++)
hM.put(arr[i], hM.getOrDefault(arr[i], 0) + 1);
// Print count of first window
max = max(max, hM.size());
// Traverse through the remaining array
for (int i = k; i < arr.length; i++) {
// Remove first element of previous window
// If there was only one occurrence
if (hM.get(arr[i - k]) == 1) {
hM.remove(arr[i - k]);
}
else // reduce count of the removed element
hM.put(arr[i - k], hM.get(arr[i - k]) - 1);
// Add new element of current window
// If this element appears first time,
// set its count as 1,
hM.put(arr[i], hM.getOrDefault(arr[i], 0) + 1);
// Print count of current window
max = max(max, hM.size());
}
return max;
}
private static int minof(int... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return min(x[0], x[1]);
if (x.length == 3)
return min(x[0], min(x[1], x[2]));
int min = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] < min)
min = x[i];
return min;
}
private static void permuteHelper(ArrayList<ArrayList<Integer>> list, ArrayList<Integer> resultList, int[] arr) {
// Base case
if (resultList.size() == arr.length) {
list.add(new ArrayList<>(resultList));
} else {
for (int i = 0; i < arr.length; i++) {
if (resultList.contains(arr[i])) {
// If element already exists in the list then skip
continue;
}
// Choose element
resultList.add(arr[i]);
// Explore
permuteHelper(list, resultList, arr);
// Unchoose element
resultList.remove(resultList.size() - 1);
}
}
}
private static String getSmallestAndLargest(String s, int k) {
String smallest = "";
String largest = "";
String arr[] = new String[s.length() - k + 1];
for (int i = 0; i < s.length() - k + 1; i++) {
arr[i] = s.substring(i, i + k);
}
for (int j = 0; j < arr.length - 1; j++) {
for (int g = 0; g < arr.length - 1; g++) {
String p = arr[g];
String q = arr[g + 1];
boolean t = true;
String temp = "";
for (int w = 0; w < k; w++) {
char pkaCharacter = p.charAt(w);
int b = (int) pkaCharacter;
char qkaCharacter = q.charAt(w);
int c = (int) qkaCharacter;
if (b == c)
continue;
else if (b > c) {
t = false;
break;
} else if (b < c) {
t = true;
break;
}
}
if (t == false) {
temp = arr[g + 1];
arr[g + 1] = arr[g];
arr[g] = temp;
} else {
continue;
}
}
}
smallest += arr[0];
largest += arr[arr.length - 1];
return smallest;
// pn(largest);
}
private static long getClosest(long val1, long val2, long target) {
if (target - val1 >= val2 - target)
return val2;
else
return val1;
}
private static long minof(long a, long b, long c) {
return min(a, min(b, c));
}
private static long minof(long... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return min(x[0], x[1]);
if (x.length == 3)
return min(x[0], min(x[1], x[2]));
long min = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] < min)
min = x[i];
return min;
}
private static int maxof(int a, int b, int c) {
return max(a, max(b, c));
}
private static int maxof(int... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return max(x[0], x[1]);
if (x.length == 3)
return max(x[0], max(x[1], x[2]));
int max = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] > max)
max = x[i];
return max;
}
private static long maxof(long a, long b, long c) {
return max(a, max(b, c));
}
private static long maxof(long... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return max(x[0], x[1]);
if (x.length == 3)
return max(x[0], max(x[1], x[2]));
long max = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] > max)
max = x[i];
return max;
}
private static int fli(double d) {
return (int) d;
}
private static int cei(double d) {
return (int) Math.ceil(d);
}
private static long fll(double d) {
return (long) d;
}
private static long cel(double d) {
return (long) Math.ceil(d);
}
private static int gcf(int a, int b) {
return b == 0 ? a : gcf(b, a % b);
}
private static long gcf(long a, long b) {
return b == 0 ? a : gcf(b, a % b);
}
private static int lcm(int a, int b) {
return a * b / gcf(a, b);
}
private static long lcm(long a, long b) {
return a * b / gcf(a, b);
}
private static int randInt(int min, int max) {
return rand.nextInt(max - min + 1) + min;
}
private static long mix(long x) {
x += 0x9e3779b97f4a7c15L;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebL;
return x ^ (x >> 31);
}
// array util
private static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
private static void reverse(int[] a, int n, int k) {
if (k > n) {
System.out.println("Invalid k");
return;
}
// One by one reverse first
// and last elements of a[0..k-1]
for (int i = 0; i < k / 2; i++) {
int tempswap = a[i];
a[i] = a[k - i - 1];
a[k - i - 1] = tempswap;
}
}
private static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
private static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
private static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
private static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
private static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
private static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
private static void sortList(ArrayList<Integer> al) {
Collections.sort(al);
}
static void reverseList(ArrayList<Integer> al) {
Collections.reverse(al);
}
// static void sortList(ArrayList<Pair<Integer,Integer>>al)
// {Collections.sort(al);}
private static void sort(int[] a) {
shuffle(a);
Arrays.parallelSort(a);
}
private static void sort(long[] a) {
shuffle(a);
Arrays.parallelSort(a);
}
private static void sort(double[] a) {
shuffle(a);
Arrays.parallelSort(a);
}
private static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
private static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
private static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
private static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
// graph util
private static List<List<Integer>> g(int n) {
List<List<Integer>> g = new ArrayList<>();
for (int i = 0; i < n; ++i)
g.add(new ArrayList<>());
return g;
}
private static List<Set<Integer>> sg(int n) {
List<Set<Integer>> g = new ArrayList<>();
for (int i = 0; i < n; ++i)
g.add(new HashSet<>());
return g;
}
private static void c(List<? extends Collection<Integer>> g, int u, int v) {
g.get(u).add(v);
g.get(v).add(u);
}
private static void cto(List<? extends Collection<Integer>> g, int u, int v) {
g.get(u).add(v);
}
private static void dc(List<? extends Collection<Integer>> g, int u, int v) {
g.get(u).remove(v);
g.get(v).remove(u);
}
private static void dcto(List<? extends Collection<Integer>> g, int u, int v) {
g.get(u).remove(v);
}
// output
private static void pn(int i) {
pw.println(i);
}
private static void pn(long l) {
pw.println(l);
}
private static void prln(long l) {
pw.println(l);
}
private static void pr(double d) {
pw.print(d);
}
private static void prln(double d) {
pw.println(d);
}
private static void pr(char c) {
pw.print(c);
}
private static void prln(char c) {
pw.println(c);
}
private static void pr(char[] s) {
pw.print(new String(s));
}
private static void prln(char[] s) {
pw.println(new String(s));
}
private static void pr(String s) {
pw.print(s);
}
private static void prln(String s) {
pw.println(s);
}
private static void pr(Object o) {
pw.print(o);
}
private static void prln(Object o) {
pw.println(o);
}
private static void prln() {
pw.println();
}
private static void pryes() {
prln("yes");
}
private static void pry() {
prln("Yes");
}
private static void pY() {
prln("YES");
}
private static void prno() {
prln("no");
}
private static void prn() {
prln("No");
}
private static void pN() {
prln("NO");
}
private static void pryesno(boolean b) {
prln(b ? "yes" : "no");
};
private static void pryn(boolean b) {
prln(b ? "Yes" : "No");
}
private static void prYN(boolean b) {
prln(b ? "YES" : "NO");
}
private static void prln(int... a) {
for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i)
;
if (a.length > 0)
prln(a[a.length - 1]);
else
prln();
}
private static void prln(long... a) {
for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i)
;
if (a.length > 0)
prln(a[a.length - 1]);
else
prln();
}
private static void prln(double... a) {
for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i)
;
if (a.length > 0)
prln(a[a.length - 1]);
else
prln();
}
private static <T> void prln(Collection<T> c) {
int n = c.size() - 1;
Iterator<T> iter = c.iterator();
for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i)
;
if (n >= 0)
prln(iter.next());
else
prln();
}
private static void h() {
prln("hlfd");
flush();
}
private static void flush() {
pw.flush();
}
private static void close() {
pw.close();
}
private static int uniquePairs(int a[], int n) {
HashSet<Integer> visited1 = new HashSet<Integer>();
// un[i] stores number of unique elements
// from un[i + 1] to un[n - 1]
int un[] = new int[n];
// Last element will have no unique elements
// after it
un[n - 1] = 0;
// To count unique elements after every a[i]
int count = 0;
for (int i = n - 1; i > 0; i--) {
// If current element has already been used
// i.e. not unique
if (visited1.contains(a[i]))
un[i - 1] = count;
else
un[i - 1] = ++count;
// Set to true if a[i] is visited
visited1.add(a[i]);
}
HashSet<Integer> visited2 = new HashSet<Integer>();
// To know which a[i] is already visited
int answer = 0;
for (int i = 0; i < n - 1; i++) {
// If visited, then the pair would
// not be unique
if (visited2.contains(a[i]))
continue;
// Calculating total unqiue pairs
answer += un[i];
// Set to true if a[i] is visited
visited2.add(a[i]);
}
return answer;
}
private static int[] replaceAll(int[] a, int val, int newVal) {
for (int i = 0; i < a.length; i++)
if (a[i] == val)
a[i] = newVal;
return a;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
private static long factorial(int n) {
if (n <= 0)
return 1;
else
return (long) ((n * factorial(n - 1)) % mod);
}
private static long factorial(long n) {
if (n <= 0)
return 1L;
else
return (long) ((n * factorial(n - 1)) % mod);
}
private static void watch(int[] a) {
for (int e : a)
p(e + " ");
pn("");
}
private static void watch(double[] a) {
for (double e : a)
p(e + " ");
pn("");
}
private static void watch(long[] a) {
for (long e : a)
p(e + " ");
pn("");
}
private static void watchList(ArrayList<Integer> al) {
for (int e : al)
p(e + " ");
pn("");
}
private static void watchListString(ArrayList<String> al) {
for (String e : al)
p(e + " ");
pn("");
}
private static void watchListD(ArrayList<Double> al) {
for (double e : al)
p(e + " ");
pn("");
}
private static void watchListOfArrays(ArrayList<int[]> al) {
for (int[] e : al)
watch(e);
}
private static void watchLongList(ArrayList<Long> al) {
for (long e : al)
p(e + " ");
pn("");
}
private static void watchSet(Set<Integer> set) {
for (int e : set)
p(e + " ");
pn("");
}
private static void watchSetOfArrays(Set<int[]> set) {
for (int[] e : set)
watch(e);
}
private static Set<Integer> putSet(int[] a) {
Set<Integer> set = new TreeSet<>();
for (int e : a)
set.add(e);
return set;
}
private static Set<Long> putSet(long[] a) {
Set<Long> set = new TreeSet<>();
for (long e : a)
set.add(e);
return set;
}
private static int bs(int[] a, int x) {
int start = 0, end = a.length - 1;
while (start <= end) {
int mid = start + ((end - start) / 2);
if (a[mid] == x)
return mid;
else if (a[mid] < x)
start = mid + 1;
else
end = mid - 1;
}
return -1;
}
private static String newString(String s, int k) {
// new string
String X = "";
// Remove characters until
// the string is empty
while (s.length() > 0) {
char temp = s.charAt(0);
// Traverse to find the smallest character in the
// first k characters
for (int i = 1; i < k && i < s.length(); i++) {
if (s.charAt(i) < temp) {
temp = s.charAt(i);
}
}
// append the smallest character
X = X + temp;
// removing the lexicographically smallest
// character from the string
for (int i = 0; i < k; i++) {
if (s.charAt(i) == temp) {
s = s.substring(0, i) + s.substring(i + 1);
// s.erase(s.begin() + i);
break;
}
}
}
return X;
}
private static boolean exist(int[] a, int x) {
for (int e : a) {
if (e == x)
return true;
}
return false;
}
private static int digitsum(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
n /= 10;
sum += digit;
}
return sum;
}
private static long digitsum(long n) {
long sum = 0;
while (n > 0) {
long digit = n % 10;
n /= 10;
sum += digit;
}
return sum;
}
private static int countPalindromicSubsequence(String str) {
int N = str.length();
// create a 2D array to store the count
// of palindromic subsequence
int[][] cps = new int[N][N];
// palindromic subsequence of length 1
for (int i = 0; i < N; i++)
cps[i][i] = 1;
// check subsequence of length L is
// palindrome or not
for (int L = 2; L <= N; L++) {
for (int i = 0; i <= N - L; i++) {
int k = L + i - 1;
if (str.charAt(i) == str.charAt(k)) {
cps[i][k] = cps[i][k - 1] + cps[i + 1][k] + 1;
} else {
cps[i][k] = cps[i][k - 1] + cps[i + 1][k] - cps[i + 1][k - 1];
}
}
}
// return total palindromic subsequence
return cps[0][N - 1];
}
private static int LCSubStr(char X[], char Y[], int m, int n) {
// Create a table to store
// lengths of longest common
// suffixes of substrings.
// Note that LCSuff[i][j]
// contains length of longest
// common suffix of
// X[0..i-1] and Y[0..j-1].
// The first row and first
// column entries have no
// logical meaning, they are
// used only for simplicity of program
int LCStuff[][] = new int[m + 1][n + 1];
// To store length of the longest
// common substring
int result = 0;
// Following steps build
// LCSuff[m+1][n+1] in bottom up fashion
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
LCStuff[i][j] = 0;
else if (X[i - 1] == Y[j - 1]) {
LCStuff[i][j] = LCStuff[i - 1][j - 1] + 1;
result = Integer.max(result, LCStuff[i][j]);
} else
LCStuff[i][j] = 0;
}
}
return result;
}
private static List<String> subStrings(String s) {
ArrayList<String> al = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
for (int j = i + 1; j <= s.length(); j++) {
al.add(s.substring(i, j));
}
}
return al;
}
private static boolean isSubSequence(String str1, String str2, int m, int n) {
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If last characters of two strings are matching
if (str1.charAt(m - 1) == str2.charAt(n - 1))
return isSubSequence(str1, str2, m - 1, n - 1);
// If last characters are not matching
return isSubSequence(str1, str2, m, n - 1);
}
private static boolean number_Is_Divisible_By_Its_Digits(long n) {
long temp = n;
while (temp > 0) {
long digit = temp % 10;
temp /= 10;
if (digit != 0 && n % digit != 0)
return false;
}
return true;
}
private static int countDivisors(int n) {
int cnt = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0)
cnt++;
divisors.add(i);
}
return cnt;
}
private static ArrayList<Integer> divisorsList(int n) {
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (!divisors.contains(i))
divisors.add(i);
if (!divisors.contains(n / i))
divisors.add(n / i);
}
}
return divisors;
}
private static ArrayList<Long> divisorsList(long n) {
for (long i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (!divisorsL.contains(i))
divisorsL.add(i);
if (!divisorsL.contains(n / i))
divisorsL.add(n / i);
}
}
return divisorsL;
}
private static ArrayList<Integer> primeFactorList(int n) {
for (int i = 2; i <= n; i++) {
if ((n % i) == 0 && primedivisors.contains(i) == false)
primedivisors.add(i);
}
return primedivisors;
}
private static boolean isStringSorted(String s) {
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) > s.charAt(i + 1))
return false;
}
return true;
}
private static boolean isPalindromeString(String s) {
String temp = "";
for (int i = s.length() - 1; i >= 0; i--) {
temp += s.charAt(i) + "";
}
return ((s.equals(temp)) ? true : false);
}
private static boolean isPalindrome(int[] a) {
for (int i = 0; i < a.length / 2; i++) {
if (a[i] != a[a.length - 1 - i])
return false;
}
return true;
}
private static boolean isPalindrome(ArrayList<Integer> al) {
for (int i = 0; i < al.size() / 2; i++) {
if (al.get(i) != al.get(al.size() - 1 - i))
return false;
}
return true;
}
private static boolean isSorted(int[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
if (a[i - 1] > a[i])
return false;
}
return true;
}
private static boolean isSortedStrictly(int[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
if (a[i - 1] >= a[i])
return false;
}
return true;
}
private static boolean isSorted(long[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
if (a[i - 1] > a[i])
return false;
}
return true;
}
private static boolean isSortedStrictly(long[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
if (a[i - 1] >= a[i])
return false;
}
return true;
}
private static boolean solution(int n, int x) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
n /= 10;
sum += digit;
}
return (sum == x) ? true : false;
}
private static ArrayList<Integer> perfectSquares(int n) {
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
al.add(i * i);
}
return al;
}
private static boolean checkDuplicateAll(String s) {
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(i - 1))
return false;
}
return true;
}
private static String sortString(String inputString) {
// convert input string to Character array
Character tempArray[] = new Character[inputString.length()];
for (int i = 0; i < inputString.length(); i++) {
tempArray[i] = inputString.charAt(i);
}
// Sort, ignoring case during sorting
Arrays.sort(tempArray, new Comparator<Character>() {
@Override
public int compare(Character c1, Character c2) {
// ignoring case
return Character.compare(Character.toLowerCase(c1), Character.toLowerCase(c2));
}
});
// using StringBuilder to convert Character array to String
StringBuilder sb = new StringBuilder(tempArray.length);
for (Character c : tempArray)
sb.append(c.charValue());
return sb.toString();
}
private static String reverseString(String s, int x) {
String ans = "", temp = s.substring(0, x);
for (int i = x; i < s.length(); i++) {
ans = s.charAt(i) + ans;
}
return temp + ans;
}
private static boolean number_Is_Not_Divisible_By_Its_Digits(int num) {
String s = num + "";
if (s.contains("0"))
return false;
int temp = num;
while (num > 0) {
int digit = num % 10;
num /= 10;
if (temp % digit == 0)
return false;
}
return true;
}
private static boolean palindrome(int n) {
int temp = n, rev = 0;
while (n > 0) {
int digit = n % 10;
n /= 10;
rev = rev * 10 + digit;
}
return (temp == rev) ? true : false;
}
private static boolean checkparanthesis(String s)
{
Stack<Character> st = new Stack<>();
int n = s.length();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == '(')
st.push('(');
else {
if (st.size() == 0)
return false;
else
st.pop();
}
}
return (st.size() == 0) ? true : false;
}
private static boolean[] sieve(int n) {
boolean[] isPrime = new boolean[n + 1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
return isPrime;
}
private static int[][] graph(int from[], int to[], int n) {
int g[][] = new int[n][];
int cnt[] = new int[n];
for (int i = 0; i < from.length; i++) {
cnt[from[i]]++;
cnt[to[i]]++;
}
for (int i = 0; i < n; i++) {
g[i] = new int[cnt[i]];
}
Arrays.fill(cnt, 0);
for (int i = 0; i < from.length; i++) {
g[from[i]][cnt[from[i]]++] = to[i];
g[to[i]][cnt[to[i]]++] = from[i];
}
return g;
}
private static String rev(String s) {
StringBuilder sb = new StringBuilder(s);
sb.reverse();
return sb.toString();
}
private static long gcd(long x, long y) {
if (y == 0) {
return x;
} else {
return gcd(y, x % y);
}
}
private static int gcd(int x, int y) {
if (y == 0) {
return x;
} else {
return gcd(y, x % y);
}
}
private static int abs(int a, int b) {
return (int) Math.abs(a - b);
}
private static int abs(int a) {
return (int) Math.abs(a);
}
private static long abs(long a) {
return (long) Math.abs(a);
}
private static long abs(long a, long b) {
return (long) Math.abs(a - b);
}
private static void swap(int a, int b) {
int c = b;
b = a;
a = c;
}
private static void swap(long a, long b) {
long c = b;
b = a;
a = c;
}
private static int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
private static int min(int a, int b) {
if (a > b) {
return b;
} else {
return a;
}
}
private static long max(long a, long b) {
if (a > b) {
return a;
} else {
return b;
}
}
private static long min(long a, long b) {
if (a > b) {
return b;
} else {
return a;
}
}
private static long pow(long n, long p, long m) {
long result = 1;
if (p == 0) {
return 1;
}
while (p != 0) {
if (p % 2 == 1) {
result *= n;
}
if (result >= m) {
result %= m;
}
p >>= 1;
n *= n;
if (n >= m) {
n %= m;
}
}
return result;
}
private static int pow(int base, int exp) {
int result = 1;
while (exp != 0) {
if ((exp & 1) == 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
private static long pow(long n, long m) {
long mod = 1000000007;
if (m == 0)
return 1;
if (m == 1)
return n;
if (m % 2 == 0) {
long ans = pow(n, m / 2);
return (ans * ans) % mod;
} else {
long ans = pow(n, ((m - 1) / 2));
return ((n * ans) % mod * ans) % mod;
}
}
private static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
private static class Pair<U, V> implements Comparable<Pair<U, V>> {
public final U first;
public V second;
public <U, V> Pair<U, V> makePair(U first, V second) {
return new Pair<U, V>(first, second);
}
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return !(first != null ? !first.equals(pair.first) : pair.first != null)
&& !(second != null ? !second.equals(pair.second) : pair.second != null);
}
@Override
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (second != null ? second.hashCode() : 0);
return result;
}
public Pair<V, U> swap() {
return makePair(second, first);
}
@Override
public String toString() {
return "(" + first + "," + second + ")";
}
@SuppressWarnings({ "unchecked" })
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) first).compareTo(o.first);
if (value != 0) {
return value;
}
return ((Comparable<V>) second).compareTo(o.second);
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 7408e4696b6fceb521d85a234e5df12d | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
public class AB_Balance {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
sc.nextLine();
while(t-->0){
String s= sc.nextLine();
String ans="";
int n=s.length();
char first=s.charAt(0);
char last=s.charAt(n-1);
if(first==last){
System.out.println(s);
}
else{
System.out.println(last+s.substring(1, n-1)+last);
}
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 00560b69a1cb85a77eb38996ca6519c3 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class test {
public static ArrayList<ArrayList<int[]>> count (int n,String s){
ArrayList<int[]> posAB= new ArrayList<>();
ArrayList<int[]> posBA= new ArrayList<>();
String si="";
int[] arr = new int[2];
for (int i = 0; i < n; i++) {
if (si.equals("a") && (s.charAt(i)+"").equals("b")) {
arr[0]++;
int[] pos = new int [2];
pos[0]=i-1;
pos[1]=i;
posAB.add(pos);
}
if (si.equals("b") && (s.charAt(i)+"").equals("a")){
arr[1]++;
int[] pos = new int [2];
pos[0]=i-1;
pos[1]=i;
posBA.add(pos);
};
si=s.charAt(i)+"";
}
ArrayList<ArrayList<int[]>> out = new ArrayList<>();
ArrayList<int[]> a = new ArrayList<>();
a.add(arr);
out.add(a);
out.add(posAB);
out.add(posBA);
return out;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
String s ="";
int n =0;
while(N>=0){
s=in.nextLine();
n=s.length();
StringBuilder str = new StringBuilder(s);
if(str.length()>1)
{
if(count(n, s).get(0).get(0)[0]!=count(n, s).get(0).get(0)[1]){
str.setCharAt(0, 'a');
str.setCharAt(str.length()-1, 'a');
s=str.toString();}}
System.out.println(s);
// solve(n, s);
N--;
}
in.close();
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | c13a9b2d8302dd48ec5936570f24c270 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class test {
public static ArrayList<ArrayList<int[]>> count (int n,String s){
ArrayList<int[]> posAB= new ArrayList<>();
ArrayList<int[]> posBA= new ArrayList<>();
String si="";
int[] arr = new int[2];
for (int i = 0; i < n; i++) {
if (si.equals("a") && (s.charAt(i)+"").equals("b")) {
arr[0]++;
int[] pos = new int [2];
pos[0]=i-1;
pos[1]=i;
posAB.add(pos);
}
if (si.equals("b") && (s.charAt(i)+"").equals("a")){
arr[1]++;
int[] pos = new int [2];
pos[0]=i-1;
pos[1]=i;
posBA.add(pos);
};
si=s.charAt(i)+"";
}
ArrayList<ArrayList<int[]>> out = new ArrayList<>();
ArrayList<int[]> a = new ArrayList<>();
a.add(arr);
out.add(a);
out.add(posAB);
out.add(posBA);
return out;
}
public static Boolean solve(int n,String s){
if(count(n, s).get(0).get(0)[1]==count(n, s).get(0).get(0)[0] || (count(n, s).get(0).get(0)[0]==0&&count(n, s).get(0).get(0)[1]==0 ) ){
System.out.println(s);
return true;
}
ArrayList<ArrayList<int[]>> count = count(n, s);
ArrayList<int[]> posAB= count.get(1);
ArrayList<int[]> posBA= count.get(2);
int AB=count.get(0).get(0)[0];
int BA=count.get(0).get(0)[1];
if(posAB.size()>0){
StringBuilder str = new StringBuilder(s);
int[] a =posAB.get(0);
str.setCharAt(a[0], 'b');
s = str.toString();
if(solve(n, s)){
return true;}
else{
str.setCharAt(a[0], 'a');
str.setCharAt(a[1], 'a');
s = str.toString();
if(solve(n, s)){
return true; }
else{
str.setCharAt(a[1], 'b');
s = str.toString();
}
}
posAB.remove(0);
}
if(posBA.size()>0){
int[] a =posBA.get(0);
StringBuilder str = new StringBuilder(s);
str.setCharAt(a[0], 'a');
s = str.toString();
if(solve(n, s)){
return true;}
else{
str.setCharAt(a[0], 'b');
str.setCharAt(a[1], 'b');
s = str.toString();
if(solve(n, s)){
return true; }
else{
str.setCharAt(a[1], 'a');
s = str.toString();
}
}
posBA.remove(0);
}
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
String s ="";
int n =0;
while(N>=0){
s=in.nextLine();
n=s.length();
StringBuilder str = new StringBuilder(s);
if(str.length()>1)
{
if(count(n, s).get(0).get(0)[0]!=count(n, s).get(0).get(0)[1]){
str.setCharAt(0, 'a');
str.setCharAt(str.length()-1, 'a');
s=str.toString();}}
System.out.println(s);
// solve(n, s);
N--;
}
in.close();
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 572e7143eb58f56c9aa8a8300a1fd6d7 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class test {
public static ArrayList<ArrayList<int[]>> count (int n,String s){
ArrayList<int[]> posAB= new ArrayList<>();
ArrayList<int[]> posBA= new ArrayList<>();
String si="";
int[] arr = new int[2];
for (int i = 0; i < n; i++) {
if (si.equals("a") && (s.charAt(i)+"").equals("b")) {
arr[0]++;
int[] pos = new int [2];
pos[0]=i-1;
pos[1]=i;
posAB.add(pos);
}
if (si.equals("b") && (s.charAt(i)+"").equals("a")){
arr[1]++;
int[] pos = new int [2];
pos[0]=i-1;
pos[1]=i;
posBA.add(pos);
};
si=s.charAt(i)+"";
}
ArrayList<ArrayList<int[]>> out = new ArrayList<>();
ArrayList<int[]> a = new ArrayList<>();
a.add(arr);
out.add(a);
out.add(posAB);
out.add(posBA);
return out;
}
public static Boolean solve(int n,String s){
if(count(n, s).get(0).get(0)[1]==count(n, s).get(0).get(0)[0] || (count(n, s).get(0).get(0)[0]==0&&count(n, s).get(0).get(0)[1]==0 ) ){
System.out.println(s);
return true;
}
ArrayList<ArrayList<int[]>> count = count(n, s);
ArrayList<int[]> posAB= count.get(1);
ArrayList<int[]> posBA= count.get(2);
int AB=count.get(0).get(0)[0];
int BA=count.get(0).get(0)[1];
if(posAB.size()>0){
StringBuilder str = new StringBuilder(s);
int[] a =posAB.get(0);
str.setCharAt(a[0], 'b');
s = str.toString();
if(solve(n, s)){
return true;}
else{
str.setCharAt(a[0], 'a');
str.setCharAt(a[1], 'a');
s = str.toString();
if(solve(n, s)){
return true; }
else{
str.setCharAt(a[1], 'b');
s = str.toString();
}
}
posAB.remove(0);
}
if(posBA.size()>0){
int[] a =posBA.get(0);
StringBuilder str = new StringBuilder(s);
str.setCharAt(a[0], 'a');
s = str.toString();
if(solve(n, s)){
return true;}
else{
str.setCharAt(a[0], 'b');
str.setCharAt(a[1], 'b');
s = str.toString();
if(solve(n, s)){
return true; }
else{
str.setCharAt(a[1], 'a');
s = str.toString();
}
}
posBA.remove(0);
}
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
String s ="";
int n =0;
while(N>=0){
s=in.nextLine();
n=s.length();
StringBuilder str = new StringBuilder(s);
if(str.length()>1)
{
if(count(n, s).get(0).get(0)[0]!=count(n, s).get(0).get(0)[1]){
str.setCharAt(0, 'a');
str.setCharAt(str.length()-1, 'a');
s=str.toString();}}
solve(n, s);
N--;
}
in.close();
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 0859d17d16673e990d0ca3e6f9bb6dd1 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.lang.*;
import static java.lang.Math.*;
// Sachin_2961 submission //
public class Codeforces {
static void solve(){
char[]str = fs.n().toCharArray();
if(str[0] != str[str.length-1]){
str[0] = str[str.length-1];
}
for(char c:str)
out.print(c);
out.println();
}
static class Pair{
int value,ind;
Pair(int value,int ind){
this.value = value;
this.ind = ind;
}
}
static boolean multipleTestCase = true;
static FastScanner fs;
static PrintWriter out;
public static void main(String[]args){
try{
out = new PrintWriter(System.out);
fs = new FastScanner();
int tc = multipleTestCase?fs.nInt():1;
while (tc-->0)solve();
out.flush();
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String n() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String Line()
{
String str = "";
try
{
str = br.readLine();
}catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int nInt() {return Integer.parseInt(n()); }
long nLong() {return Long.parseLong(n());}
double nDouble(){return Double.parseDouble(n());}
int[]aI(int n){
int[]ar = new int[n];
for(int i=0;i<n;i++)
ar[i] = nInt();
return ar;
}
}
public static void sort(int[] arr){
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static void sort(long[] arr){
ArrayList<Long> ls = new ArrayList<>();
for(long x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 9b53e88d87d3b1a45678c3a555891bbe | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
public class Con_116_Div_2_A_AB_Balance {
// public static boolean isEqual (String temp){
// int ab_count = 0;
// int ba_count = 0;
//
// for(int i = 0; i < temp.length(); i++){
//
// for(int j = i + 1; j <= temp.length(); j++){
//
// if(temp.substring(i, j).equals("ab")){
// ab_count++;
// }
// if(temp.substring(i, j).equals("ba")){
// ba_count++;
// }
// }
// }
// return ab_count == ba_count;
// }
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int test = 0; test < t; test ++) {
// Optimized Solution
String s = sc.next();
char[] arr = s.toCharArray();
if(arr[0] != arr[arr.length - 1]){
arr[0] = arr[arr.length - 1];
String changed_string = String.copyValueOf(arr);
System.out.println(changed_string);
}
else {
System.out.println(s);
}
// String original_string = sc.next();
//
// String temp = original_string;
//
// if(isEqual(temp)){
// System.out.println(temp);
// }
// else{
//
// for(int i = 0; i < temp.length(); i++){
//
// if(temp.charAt(i) == 'a'){
// temp = temp.substring(0,i) + "b" + temp.substring(i+1);
//
// if(isEqual(temp)){
// System.out.println(temp);
// break;
// }
// else {
// temp = original_string;
// }
// }
// else if(temp.charAt(i) == 'b'){
//
// temp = temp.substring(0,i) + "a" + temp.substring(i+1);
//
// if(isEqual(temp)){
// System.out.println(temp);
// break;
// }
// else {
// temp = original_string;
// }
// }
// }
// }
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 4013c6e85bd0a6ba668f632bebdb21d5 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
public class Con_116_Div_2_A_AB_Balance {
public static boolean isEqual (String temp){
int ab_count = 0;
int ba_count = 0;
for(int i = 0; i < temp.length(); i++){
for(int j = i + 1; j <= temp.length(); j++){
if(temp.substring(i, j).equals("ab")){
ab_count++;
}
if(temp.substring(i, j).equals("ba")){
ba_count++;
}
}
}
return ab_count == ba_count;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int test = 0; test < t; test ++) {
String original_string = sc.next();
String temp = original_string;
if(isEqual(temp)){
System.out.println(temp);
}
else{
for(int i = 0; i < temp.length(); i++){
if(temp.charAt(i) == 'a'){
temp = temp.substring(0,i) + "b" + temp.substring(i+1);
if(isEqual(temp)){
System.out.println(temp);
break;
}
else {
temp = original_string;
}
}
else if(temp.charAt(i) == 'b'){
temp = temp.substring(0,i) + "a" + temp.substring(i+1);
if(isEqual(temp)){
System.out.println(temp);
break;
}
else {
temp = original_string;
}
}
}
}
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | a66c739ebaab8bf11fd42691cf7459f3 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
public class Con_116_Div_2_A_AB_Balance {
public static boolean isEqual (String temp){
int ab_count = 0;
int ba_count = 0;
for(int i = 0; i < temp.length(); i++){
for(int j = i + 1; j <= temp.length(); j++){
if(temp.substring(i, j).equals("ab")){
ab_count++;
}
if(temp.substring(i, j).equals("ba")){
ba_count++;
}
}
}
return ab_count == ba_count;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int test = 0; test < t; test ++)
{
String original_string = sc.next();
String temp = original_string;
if(isEqual(temp)){
System.out.println(temp);
}
else{
for(int i = 0; i < temp.length(); i++){
if(temp.charAt(i) == 'a'){
temp = temp.substring(0,i) + "b" + temp.substring(i+1);
if(isEqual(temp)){
System.out.println(temp);
break;
}
else {
temp = original_string;
}
}
else if(temp.charAt(i) == 'b'){
temp = temp.substring(0,i) + "a" + temp.substring(i+1);
if(isEqual(temp)){
System.out.println(temp);
break;
}
else {
temp = original_string;
}
}
}
}
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 02f16be45531a245a45c138cc8f6ded4 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
public class Con_116_Div_2_A_AB_Balance {
public static boolean isEqual (String temp){
int ab_count = 0;
int ba_count = 0;
for(int i = 0; i < temp.length(); i++){
for(int j = i + 1; j <= temp.length(); j++){
if(temp.substring(i, j).equals("ab")){
ab_count++;
}
if(temp.substring(i, j).equals("ba")){
ba_count++;
}
}
}
return ab_count == ba_count;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int test = 0; test < t; test ++)
{
String s = sc.next();
String temp = s;
if(isEqual(temp)){
System.out.println(temp);
}
else{
for(int i = 0; i < temp.length(); i++){
if(temp.charAt(i) == 'a'){
temp = temp.substring(0,i) + "b" + temp.substring(i+1);
if(isEqual(temp)){
System.out.println(temp);
break;
}
else {
temp = s;
}
}
else if(temp.charAt(i) == 'b'){
temp = temp.substring(0,i) + "a" + temp.substring(i+1);
if(isEqual(temp)){
System.out.println(temp);
break;
}
else {
temp = s;
}
}
}
}
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 623f030891663d0edbd293620b44d40a | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
public class abbalance {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
StringBuilder s = new StringBuilder();
s.append(sc.next());
if(s.charAt(0) != s.charAt(s.length()-1)) {
s.setCharAt(0,s.charAt(s.length()-1));
System.out.println(s);
}
else {
System.out.println(s);
}
}
sc.close();
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | dcbc00ef8ad01c5aaf7bf11b2290d971 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.jar.JarEntry;
public class TaskA {
public static char reverse(char a){
if(a=='a') return 'b';
else return 'a';
}
public static String getString(String s){
StringBuilder ans = new StringBuilder();
ans.append(s);
ans.setCharAt(0,reverse(s.charAt(0)));
return ans.toString();
}
public static int count(String s,String t){
int cnt = 0;
for(int i=0;i<s.length()-1;i++){
if(s.charAt(i)==t.charAt(0)&&s.charAt(i+1)==t.charAt(1)) cnt++;
}
return cnt;
}
public static void solve(FastReader in,PrintWriter out,int nTestCase) {
String a = in.next();
int abCnt = count(a,"ab");
int baCnt = count(a,"ba");
if(abCnt==baCnt){
out.println(a);
return;
}
String ans = "";
ans = getString(a);
out.println(ans);
}
public static void main(String[] args) {
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int T = 1;
T = in.nextInt();
for(int tt=1;tt<=T;tt++) {
solve(in,out,tt);
}
out.close();
}
static final Random random=new Random();
static void fastSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
int temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void fastSort(char[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
char temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static class Pair implements Comparable<Pair>{
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair o) {
if(this.a==o.a)
return (int)(this.b - o.b);
return (int)(this.a - o.a);
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | e3562ed531b7ca017a4908d642d2f660 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.*;
import java.util.*;
public class q1 {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void solve() throws Exception {
StringBuilder str = new StringBuilder(br.readLine());
str.setCharAt(0,str.charAt(str.length() - 1));
System.out.println(str);
}
public static void main(String[] args) throws Exception {
int tests = Integer.parseInt(br.readLine());
for (int test = 1; test <= tests; test++) {
solve();
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 7e7d3f01c1e5670f9ada136ae8f36178 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.*;
import java.util.*;
public class q1 {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void solve() throws Exception {
StringBuilder str = new StringBuilder(br.readLine());
int c = 1;
for(int i = 0;i < str.length() - 1;i++){
if(str.charAt(i + 1) != str.charAt(i)) c++;
}
if(c % 2 == 1){
System.out.println(str);
return;
}
if(str.charAt(0) == 'a') str.setCharAt(0,'b');
else str.setCharAt(0,'a');
System.out.println(str);
}
public static void main(String[] args) throws Exception {
int tests = Integer.parseInt(br.readLine());
for (int test = 1; test <= tests; test++) {
solve();
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | a5b9bba9dbf51f6c71aae82b4fa5c9cd | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
public class codeforcesround {
public static Scanner read = new Scanner(System.in);
public static void main(String[] args) {
int t = Integer.parseInt(read.nextLine().trim());
while (t-- > 0) {
solve();
}
}
private static void solve() {
String s = read.nextLine();
int abN = 0;
int baN = 0;
if (s.length() < 2) {
System.out.println(s);
return;
}
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i-1) == 'a' && s.charAt(i) == 'b')
abN++;
else if (s.charAt(i-1) == 'b' && s.charAt(i) == 'a')
baN++;
}
if (baN==abN)
{
System.out.println(s);
return;
}
else if (baN-abN==1) {
s = s.substring(0,s.length()-1);
s+="b";
System.out.println(s);
}
else {
s = s.substring(0,s.length()-1);
s+="a";
System.out.println(s);
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 01c1f8fc54606e73e15ad27a7d21ec75 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class B {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader sc = new FastReader();
int t= sc.nextInt();
while(t--!=0) {
String str= sc.next();
int ab=0;
int ba=0;
if(str.charAt(0)== str.charAt(str.length()-1))System.out.println(str);
else {
char[] arr = str.toCharArray();
arr[str.length()-1]= str.charAt(0);
for(int i=0;i<str.length();i++) {
System.out.print(arr[i]);
}
System.out.println();
}
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | abf970325c7822ed3123b803deca2ed7 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
public class file {
public static void main(String []args) {
Scanner sc= new Scanner(System.in);
int num = sc.nextInt()+1;
String str[] = new String[num];
for (int i = 0; i < num; i++) {
String input = sc.nextLine();
str[i] = input;
}
for (int i = 0 ; i < num; i++) {
System.out.println(balancedStr(str[i]));
}
}
private static String balancedStr(String s) {
String original = s;
if (AB(s) == BA(s)) return s;
for (int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) == 'b') {
s = replaceAt(original,i,'a');
} else {
s = replaceAt(original,i,'b');
}
if (AB(s) == BA(s)) {
return s;
}
}
return null;
}
private static int AB(String s) {
int count = 0;
for (int i = 0; i < s.length()-1; i++) {
if (s.charAt(i)== 'a' && s.charAt(i+1) == 'b') {
count++;
}
}
return count;
}
private static int BA(String s) {
int count = 0;
for (int i = 0; i < s.length()-1; i++) {
if (s.charAt(i)== 'b' && s.charAt(i+1) == 'a') {
count++;
}
}
return count;
}
private static String replaceAt(String s, int k, char c) {
String result = "";
for (int i = 0; i < s.length(); i++) {
if (i == k) {
result += c;
} else {
result += s.charAt(i);
}
}
return result;
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 17cccb5275b1356281360abb3a96fcc3 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
/**
*
* @author eslam
*/
public class ABBalance {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException {
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader input = new FastReader();
int t = input.nextInt();
for (int i = 0; i < t; i++) {
String w = input.next();
log.write(w.substring(0, w.length()-1)+w.charAt(0)+"\n");
}
log.flush();
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 262b5394ca4e29de9100f505d76dbde9 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
/**
*
* @author eslam
*/
public class ABBalance {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for (int i = 0; i < t; i++) {
String w = input.next();
System.out.println(w.substring(0, w.length()-1)+w.charAt(0));
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 4e01f6706e853b81a8908eee4620b967 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
/**
*
* @author eslam
*/
public class ABBalance {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for (int i = 0; i < t; i++) {
String w = input.next();
if(w.charAt(0)=='b'){
System.out.println(w.substring(0, w.length()-1)+'b');
}else{
System.out.println(w.substring(0, w.length()-1)+'a');
}
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | b031aa52b8cbdbd5cfe7295391389fe4 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
/**
*
* @author eslam
*/
public class JavaApplication778 {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException {
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader input = new FastReader();
int t = input.nextInt();
for (int i = 0; i < t; i++) {
String w = input.next();
int ab = 0;
int ba = 0;
for (int j = 0; j < w.length(); j++) {
if (w.charAt(j) == 'a') {
while (j < w.length()) {
if (w.charAt(j) == 'b') {
ab++;
j--;
break;
}
j++;
}
} else if (w.charAt(j) == 'b') {
while (j < w.length()) {
if (w.charAt(j) == 'a') {
ba++;
j--;
break;
}
j++;
}
}
}
if (ab == ba) {
log.write(w + "\n");
} else if (ab < ba) {
log.write(w.substring(0, w.length()-1)+"b"+"\n");
} else {
log.write(w.substring(0, w.length()-1)+"a"+"\n");
}
}
log.flush();
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | d5371bb68e048993a027c7fcc61196e2 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9 + 7;
// static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
while(t-->0)
{
char arr[] = sc.next().toCharArray();
int n = arr.length;
int ab = 0 , ba = 0;
for(int i=0;i<n-1;i++)
{
if(arr[i] == 'a' && arr[i+1] == 'b')
{
ab++;
}
else if(arr[i] == 'b' && arr[i+1] == 'a')
{
ba++;
}
}
if(ab == ba)
{
for(char x:arr)
{
System.out.print(x);
}
System.out.println();
}
else
{
if(ab > ba)
{
int limit = ab - ba;
for(int i=0;i<limit;i++)
{
for(int j=0;j<n-1;j++)
{
if(arr[j]=='a' && arr[j]=='b' || arr[j]=='a' && arr[j]=='a')
{
arr[j] = 'b';
break;
}
}
}
for(char x:arr)
{
System.out.print(x);
}
System.out.println();
}
else
{
int limit = ba - ab;
for(int i=0;i<limit;i++)
{
for(int j=0;j<n-1;j++)
{
if(arr[j]=='b' && arr[j]=='a' || arr[j]=='b' && arr[j]=='b')
{
arr[j] = 'a';
break;
}
}
}
for(char x:arr)
{
System.out.print(x);
}
System.out.println();
}
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<String, Integer> > list =
new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(char arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(char arr[], int n)
{
int i;
char temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static void rightRotate(char arr[], int d, int n)
{
for (int i = 0; i < d; i++)
rightRotatebyOne(arr, n);
}
static void rightRotatebyOne(char arr[], int n)
{
int i;
char temp = arr[n - 1];
for (i = n - 1; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(long[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
}
class Pair
{
int first , second;
Pair(int first , int second)
{
this.first = first;
this.second = second;
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 0f1541b8fa51c88d85b985e5163cc591 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
while(t-->0)
{
char a[]=sc.next().toCharArray();
int ac=0,bc=0;
for(int i=0;i<a.length-1;i++)
{
if(a[i]=='a'&&a[i+1]=='b')
{
ac++;
}
else if(a[i]=='b'&&a[i+1]=='a')
{
bc++;
}
}
if(ac==bc)
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
System.out.println();
continue;
}
else if(ac>bc)
{
int m=ac-bc;
for(int i=0;i<m;i++)
{
for(int j=0;j<a.length-1;j++)
{
if(a[j]=='a'&&a[j+1]=='b'||a[j]=='a'&&a[j+1]=='a')
{
a[j]='b';
break;
}
}
}
}
else
{
int m=bc-ac;
for(int i=0;i<m;i++)
{
for(int j=0;j<a.length-1;j++)
{
if(a[j]=='b'&&a[j+1]=='a'||a[j]=='b'&&a[j+1]=='b')
{
a[j]='a';
break;
}
}
}
}
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
System.out.println();
}
}
static int findfrequencies(int a[],int n)
{
int count=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==n)
{
count++;
}
}
return count;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static int[] EvenOddArragement(int a[])
{
ArrayList<Integer> list=new ArrayList<>();
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
list.add(a[i]);
}
}
for(int i=0;i<a.length;i++)
{
if(a[i]%2!=0)
{
list.add(a[i]);
}
}
for(int i=0;i<a.length;i++)
{
a[i]=list.get(i);
}
return a;
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int DigitSum(int n)
{
int r=0,sum=0;
while(n>=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
return sum;
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 535402f0e8f8df1a58d96330ba4bd0a3 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.InterruptedIOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import javax.swing.plaf.synth.SynthOptionPaneUI;
public class Main {
private static long gcd(long a, long b)
{
while (b > 0)
{
long temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
private static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}
static long binpow(long a,long b) {
long M=1000000007;
long res=1;
while(b>0) {
if(b%2==1) {
res=(res*a)%M;
}
a=(a*a)%M;
b/=2;
}
return res;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc1=new Scanner(System.in);
int t=sc1.nextInt();
while(t-->0) {
String s = sc1.next();
if(s.length()==1) {
System.out.println(s);
}
else {
char a[] = s.toCharArray();
int cnt = 0;
int cnt1 = 0;
for(int i =0 ; i<a.length-1 ; i++) {
if(s.charAt(i) == 'a' && s.charAt(i+1) == 'b') {
cnt++;
}
else if(s.charAt(i) == 'b' && s.charAt(i+1) == 'a') {
cnt1++;
}
}
if(cnt>cnt1) {
int diff = cnt - cnt1;
for(int i = s.length() - 1 ; i>0 && diff>0 ; i--) {
if(a[i] == 'b' && a[i-1] == 'a')
{
a[i] = 'a';
diff--;
}
else if(a[i] == 'b' && a[i-1] == 'b') {
a[i] = 'a';
diff--;
}
}
}
else if(cnt1>cnt) {
int diff = cnt1 - cnt;
for(int i = s.length() - 1 ; i>0 && diff>0 ; i--) {
if(a[i] == 'a' && a[i-1] == 'b')
{
a[i] = 'b';
diff--;
}
else if(a[i] == 'a' && a[i-1] == 'a') {
a[i] = 'b';
diff--;
}
}
}
for(int i = 0 ; i<a.length ; i++) {
System.out.print(a[i] +"");
}
System.out.println();
}
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | c96503d68fb53090939bed5c2a4d32bb | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.InterruptedIOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import javax.swing.plaf.synth.SynthOptionPaneUI;
public class Main {
private static long gcd(long a, long b)
{
while (b > 0)
{
long temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
private static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}
static long binpow(long a,long b) {
long M=1000000007;
long res=1;
while(b>0) {
if(b%2==1) {
res=(res*a)%M;
}
a=(a*a)%M;
b/=2;
}
return res;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc1=new Scanner(System.in);
int t=sc1.nextInt();
while(t-->0) {
String s=sc1.next();
char ch[]=s.toCharArray();
if(ch.length==1)
System.out.println(ch);
else {
if(ch[0]!=ch[ch.length-1]) {
ch[ch.length-1]=ch[0];
System.out.println(ch);
}
else {
System.out.println(ch);
}
}
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 568e332e84fa72ca9239c52a05d3464c | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0){
String s=scn.next();
s=s.substring(0, s.length()-1) + s.charAt(0);
System.out.println(s);
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 516e3aaa1885867cceff1ac30612d367 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;
import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;
import java.security.AccessControlException;import java.util.ArrayList;import java.util.List;
public class _A {static public void main(final String[] args) throws IOException
{A._main(args);}
static class A extends Solver{public A(){}static public void _main(String[]args)throws
IOException{new A().run();}@Override public void solve()throws IOException{int[]s=sc.nextLine().chars().toArray();
ArrayList<Integer>chunks=new ArrayList<>();int cur='a';int start='a';chunks.add(0);
for(int i=0;i<s.length;i++){if(s[i]==cur){chunks.set(chunks.size()-1,chunks.get(chunks.size()
-1)+1);}else{cur=s[i];chunks.add(1);}}if(chunks.get(0)==0){chunks.remove(0);start
='b';}if(chunks.size()%2==0){if(chunks.get(0)==1){chunks.set(1,chunks.get(1)+1);
chunks.remove(0);start=start=='a'?'b':'a';}else if(chunks.get(chunks.size()-1)==
1){chunks.set(chunks.size()-2,chunks.get(chunks.size()-2)+1);chunks.remove(chunks.size()
-1);}else{chunks.set(chunks.size()-1,chunks.get(chunks.size()-1)-1);chunks.add(1);
}}cur=start;for(int i=0;i<chunks.size();i++){pw.print(String.format("%"+chunks.get(i)
+"s","").replace(' ',(char)cur));cur=cur=='a'?'b':'a';}pw.println();}}static class
MyScanner{private StringBuilder cache=new StringBuilder();int cache_pos=0;private
int first_linebreak=-1;private int second_linebreak=-1;private StringBuilder sb=
new StringBuilder();private InputStream is=null;public MyScanner(final InputStream
is){this.is=is;}public String charToString(final int c){return String.format("'%s'",
c=='\n'?"\\n":(c=='\r'?"\\r":String.valueOf((char)c)));}public int get(){int res
=-1;if(cache_pos<cache.length()){res=cache.charAt(cache_pos);cache_pos++;if(cache_pos
==cache.length()){cache.delete(0,cache_pos);cache_pos=0;}}else{try{res=is.read();
}catch(IOException ex){throw new RuntimeException(ex);}}return res;}private void
unget(final int c){if(cache_pos==0){cache.insert(0,(char)c);}else{cache_pos--;}}
public String nextLine(){sb.delete(0,sb.length());int c;boolean done=false;boolean
end=false;while((c=get())!=-1){if(check_linebreak(c)){done=true;if(c==first_linebreak)
{if(!end){end=true;}else{cache.append((char)c);break;}}else if(second_linebreak!=
-1 && c==second_linebreak){break;}}if(end && c!=first_linebreak && c!=second_linebreak)
{cache.append((char)c);break;}if(!done){sb.append((char)c);}}return!done && sb.length()
==0?null:sb.toString();}private boolean check_linebreak(int c){if(c=='\n'|| c=='\r')
{if(first_linebreak==-1){first_linebreak=c;}else if(c!=first_linebreak && second_linebreak
==-1){second_linebreak=c;}return true;}return false;}public int nextInt(){return
Integer.parseInt(next());}public long nextLong(){return Long.parseLong(next());}
public boolean hasNext(){boolean res=false;int c;while((c=get())!=-1){if(!check_linebreak(c)
&& c!=' '&& c!='\t'){res=true;unget(c);break;}}return res;}public String next(){
sb.delete(0,sb.length());boolean started=false;int c;while((c=get())!=-1){if(check_linebreak(c)
|| c==' '|| c=='\t'){if(started){unget(c);break;}}else{started=true;sb.append((char)c);
}}return sb.toString();}public int nextChar(){return get();}public boolean eof()
{int c=get();boolean res=false;if(c!=-1){unget(c);}else{res=true;}return res;}public
double nextDouble(){return Double.parseDouble(next());}}static abstract class Solver
{protected String nameIn=null;protected String nameOut=null;protected boolean singleTest
=false;protected MyScanner sc=null;protected PrintWriter pw=null;private int current_test
=0;private int count_tests=0;protected int currentTest(){return current_test;}protected
int countTests(){return count_tests;}private void process()throws IOException{if(!singleTest)
{count_tests=sc.nextInt();sc.nextLine();for(current_test=1;current_test<=count_tests;
current_test++){solve();pw.flush();}}else{count_tests=1;current_test=1;solve();}
}abstract protected void solve()throws IOException;public void run()throws IOException
{boolean done=false;try{if(nameIn!=null){if(new File(nameIn).exists()){try(FileInputStream
fis=new FileInputStream(nameIn);PrintWriter pw0=select_output();){select_output();
done=true;sc=new MyScanner(fis);pw=pw0;process();}}else{throw new RuntimeException("File "
+new File(nameIn).getAbsolutePath()+" does not exist!");}}}catch(IOException | AccessControlException
ex){}if(!done){try(PrintWriter pw0=select_output();){sc=new MyScanner(System.in);
pw=pw0;process();}}}private PrintWriter select_output()throws FileNotFoundException
{if(nameOut!=null){return new PrintWriter(nameOut);}return new PrintWriter(System.out);
}}static abstract class Tester{static public int getRandomInt(final int min,final
int max){return(min+(int)Math.floor(Math.random()*(max-min+1)));}static public long
getRandomLong(final long min,final long max){return(min+(long)Math.floor(Math.random()
*(max-min+1)));}static public double getRandomDouble(final double min,final double
maxExclusive){return(min+Math.random()*(maxExclusive-min));}abstract protected void
testOutput(final List<String>output_data);abstract protected void generateInput();
abstract protected String inputDataToString();private boolean break_tester=false;
protected void beforeTesting(){}protected void breakTester(){break_tester=true;}
public boolean broken(){return break_tester;}}}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 0717822ca6d287de8ece162a7e9e41bb | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.StringTokenizer;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;
public class p1
{
BufferedReader br;
StringTokenizer st;
BufferedWriter bw;
public static void main(String[] args)throws Exception
{
new p1().run();
}
void run()throws IOException
{
br = new BufferedReader(new InputStreamReader(System.in));
bw=new BufferedWriter(new OutputStreamWriter(System.out));
solve();
}
void solve() throws IOException
{
short t=ns();
while(t-->0)
{
char c[]=nac();
int n=c.length;
int count=0,a;
a=c[0]-97;
for(int i=-1;++i<n;)
{
if(c[i]-97!=a)
{
a^=1;
count++;
}
}
if(count%2==1)
{
c[n-1]-=97;
c[n-1]^=1;
c[n-1]+=97;
}
for(int i=-1;++i<n;)
bw.write(c[i]);
bw.write("\n");;
}
bw.flush();
}
public int gcd(int a, int b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
int[] nai(int n) { int a[]=new int[n]; for(int i=-1;++i<n;)a[i]=ni(); return a;}
long[] nal(int n) { long a[]=new long[n]; for(int i=-1;++i<n;)a[i]=nl(); return a;}
char[] nac() {char c[]=nextLine().toCharArray(); return c;}
char [][] nmc(int n) {char c[][]=new char[n][]; for(int i=-1;++i<n;)c[i]=nac(); return c;}
int[][] nmi(int r, int c) {int a[][]=new int[r][c]; for(int i=-1;++i<r;)a[i]=nai(c); return a;}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
byte nb() { return Byte.parseByte(next()); }
short ns() { return Short.parseShort(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public int[] primeNO(long limit)
{
int size=(int)Math.sqrt(limit)+1;
int size1=size/2;
boolean composite[]=new boolean[size1];
int zz=(int)Math.sqrt(size-1);
for(int i=3;i<=zz;)
{
for(int j=(i*i)/2;j<size1;j+=i)
composite[j]=true;
for(i+=2;composite[i/2];i+=2);
}
int prime[]=new int[3401];
prime[0]=2;
int p=1;
for(int i=1;i<size1;i++)
{
if(!composite[i])
prime[p++]=2*i+1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// System.out.println(p);
////////////////////////////////////////////////////////////////////////////////////////////////////////
prime=Arrays.copyOf(prime, p);
return prime;
}
public static class queue
{
public static class node
{
node next;
int val;
public node(int val)
{
this.val=val;
}
}
node head,tail;
public void add(int val)
{
node a=new node(val);
if(head==null)
{
head=tail=a;
return;
}
tail.next=a;
tail=a;
}
public int remove()
{
int a=head.val;
head=head.next;
if(head==null)
tail=null;
return a;
}
}
public static class stack
{
public static class node
{
node next;
int val;
public node(int val)
{
this.val=val;
}
}
node head;
public void add(int val)
{
node a=new node(val);
a.next=head;
head=a;
}
public int remove()
{
int a=head.val;
head=head.next;
return a;
}
}
public static class data
{
int a,b;
public data(int a, int b)
{
this.a=a;
this.b=b;
}
}
public data[] dataArray(int n)
{
data d[]=new data[n];
for(int i=-1;++i<n;)
d[i]=new data(ni(), ni());
return d;
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 4fd9a871d95dc76c89c42bd45cbddf3e | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.StringTokenizer;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;
public class p1
{
BufferedReader br;
StringTokenizer st;
BufferedWriter bw;
public static void main(String[] args)throws Exception
{
new p1().run();
}
void run()throws IOException
{
br = new BufferedReader(new InputStreamReader(System.in));
bw=new BufferedWriter(new OutputStreamWriter(System.out));
solve();
}
void solve() throws IOException
{
short t=ns();
while(t-->0)
{
char c[]=nac();
int n=c.length;
int count=0,a;
a=c[0]-97;
for(int i=-1;++i<n;)
{
if(c[i]-97!=a)
{
a^=1;
count++;
}
}
if(count%2==0)
{
for(int i=-1;++i<n;)
System.out.print(c[i]);
System.out.println();
}
else
{
c[n-1]-=97;
c[n-1]^=1;
c[n-1]+=97;
for(int i=-1;++i<n;)
System.out.print(c[i]);
System.out.println();
}
}
}
public int gcd(int a, int b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
int[] nai(int n) { int a[]=new int[n]; for(int i=-1;++i<n;)a[i]=ni(); return a;}
long[] nal(int n) { long a[]=new long[n]; for(int i=-1;++i<n;)a[i]=nl(); return a;}
char[] nac() {char c[]=nextLine().toCharArray(); return c;}
char [][] nmc(int n) {char c[][]=new char[n][]; for(int i=-1;++i<n;)c[i]=nac(); return c;}
int[][] nmi(int r, int c) {int a[][]=new int[r][c]; for(int i=-1;++i<r;)a[i]=nai(c); return a;}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
byte nb() { return Byte.parseByte(next()); }
short ns() { return Short.parseShort(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public int[] primeNO(long limit)
{
int size=(int)Math.sqrt(limit)+1;
int size1=size/2;
boolean composite[]=new boolean[size1];
int zz=(int)Math.sqrt(size-1);
for(int i=3;i<=zz;)
{
for(int j=(i*i)/2;j<size1;j+=i)
composite[j]=true;
for(i+=2;composite[i/2];i+=2);
}
int prime[]=new int[3401];
prime[0]=2;
int p=1;
for(int i=1;i<size1;i++)
{
if(!composite[i])
prime[p++]=2*i+1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// System.out.println(p);
////////////////////////////////////////////////////////////////////////////////////////////////////////
prime=Arrays.copyOf(prime, p);
return prime;
}
public static class queue
{
public static class node
{
node next;
int val;
public node(int val)
{
this.val=val;
}
}
node head,tail;
public void add(int val)
{
node a=new node(val);
if(head==null)
{
head=tail=a;
return;
}
tail.next=a;
tail=a;
}
public int remove()
{
int a=head.val;
head=head.next;
if(head==null)
tail=null;
return a;
}
}
public static class stack
{
public static class node
{
node next;
int val;
public node(int val)
{
this.val=val;
}
}
node head;
public void add(int val)
{
node a=new node(val);
a.next=head;
head=a;
}
public int remove()
{
int a=head.val;
head=head.next;
return a;
}
}
public static class data
{
int a,b;
public data(int a, int b)
{
this.a=a;
this.b=b;
}
}
public data[] dataArray(int n)
{
data d[]=new data[n];
for(int i=-1;++i<n;)
d[i]=new data(ni(), ni());
return d;
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 49e21e2b3f2276c147a8d52ce2f1aade | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | //package com.company;
import java.util.Scanner;
public class A {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
while(n-- > 0) {
String s = scanner.next();
StringBuilder ans = new StringBuilder(s);
char temp = ans.charAt(0);
ans.setCharAt(ans.length() - 1, temp);
System.out.println(ans);
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 072b4d1ebf55d6bb79a87f5d7810d30e | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes |
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Long.parseLong;
//Make it Divisible by 25
//-00
//-25
//-50
//-75
public class Main {
public static void main(String[] args) throws IOException, NumberFormatException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String s = br.readLine();
char ch[] = s.toCharArray();
if(ch[0]!=ch[s.length()-1]){
ch[0] =ch[s.length()-1];
}
for(char c:ch){
System.out.print(c);
}
System.out.println();
}
}
// ------------------------------------------swap----------------------------------------------------------------------
static void swap(int arr[],int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
//-------------------------------------------seiveOfEratosthenes----------------------------------------------------
static boolean prime[];
static void sieveOfEratosthenes(int n)
{
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
prime= new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
// for (int i = 2; i <= n; i++)
// {
// if (prime[i] == true)
// System.out.print(i + " ");
// }
}
// ---------------------------------------- power------------------------------------------------------------------
public static long power(int a , int b) {
if (b == 0) {
return 1;
} else if (b == 1) {
return a;
} else {
long R = power(a, b / 2);
if (b % 2 != 0) {
return (((power(a, b / 2))) * a * ((power(a, b / 2))));
} else {
return ((power(a, b / 2))) * ((power(a, b / 2)));
}
}
}
//--------------------------------------lower bound----------------------------------------------------------
static int LowerBound(int a[], int x)
{ // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
//--------------------------------------------------------------------------------------------------------------
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 8fa48aa54695197af55eaea0e2bbd3ca | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class div2 {
static boolean a=false;
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
long t=sc.nextLong();
while(t-->0)
{
String s=sc.next();
int c1=0,c2=0;
for(int i=0;i<s.length()-1;i++)
{
if(s.charAt(i)=='a'&&s.charAt(i+1)=='b')
{
c1++;
}
else if(s.charAt(i)=='b'&&s.charAt(i+1)=='a')
{
c2++;
}
}
if(c1==c2)
{
System.out.println(s);
}
else
{
String ans="";
char c[]=s.toCharArray();
c[0]=s.charAt(0)=='a'?'b':'a';
for(int i=0;i<s.length();i++)
{
ans+=c[i];
}
System.out.println(ans);
}
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 8226f5e02317581d31911d6c5f8033d8 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes |
import java.util.*;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder str = new StringBuilder();
int t = sc.nextInt();
for (int xx = 0; xx < t; xx++) {
String s = sc.next();
int ab =0;
int ba =0;
for(int i=0;i<s.length()-1;i++)
{
if(s.charAt(i)=='a'&&s.charAt(i+1)=='b')
{
ab++;
}
else if(s.charAt(i)=='b'&&s.charAt(i+1)=='a')
{
ba++;
}
}
if(ab==ba)
{
str.append(s+"\n");
}
else
{
for(int i=0;i<s.length()-1;i++)
{
str.append(s.charAt(i));
}
if(ba>ab)
str.append('b');
else
str.append('a');
str.append("\n");
}
}
System.out.println(str);
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 91740ec7e928fc4fbe2c23fbb2af8a6e | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class _1606_A {
private static final FastScanner in = new FastScanner();
private static final PrintWriter out = new PrintWriter(System.out);
private static final PrintWriter err = new PrintWriter(System.err);
public static void solve() {
char[] chars = in.nextLine().toCharArray();
if (chars.length == 1) {
print(chars);
return;
}
int ab = 0;
int ba = 0;
for (int i = 0; i < chars.length-1; i++) {
if (chars[i] == 'a' && chars[i+1] == 'b') ab++;
if (chars[i] == 'b' && chars[i+1] == 'a') ba++;
}
if (ab > ba) {
for (int i = 0; i < chars.length-1; i++) {
// ^ab
// ab$
if (chars[i] == 'a' && chars[i+1] == 'b') {
if (i == 0) {
chars[i] = 'b';
ab--;
} else if (i+1 == chars.length-1) {
chars[i+1] = 'a';
ab--;
}
}
// aaabbb
if (chars[i] == 'a' && chars[i+1] == 'a') {
chars[i] = 'b';
ba++;
}
if (ab == ba) break;
}
} else if (ab < ba) {
for (int i = 0; i < chars.length-1; i++) {
// ^ba -> aa
// ba$ -> bb
if (chars[i] == 'b' && chars[i+1] == 'a') {
if (i == 0) {
chars[i] = 'a';
ba--;
} else if (i+1 == chars.length-1) {
chars[i + 1] = 'b';
}
}
// bbbaaa
if (chars[i] == 'b' && chars[i+1] == 'b') {
chars[i] = 'a';
ab++;
}
if (ab == ba) break;
}
}
print(chars);
}
private static void print(char[] chars) {
for (char c : chars) {
out.print(c);
}
out.println();
}
// Boilerplate
public static void main(String[] args) {
int t = in.nextInt();
while (t-- > 0) {
solve();
}
err.close();
out.close();
}
@SuppressWarnings("unused")
static class FastScanner {
StringTokenizer tok = new StringTokenizer("");
BufferedReader in;
FastScanner() {
in = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (!tok.hasMoreElements()) {
try {
tok = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tok.nextToken();
}
String nextLine() {
String line;
try {
line = in.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
List<Integer> nextIntegerList(int n) {
ArrayList<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
list.add(nextInt());
}
return list;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
List<Long> nextLongList(int n) {
ArrayList<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
list.add(nextLong());
}
return list;
}
ArrayList<Integer>[] nextGraph(int n, int m) {
@SuppressWarnings("unchecked")
ArrayList<Integer>[] adj = (ArrayList<Integer>[]) new ArrayList[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int v = nextInt();
int t = nextInt();
adj[v - 1].add(t - 1);
adj[t - 1].add(v - 1);
}
return adj;
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | e28e7666bf57cdd718ba8d6b43b9a721 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class codeforces_Edu116_A {
private static void solve(FastIOAdapter io) {
char[] c = io.next().toCharArray();
int ind = 0;
while (!check(c)) {
if (ind > 0) {
if (c[ind - 1] == 'a') {
c[ind - 1]++;
} else {
c[ind - 1]--;
}
}
if (c[ind] == 'a') {
c[ind]++;
} else {
c[ind]--;
}
ind++;
}
for (int i = 0; i < c.length; i++) {
io.out.print(c[i]);
}
io.out.println();
}
private static boolean check(char[] c) {
int ab = 0;
int ba = 0;
for (int i = 0; i < c.length - 1; i++) {
if (c[i] == 'a' && c[i + 1] == 'b') {
ab++;
}
if (c[i] == 'b' && c[i + 1] == 'a') {
ba++;
}
}
return ab == ba;
}
public static void main(String[] args) throws Exception {
try (FastIOAdapter ioAdapter = new FastIOAdapter()) {
int count = 1;
count = ioAdapter.nextInt();
while (count-- > 0) {
solve(ioAdapter);
}
}
}
static void ruffleSort(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static class FastIOAdapter implements AutoCloseable {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out))));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
@Override
public void close() throws Exception {
out.flush();
out.close();
br.close();
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 21b6437fc3a3c9b28858b02898a575af | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static int getAB(String msg) {
int count = 0;
for(int i = 0; i < msg.length()-1 ; i++) {
if(msg.charAt(i) =='a' && msg.charAt(i+1) =='b') {
count++;
}
}
return count;
}
static int getBA(String msg) {
int count = 0;
for(int i = 0; i < msg.length()-1 ; i++) {
if(msg.charAt(i) =='b' && msg.charAt(i+1) =='a') {
count++;
}
}
return count;
}
public static void main(String[] args) throws IOException{
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for(int i = 0 ; i < T ; i++) {
String line = br.readLine();
int AB = getAB(line);
int BA = getBA(line);
if(AB == BA) {
bw.append(line+"\n");
continue;
}
char first = line.charAt(0);
if(first == 'a') {
bw.append("b"+line.substring(1)+"\n");
}
else {
bw.append("a"+line.substring(1)+"\n");
}
}
bw.flush();
bw.close();
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 567199e24299b37668ab11a0fda28e00 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class ABBalance {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st;
private static String next() {
if(st == null|| !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch(IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
return st.nextToken();
}
private static int nextInt() {
return Integer.parseInt(next());
}
@SuppressWarnings("SpellCheckingInspection")
private static int gcdByEuclidsAlgorithm(int n1, int n2) {
if (n2 == 0) {
return n1;
}
return gcdByEuclidsAlgorithm(n2, n1 % n2);
}
public static void main(String[] args) throws IOException {
int i = nextInt();
for(int j = 0; j < i; ++j) {
String s = br.readLine();
char[] c = s.toCharArray();
if(c[0] != c[c.length - 1]) {
c[c.length - 1] = c[0];
}
System.out.println(new String(c));
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 6bb7883ecc9676ea1170082fc59f1430 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-- > 0 ){
String str = scanner.next();
if (str.charAt(0) == str.charAt(str.length() -1))
System.out.println(str);
else {
System.out.println(str.substring(0,str.length() -1 ) + str.charAt(0));
}
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 09866ae3aca16f1bab224b6671e20ead | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes |
import java.util.*;
import java.math.*;
//import java.io.*;
public class Experiment {
static Scanner in=new Scanner(System.in);
// static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
public static void sort(int ar[],int l,int r) {
if(l<r) {
int m=l+(r-l)/2;
sort(ar,l,m);
sort(ar,m+1,r);
merge(ar,l,m,r);
}
}
public static void merge(int ar[],int l,int m,int r) {
int n1=m-l+1;int n2=r-m;
int L[]=new int[n1];
int R[]=new int[n2];
for(int i=0;i<n1;i++)
L[i]=ar[l+i];
for(int i=0;i<n2;i++)
R[i]=ar[m+i+1];
int i=0;int j=0;int k=l;
while(i<n1 && j<n2) {
if(L[i]<=R[j]) {
ar[k++]=L[i++];
}else {
ar[k++]=R[j++];
}
}
while(i<n1)
ar[k++]=L[i++];
while(j<n2)
ar[k++]=R[j++];
}
public static int[] sort(int ar[]) {
sort(ar,0,ar.length-1);
//Array.sort uses O(n^2) in its worst case ,so better use merge sort
return ar;
}
public static void func(String s) {
int l=s.length();
if(l==1) {
System.out.println(s);return;
}
char ch=s.charAt(0);
char ch1=s.charAt(l-1);
if(ch==ch1)
System.out.println(s);
else
System.out.println('a'+s.substring(1,l-1)+'a');
}
public static void main(String[] args) {
int t=in.nextInt();
while(t!=0) {
//int n=in.nextInt();
String s=in.next();
//
func(s);
t--;
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 68b76e3eadca1d9403ef4878d76a453b | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main
{
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() { // reads in the next string
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() { // reads in the next int
return Integer.parseInt(next());
}
public long nextLong() { // reads in the next long
return Long.parseLong(next());
}
public double nextDouble() { // reads in the next double
return Double.parseDouble(next());
}
}
static InputReader r = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args)
{
int t = r.nextInt();
while (t > 0)
{
t--;
String s = r.next();
int n = s.length();
int ab = 0;
int ba = 0;
for (int i = 0; i < n - 1; i ++)
{
char c = s.charAt(i); char d = s.charAt(i+1);
if (c == 'a' && d == 'b')
{
ab ++;
}
else if (c == 'b' && d== 'a')
{
ba ++;
}
}
int c = ba;
ba-=Math.min(ab,ba);
ab-=Math.min(ab,c);
StringBuilder s1 = new StringBuilder();
/*for (int i = 0; i < n - 1; i ++)
{
int c1 = s.charAt(i); int d1 = s.charAt(i+1);
if (c1 == 'a' && d1 == 'b' && ab > 0)
{
ab --;
s1.append('b');
}
else if (c1 == 'b' && d1== 'a' && ba > 0)
{
ba --;
s1.append('a');
}
else
{
s1.append(s.charAt(i));
}
}*/
StringBuilder s2 = new StringBuilder();
if (s.charAt(0) == s.charAt(n-1))
{
pw.println(s);
}
else
{
s2.append(s.charAt(n-1));
for (int i = 1; i < n; i ++)
{
s2.append(s.charAt(i));
}
}
pw.println(s2);
}
pw.close();
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 66104b9c7a513ca718af332f33747780 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class NewTemplate {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws IOException{
FastReader Scanner = new FastReader();
// FOR GETTING THE TEST CASES
int testCases = Scanner.nextInt();
while(testCases-- > 0){
String s = Scanner.nextLine();
int ab = 0, ba = 0, len = s.length();
for(int i = 0; i < len - 1; i++){
if(s.charAt(i) == 'a' && s.charAt(i + 1) == 'b') ab++;
else if(s.charAt(i) == 'b' && s.charAt(i + 1) == 'a') ba++;
}
if(ab == ba) {
System.out.println(s);
continue;
}
System.out.print(s.substring(0, len - 1));
System.out.println(s.charAt(0));
}
}
private static void PrintArr(int[] array){
for(int i : array){
System.out.print(i + " ");
}
System.out.println();
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 2f6c1640fcf929b6f1c79f1aef7e8c2c | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class Main {
// Graph
// prefix sums
//inputs
public static void main(String args[])throws Exception{
Input sc=new Input();
precalculates p=new precalculates();
StringBuilder sb=new StringBuilder();
int t=sc.readInt();
for(int f=0;f<t;f++){
String str=sc.readString();
min=1000;
ss=new String(str);
if(equals(str)){
sb.append(str+"\n");
}else{
int dp[][]=new int[101][101];
for(int i=0;i<=100;i++){
Arrays.fill(dp[i],-1);
}
fun(str,str,0,0,dp);
sb.append(ss+"\n");
}
}
System.out.print(sb);
}
static String ss;
static int min;
public static int fun(String str,String orig,int i,int change,int dp[][]){
if(i>=orig.length()){
int v=change;
if(v<min && equals(str)){
ss=new String(str);
min=change;
return change;
}
return 1000;
}
if(dp[i][change]!=-1)
return dp[i][change];
boolean b=true;
if(str.charAt(i)=='a'){
b=false;
}else
b=true;
if(b)
return dp[i][change]=Math.min(fun(str.substring(0,i)+"a"+str.substring(i+1),orig,i+1,change+1,dp),fun(str,orig,i+1,change,dp));
else
return dp[i][change]=Math.min(fun(str.substring(0,i)+"b"+str.substring(i+1),orig,i+1,change+1,dp),fun(str,orig,i+1,change,dp));
}
public static int stringChanges(String s1,String s2){
int count=0;
for(int i=0;i<s1.length();i++){
if(s1.charAt(i)!=s2.charAt(i))
count++;
}
return count;
}
public static boolean equals(String s){
int ab=0;
int ba=0;
for(int i=1;i<s.length();i++){
if(s.charAt(i)=='b' && s.charAt(i-1)=='a')
ab++;
if(s.charAt(i)=='a' && s.charAt(i-1)=='b')
ba++;
}
if(ba==ab)
return true;
else
return false;
}
}
class Input{
BufferedReader br;
StringTokenizer st;
Input(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public int[] readArray() throws Exception{
st=new StringTokenizer(br.readLine());
int a[]=new int[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Integer.parseInt(st.nextToken());
}
return a;
}
public long[] readArrayLong() throws Exception{
st=new StringTokenizer(br.readLine());
long a[]=new long[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Long.parseLong(st.nextToken());
}
return a;
}
public int readInt() throws Exception{
st=new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
public long readLong() throws Exception{
st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());
}
public String readString() throws Exception{
return br.readLine();
}
public int[][] read2dArray(int n,int m)throws Exception{
int a[][]=new int[n][m];
for(int i=0;i<n;i++){
st=new StringTokenizer(br.readLine());
for(int j=0;j<m;j++){
a[i][j]=Integer.parseInt(st.nextToken());
}
}
return a;
}
}
class precalculates{
public int[] prefixSumOneDimentional(int a[]){
int n=a.length;
int dp[]=new int[n];
for(int i=0;i<n;i++){
if(i==0)
dp[i]=a[i];
else
dp[i]=dp[i-1]+a[i];
}
return dp;
}
public int[] postSumOneDimentional(int a[]) {
int n = a.length;
int dp[] = new int[n];
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1)
dp[i] = a[i];
else
dp[i] = dp[i + 1] + a[i];
}
return dp;
}
public int[][] prefixSum2d(int a[][]){
int n=a.length;int m=a[0].length;
int dp[][]=new int[n+1][m+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=a[i-1][j-1]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
}
}
return dp;
}
public long pow(long a,long b){
long mod=1000000007;
long ans=0;
if(b<=0)
return 1;
if(b%2==0){
ans=pow(a,b/2)%mod;
return ((ans%mod)*(ans%mod))%mod;
}else{
return ((a%mod)*(ans%mod))%mod;
}
}
}
class GraphInteger{
HashMap<Integer,vertex> vtces;
class vertex{
HashMap<Integer,Integer> children;
public vertex(){
children=new HashMap<>();
}
}
public GraphInteger(){
vtces=new HashMap<>();
}
public void addVertex(int a){
vtces.put(a,new vertex());
}
public void addEdge(int a,int b,int cost){
if(!vtces.containsKey(a)){
vtces.put(a,new vertex());
}
if(!vtces.containsKey(b)){
vtces.put(b,new vertex());
}
vtces.get(a).children.put(b,cost);
// vtces.get(b).children.put(a,cost);
}
public boolean isCyclicDirected(){
boolean isdone[]=new boolean[vtces.size()+1];
boolean check[]=new boolean[vtces.size()+1];
for(int i=1;i<=vtces.size();i++) {
if (!isdone[i] && isCyclicDirected(i,isdone, check)) {
return true;
}
}
return false;
}
private boolean isCyclicDirected(int i,boolean isdone[],boolean check[]){
if(check[i])
return true;
if(isdone[i])
return false;
check[i]=true;
isdone[i]=true;
Set<Integer> set=vtces.get(i).children.keySet();
for(Integer ii:set){
if(isCyclicDirected(ii,isdone,check))
return true;
}
check[i]=false;
return false;
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 344ed01f076fc13d26bcde68a920e887 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
for(int i=0;i<r;i++)
{
String t = sc.next();
char[] arr = t.toCharArray();
if (arr[0] != arr[t.length() - 1]) {
arr[0] = arr[t.length() - 1];
System.out.println(String.copyValueOf(arr));
} else {
System.out.println(t);
}
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 483a02118e8fe305deaab96e0f8a53c1 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Solution {
static PrintWriter out;
public static void main(String[] args) {
FastReader in = new FastReader();
out = new PrintWriter(System.out);
long t = in.nextLong();
long test = 1;
while (test <= t) {
out.println(solve(in));
//solve(in);
test++;
}
out.close();
}
private static String solve(FastReader in) {
String s = in.next();
if (s.charAt(0) == s.charAt(s.length() - 1)) return s;
return s.charAt(s.length() - 1) + s.substring(1);
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | b21674d5f60ef3d161d04fbf56052ecb | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static StringTokenizer tok;
static BufferedReader br;
static PrintWriter out;
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static String next() throws IOException {
while(tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(br.readLine());
}
return tok.nextToken();
}
static void solve() throws IOException{
String s;
s = next();
if(s.charAt(0)==s.charAt(s.length() - 1)) {
out.println(s);
}
else {
char arr[] = s.toCharArray();
int n = arr.length;
arr[n-1] = arr[0];
for(int i = 0; i < n; i++) {
out.print(arr[i]);
}
out.println();
}
}
public static void main(String[] args) {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int t = nextInt();
while(t-- > 0) {
solve();
}
br.close();
out.close();
}
catch(Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | aca6b7eb8cf88baddeaa43ae95bb129d | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
FastReader f = new FastReader();
int t = f.nextInt();
while (t-- > 0) {
String str = f.nextLine();
if (str.charAt(0) != str.charAt(str.length() - 1)) {
if (str.charAt(0) == 'a') {
System.out.println("b" + "" + str.substring(1, str.length()));
} else {
System.out.println("a" + "" + str.substring(1, str.length()));
}
} else {
System.out.println(str);
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | 1ff8b7f3edba60bb610018ed564ee756 | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class educational116A {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
long k = gcd(b, a % b);
return k;
}
public static void main(String[] args) {
FastReader scn = new FastReader();
try {
int t = scn.nextInt();
while (t-- > 0) {
String str = scn.nextLine();
if (str.length() == 1) {
System.out.println(str);
} else {
int countab = 0;
int countba = 0;
for (int i = 0; i < str.length() - 1; i++) {
if (str.substring(i, i + 2).equals("ab")) {
countab++;
} else if (str.substring(i, i + 2).equals("ba")) {
countba++;
}
}
int a = Math.abs(countba - countab);
if(countba == countab){
System.out.println(str);
}
else if(countab > countba){
String ans = str.substring(0, 2);
String lans = str.substring(str.length()-2);
if(ans.equals("ab")){
str = "bb" + str.substring(2);
System.out.println(str);
}
else if(lans.equals("ab")){
str = str.substring(0, str.length()-2) + "aa";
System.out.println(str);
}
else{
String p = str.substring(0,str.length()-1) + 'a';
System.out.println(p);
}
}
else{
String ans = str.substring(0, 2);
String lans = str.substring(str.length()-2);
if(ans.equals("ba")){
str = "aa" + str.substring(2);
System.out.println(str);
}
else if(lans.equals("ba")){
str = str.substring(0, str.length()-2) + "bb";
System.out.println(str);
}
else{
String p = str.substring(0,str.length()-1) + 'b';
System.out.println(p);
}
}
}
}
}
catch (Exception e){
return;
}
}
}
| Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output | |
PASSED | bbc1c103d1ce28636517f04cc467799a | train_108.jsonl | 1635518100 | You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa. | 256 megabytes | import java.util.*;
import java.util.Map;
public class Sol{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
char s[]=sc.next().toCharArray();
s[0]=s[s.length-1];
System.out.println(s);
}
}
} | Java | ["4\nb\naabbbabaa\nabbb\nabbaab"] | 2 seconds | ["b\naabbbabaa\nbbbb\nabbaaa"] | NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$. | Java 11 | standard input | [
"strings"
] | 351ffff1dfe1bc1762f062f612463759 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b. | 900 | For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.