Advertisement
qwerty787788

SNSS R3 A

Aug 19th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Test {
  5.  
  6.     long mod = (long) 1e9 + 7;
  7.  
  8.     int MAX = 226;
  9.  
  10.     long[] getAns(int n, int m) {
  11.         ArrayList<Integer> okay = new ArrayList<>();
  12.         for (int st = 0; st < 1 << n; st++) {
  13.             boolean ok = true;
  14.             for (int i = 0; i < n - 1; i++)
  15.                 if (((1 << i) & st) != 0)
  16.                     if (((1 << (i + 1)) & st) != 0)
  17.                         ok = false;
  18.             if (ok)
  19.                 okay.add(st);
  20.         }
  21.         ArrayList<Integer> g[] = new ArrayList[okay.size()];
  22.         for (int i = 0; i < g.length; i++)
  23.             g[i] = new ArrayList<>();
  24.         for (int i = 0; i < okay.size(); i++) {
  25.             int x = okay.get(i);
  26.             for (int j = 0; j < okay.size(); j++) {
  27.                 int y = okay.get(j);
  28.                 boolean ok = true;
  29.                 for (int i1 = 0; i1 < n; i1++)
  30.                     if (((1 << i1) & x) != 0)
  31.                         for (int dx = -1; dx <= 1; dx++)
  32.                             if (i1 + dx >= 0 && i1 + dx < n)
  33.                                 if (((1 << (i1 + dx)) & y) != 0)
  34.                                     ok = false;
  35.                 if (ok) {
  36.                     g[i].add(j);
  37.                 }
  38.             }
  39.         }
  40.         int m1 = okay.size();
  41.         long[][] dp = new long[MAX][m1];
  42.         dp[0][0] = 1;
  43.         for (int it = 0; it < m; it++) {
  44.             long[][] dp2 = new long[MAX][m1];
  45.             for (int last = 0; last < m1; last++)
  46.                 for (int lastcnt = 0; lastcnt < MAX; lastcnt++)
  47.                     if (dp[lastcnt][last] != 0)
  48.                         for (int ne : g[last])
  49.                             dp2[lastcnt + Integer.bitCount(okay.get(ne))][ne] += dp[lastcnt][last];
  50.             dp = dp2;
  51.             for (int i = 0; i < MAX; i++)
  52.                 for (int st = 0; st < m1; st++)
  53.                     dp[i][st] %= mod;
  54.         }
  55.         long[] tmp = new long[MAX];
  56.         for (int sz = 0; sz < MAX; sz++) {
  57.             for (int i = 0; i < m1; i++)
  58.                 tmp[sz] += dp[sz][i];
  59.             tmp[sz] %= mod;
  60.         }
  61.         return tmp;
  62.     }
  63.  
  64.     int MM = 15;
  65.    
  66.     long[][][] res = new long[MM + 1][MM + 1][];
  67.  
  68.     void realSolve() {
  69.         for (int n = 0; n <= MM; n++)
  70.             for (int m = 0; m <= MM; m++) {
  71.                 res[n][m] = getAns(n, m);
  72.                 // System.err.println(n + " " + m);
  73.             }
  74.         sol();
  75.     }
  76.  
  77.     void sol() {
  78.         int te = in.nextInt();
  79.         for (int t = 0; t < te; t++) {
  80.             int n = in.nextInt();
  81.             int m = in.nextInt();
  82.             int k = in.nextInt();
  83.             out.println(res[n][m][k]);
  84.         }
  85.     }
  86.  
  87.     private class InputReader {
  88.         StringTokenizer st;
  89.         BufferedReader br;
  90.  
  91.         public InputReader(File f) {
  92.             try {
  93.                 br = new BufferedReader(new FileReader(f));
  94.             } catch (FileNotFoundException e) {
  95.                 e.printStackTrace();
  96.             }
  97.         }
  98.  
  99.         public InputReader(InputStream f) {
  100.             br = new BufferedReader(new InputStreamReader(f));
  101.         }
  102.  
  103.         String next() {
  104.             while (st == null || !st.hasMoreElements()) {
  105.                 String s;
  106.                 try {
  107.                     s = br.readLine();
  108.                 } catch (IOException e) {
  109.                     return null;
  110.                 }
  111.                 if (s == null)
  112.                     return null;
  113.                 st = new StringTokenizer(s);
  114.             }
  115.             return st.nextToken();
  116.         }
  117.  
  118.         int nextInt() {
  119.             return Integer.parseInt(next());
  120.         }
  121.  
  122.         double nextDouble() {
  123.             return Double.parseDouble(next());
  124.         }
  125.  
  126.         boolean hasMoreElements() {
  127.             while (st == null || !st.hasMoreElements()) {
  128.                 String s;
  129.                 try {
  130.                     s = br.readLine();
  131.                 } catch (IOException e) {
  132.                     return false;
  133.                 }
  134.                 st = new StringTokenizer(s);
  135.             }
  136.             return st.hasMoreElements();
  137.         }
  138.  
  139.         long nextLong() {
  140.             return Long.parseLong(next());
  141.         }
  142.     }
  143.  
  144.     InputReader in;
  145.     PrintWriter out;
  146.  
  147.     void solve() {
  148.         in = new InputReader(new File("object.in"));
  149.         try {
  150.             out = new PrintWriter(new File("object.out"));
  151.         } catch (FileNotFoundException e) {
  152.             e.printStackTrace();
  153.         }
  154.  
  155.         realSolve();
  156.  
  157.         out.close();
  158.     }
  159.  
  160.     void solveIO() {
  161.         in = new InputReader(System.in);
  162.         out = new PrintWriter(System.out);
  163.  
  164.         realSolve();
  165.  
  166.         out.close();
  167.  
  168.     }
  169.  
  170.     public static void main(String[] args) {
  171.         Locale.setDefault(Locale.US);
  172.         new Test().solveIO();
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement