Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.InputMismatchException;
- import java.util.StringTokenizer;
- public class AMain {
- private QuickReader in;
- private PrintWriter out;
- public AMain(QuickReader in, PrintWriter out) {
- this.in = in;
- this.out = out;
- }
- public static void main(String[] args) {
- QuickReader in = new QuickReader(System.in);
- try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));)
- {
- new AMain(in, out).solve();
- }
- }
- private void solve() {
- int a = in.nextInt();
- int b = in.nextInt();
- int n = in.nextInt();
- long[] mul = new long[n];
- mul[0] = 1;
- for(int i=1;i<n;i++)
- mul[i] = mul[i-1] * a % b;
- char[] s = in.next().toCharArray();
- int[] snum = new int[n];
- for(int i=0;i<n;i++)
- snum[i] = s[n-1-i] -'a';
- int k = in.nextInt();
- int[] cur = snum.clone();
- long curSum = 0;
- int[] count = new int[b];
- long targetSum = -1;
- for(int i=0;i<n;i++)
- curSum = (curSum + cur[i] * mul[i])%b;
- while(true)
- {
- int intSum = (int) curSum;
- count[intSum]++;
- if(count[intSum] >= k)
- {
- targetSum = intSum;
- break;
- }
- boolean carryDone = false;
- for(int i=0;i<n && !carryDone;i++) {
- curSum = ((curSum - cur[i]*mul[i])%b + b)%b;
- if(cur[i] == 25)
- cur[i] = 0;
- else
- {
- cur[i]++;
- carryDone = true;
- }
- curSum = ((curSum + cur[i]*mul[i])%b + b)%b;
- }
- if(!carryDone)
- break;
- }
- if(targetSum == -1)
- out.println(-1);
- else
- {
- cur = snum.clone();
- curSum = 0;
- for(int i=0;i<n;i++)
- curSum = (curSum + cur[i] * mul[i])%b;
- ArrayList<String> res = new ArrayList<>();
- while(res.size()<k)
- {
- if(curSum == targetSum)
- res.add(convert(cur));
- boolean carryDone = false;
- for(int i=0;i<n && !carryDone;i++) {
- curSum = ((curSum - cur[i]*mul[i])%b + b)%b;
- if(cur[i] == 25)
- cur[i] = 0;
- else
- {
- cur[i]++;
- carryDone = true;
- }
- curSum = ((curSum + cur[i]*mul[i])%b + b)%b;
- }
- if(!carryDone)
- break;
- }
- for(String r: res)
- out.println(r);
- }
- }
- String convert(int[] x)
- {
- StringBuilder sb = new StringBuilder();
- for(int i=x.length-1;i>=0;i--)
- sb.append((char)(x[i]+'a'));
- return sb.toString();
- }
- }
- class QuickReader
- {
- BufferedReader in;
- StringTokenizer token;
- public QuickReader(InputStream ins)
- {
- in=new BufferedReader(new InputStreamReader(ins));
- token=new StringTokenizer("");
- }
- public boolean hasNext()
- {
- while (!token.hasMoreTokens())
- {
- try
- {
- String s = in.readLine();
- if (s == null) return false;
- token = new StringTokenizer(s);
- } catch (IOException e)
- {
- throw new InputMismatchException();
- }
- }
- return true;
- }
- public String next()
- {
- hasNext();
- return token.nextToken();
- }
- public int nextInt()
- {
- return Integer.parseInt(next());
- }
- public int[] nextInts(int n)
- {
- int[] res = new int[n];
- for (int i = 0; i < n; i++)
- res[i] = nextInt();
- return res;
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public long[] nextLongs(int n)
- {
- long[] res = new long[n];
- for (int i = 0; i < n; i++)
- res[i] = nextLong();
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment