knightL

SNWS A

Feb 5th, 2022
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. import java.io.BufferedOutputStream;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.InputMismatchException;
  10. import java.util.StringTokenizer;
  11.  
  12. public class AMain {
  13.  
  14.     private QuickReader in;
  15.     private PrintWriter out;
  16.  
  17.     public AMain(QuickReader in, PrintWriter out) {
  18.         this.in = in;
  19.         this.out = out;
  20.     }
  21.  
  22.     public static void main(String[] args) {
  23.         QuickReader in = new QuickReader(System.in);
  24.        
  25.         try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));)
  26.         {
  27.             new AMain(in, out).solve();
  28.         }
  29.     }
  30.    
  31.     private void solve() {
  32.         int a = in.nextInt();
  33.         int b = in.nextInt();
  34.         int n = in.nextInt();
  35.        
  36.         long[] mul = new long[n];
  37.         mul[0] = 1;
  38.         for(int i=1;i<n;i++)
  39.             mul[i] = mul[i-1] * a % b;
  40.        
  41.         char[]  s = in.next().toCharArray();
  42.         int[] snum = new int[n];
  43.         for(int i=0;i<n;i++)
  44.             snum[i] = s[n-1-i] -'a';
  45.        
  46.         int k = in.nextInt();
  47.        
  48.         int[] cur = snum.clone();
  49.         long curSum = 0;
  50.         int[] count = new int[b];
  51.        
  52.         long targetSum = -1;
  53.         for(int i=0;i<n;i++)
  54.             curSum = (curSum + cur[i] * mul[i])%b;
  55.        
  56.         while(true)
  57.         {
  58.             int intSum = (int) curSum;
  59.             count[intSum]++;
  60.             if(count[intSum] >= k)
  61.             {
  62.                 targetSum = intSum;
  63.                 break;
  64.             }
  65.             boolean carryDone = false;
  66.             for(int i=0;i<n && !carryDone;i++) {
  67.                 curSum = ((curSum - cur[i]*mul[i])%b + b)%b;
  68.                 if(cur[i] == 25)
  69.                     cur[i] = 0;
  70.                 else
  71.                 {
  72.                     cur[i]++;
  73.                     carryDone = true;
  74.                 }
  75.                 curSum = ((curSum + cur[i]*mul[i])%b + b)%b;
  76.             }
  77.             if(!carryDone)
  78.                 break;
  79.         }
  80.        
  81.         if(targetSum == -1)
  82.             out.println(-1);
  83.         else
  84.         {
  85.             cur = snum.clone();
  86.             curSum = 0;
  87.            
  88.             for(int i=0;i<n;i++)
  89.                 curSum = (curSum + cur[i] * mul[i])%b;
  90.            
  91.             ArrayList<String> res = new ArrayList<>();
  92.            
  93.             while(res.size()<k)
  94.             {
  95.                 if(curSum == targetSum)
  96.                     res.add(convert(cur));
  97.                
  98.                 boolean carryDone = false;
  99.                 for(int i=0;i<n && !carryDone;i++) {
  100.                     curSum = ((curSum - cur[i]*mul[i])%b + b)%b;
  101.                     if(cur[i] == 25)
  102.                         cur[i] = 0;
  103.                     else
  104.                     {
  105.                         cur[i]++;
  106.                         carryDone = true;
  107.                     }
  108.                     curSum = ((curSum + cur[i]*mul[i])%b + b)%b;
  109.                 }
  110.                 if(!carryDone)
  111.                     break;
  112.             }
  113.            
  114.             for(String r: res)
  115.                 out.println(r);
  116.            
  117.         }
  118.     }
  119.    
  120.     String convert(int[] x)
  121.     {
  122.         StringBuilder sb = new StringBuilder();
  123.         for(int i=x.length-1;i>=0;i--)
  124.             sb.append((char)(x[i]+'a'));
  125.         return sb.toString();
  126.     }
  127.    
  128.  
  129. }
  130.  
  131.  
  132. class QuickReader
  133. {
  134.     BufferedReader in;
  135.     StringTokenizer token;
  136.    
  137.     public QuickReader(InputStream ins)
  138.     {
  139.         in=new BufferedReader(new InputStreamReader(ins));
  140.         token=new StringTokenizer("");
  141.     }
  142.  
  143.     public boolean hasNext()
  144.     {
  145.         while (!token.hasMoreTokens())
  146.         {
  147.             try
  148.             {
  149.                 String s = in.readLine();
  150.                 if (s == null) return false;
  151.                 token = new StringTokenizer(s);
  152.             } catch (IOException e)
  153.             {
  154.                 throw new InputMismatchException();
  155.             }
  156.         }
  157.         return true;
  158.     }
  159.  
  160.     public String next()
  161.     {
  162.         hasNext();
  163.         return token.nextToken();
  164.     }
  165.  
  166.     public int nextInt()
  167.     {
  168.         return Integer.parseInt(next());
  169.     }
  170.  
  171.     public int[] nextInts(int n)
  172.     {
  173.         int[] res = new int[n];
  174.         for (int i = 0; i < n; i++)
  175.             res[i] = nextInt();
  176.         return res;
  177.     }
  178.  
  179.     public long nextLong() {
  180.         return Long.parseLong(next());
  181.     }
  182.    
  183.     public long[] nextLongs(int n)
  184.     {
  185.         long[] res = new long[n];
  186.         for (int i = 0; i < n; i++)
  187.             res[i] = nextLong();
  188.         return res;
  189.     }
  190.    
  191. }
  192.  
  193.  
Advertisement
Add Comment
Please, Sign In to add comment