Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <cstdio>
  2. #include <ctime>
  3.  
  4. const int maxN = 100500500;//124500500;
  5. int to[2][maxN], // graph with 2 types of edges (0 and 1)
  6.     smart[2][maxN]; // smart sufflinks
  7. char len[maxN]; // lengthes of palindrome in all vertices
  8. char s[maxN]; // main string
  9. bool was[maxN]; // visited vertices
  10. int now, //current vertex in graph
  11.     size, //size of graph
  12.     N, //limit to size of string
  13.     firstTime; //start of programm (after entering N)
  14.  
  15.  
  16. void init()
  17. {
  18.     len[1] = -1;
  19.     smart[0][2] = smart[1][2] = 1;
  20.     s[0] = 2;
  21.     // letter 0
  22.     len[3] = 1;
  23.     smart[1][3] = 1;
  24.     smart[0][3] = 2;
  25.     to[0][1] = 3;
  26.  
  27.     //letter 1
  28.     len[4] = 1;
  29.     smart[0][4] = 1;
  30.     smart[1][4] = 2;
  31.     to[1][1] = 4;
  32.  
  33.     size = 5;
  34. }
  35.  
  36. long long cnt[100]; //answer to the problem
  37.  
  38. void dfs(int i, int state)
  39. {
  40.     if(i == N + 1) return;
  41.     for(int bit = 0; bit < 2; bit++)
  42.     {
  43.         int * go = to[bit];
  44.         s[i] = bit;
  45.         int first = i - len[state] - 1;
  46.         int now = (bit == s[first]) ? state : smart[bit][state];
  47.         int nxt = go[now];
  48.         if(nxt == 0)
  49.         {
  50.             nxt = size++;
  51.             len[nxt] = len[now] + 2;
  52.             int sb = go[smart[bit][now]];
  53.             int B = s[i - len[sb]];
  54.             smart[B][nxt] = sb;
  55.             smart[B ^ 1][nxt] = smart[B ^ 1][sb];
  56.             go[now] = nxt;
  57.         }
  58.         if(!was[nxt])
  59.         {
  60.             was[nxt] = 1;
  61.             cnt[i]++;
  62.             dfs(i + 1, nxt);
  63.             was[nxt] = 0;
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. long long print(long long num)
  70. {
  71.     bool first = true;
  72.     long long n = num;
  73.     for(long long mod = (long long) 1e9; mod > 0; mod /= 1000)
  74.     {
  75.         if(n / mod == 0)
  76.         {
  77.             printf("    ");
  78.         }
  79.         else if(first)
  80.         {
  81.             printf("% 4lld", n / mod);
  82.             first = false;
  83.         }
  84.         else
  85.         {
  86.             printf("'%03lld", n / mod);
  87.         }
  88.         n %= mod;
  89.     }
  90.     printf("\n");
  91.     return num;
  92. }
  93.  
  94.  
  95. void printTime(int time)
  96. {
  97.     printf("elapsed time = %d:%02d:%02d\n", time / 3600, time % 3600 / 60, time % 60);
  98. }
  99.  
  100.  
  101. void print()
  102. {
  103.     freopen("output.txt", "w", stdout);
  104.     long long sum = 2;
  105.     printf(" 1  ");
  106.     print(2);
  107.     for(int i = 2; i <= N; i++)
  108.     {
  109.         printf("% 2d    ", i);
  110.         sum += print(cnt[i] * 2);
  111.     }
  112.     printf("sum = ");
  113.     print(sum);
  114.     printf("count of vertices of graph = ");
  115.     print(size);
  116.     int lastTime = time(0);
  117.     printTime(lastTime - firstTime);
  118. }
  119.  
  120.  
  121. int main()
  122. {
  123.     scanf("%d", &N);
  124.     firstTime = time(0);
  125.     init();
  126.     dfs(2, 3);
  127.     print();
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement