Advertisement
Guest User

Untitled

a guest
May 29th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <string>
  5. #include <complex>
  6. #include <vector>
  7. #include <stdlib.h>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. #include <cstdio>
  13.  
  14. /** Interface */
  15.  
  16. template <class T = int>
  17. inline T readInt();              // skip spaces, read signed int
  18. inline int readUInt();           // skip spaces, read unsigned int
  19. inline int readChar();           // skip spaces, read char
  20. inline void readWord( char *s ); // skip spaces, read word
  21. inline bool readLine( char *s ); // read line (do not save '\n')
  22. inline bool isEof();
  23. inline int peekChar();
  24. inline bool seekEof();
  25.  
  26. template <class T>
  27. inline void writeInt( T x );
  28. inline void writeChar( int x ); // you may use putchar() instead of it
  29. inline void writeWord( const char *s );
  30. inline void flush();
  31.  
  32. /** Read */
  33.  
  34. static const int buf_size = 4096;
  35.  
  36. static char __buf[buf_size];
  37. static int __len = 0, __pos = 0;
  38.  
  39. inline bool isEof() {
  40.     if (__pos == __len) {
  41.         __pos = 0, __len = fread(__buf, 1, buf_size, stdin);
  42.         if (__pos == __len)
  43.             return 1;
  44.     }
  45.     return 0;
  46. }
  47.  
  48. inline int getchar_fast() { // you may use getchar() instead of it
  49.     return isEof() ? -1 : __buf[__pos++];
  50. }
  51.  
  52. inline int peekChar() {
  53.     return isEof() ? -1 : __buf[__pos];
  54. }
  55.  
  56. inline bool seekEof() {
  57.     int c;
  58.     while ((c = peekChar()) != -1 && c <= 32)
  59.         __pos++;
  60.     return c == -1;
  61. }
  62.  
  63. inline int readChar() {
  64.     int c = getchar_fast();
  65.     while (c != -1 && c <= 32)
  66.         c = getchar_fast();
  67.     return c;
  68. }
  69.  
  70. inline int readUInt() {
  71.     int c = readChar(), x = 0;
  72.     while ('0' <= c && c <= '9')
  73.         x = x * 10 + c - '0', c = getchar_fast();
  74.     return x;
  75. }
  76.  
  77. template <class T>
  78. inline T readInt() {
  79.     int s = 1, c = readChar();
  80.     T x = 0;
  81.     if (c == '-')
  82.         s = -1, c = getchar_fast();
  83.     while ('0' <= c && c <= '9')
  84.         x = x * 10 + c - '0', c = getchar_fast();
  85.     return s == 1 ? x : -x;
  86. }
  87.  
  88. inline void readWord( char *s ) {
  89.     int c = readChar();
  90.     while (c > 32)
  91.         *s++ = c, c = getchar_fast();
  92.     *s = 0;
  93. }
  94.  
  95. inline bool readLine( char *s ) {
  96.     int c = getchar_fast();
  97.     while (c != '\n' && c != -1)
  98.         *s++ = c, c = getchar_fast();
  99.     *s = 0;
  100.     return c != -1;
  101. }
  102.  
  103. /** Write */
  104.  
  105. static int write_pos = 0;
  106. static char write_buf[buf_size];
  107.  
  108. inline void writeChar( int x ) {
  109.     if (write_pos == buf_size)
  110.         fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
  111.     write_buf[write_pos++] = x;
  112. }
  113.  
  114. inline void flush() {
  115.     if (write_pos)
  116.         fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
  117. }
  118.  
  119. template <class T>
  120. inline void writeInt( T x ) {
  121.     if (x < 0)
  122.         writeChar('-'), x = -x;
  123.    
  124.     char s[24];
  125.     int n = 0;
  126.     while (x || !n)
  127.         s[n++] = '0' + x % 10, x /= 10;
  128.     while (n--)
  129.         writeChar(s[n]);
  130. }
  131.  
  132. inline void writeWord( const char *s ) {
  133.     while (*s)
  134.         writeChar(*s++);
  135. }
  136.  
  137. const int MAXN = 1 << 20;
  138.  
  139. char s[MAXN];
  140. int n1, n2, n;
  141. int res[MAXN];
  142. vector < complex < double > > b;
  143. vector < int > a;
  144.  
  145. void rev(vector < complex < double > > &a) {
  146.     int n = (int)a.size();
  147.     for (int i = 1, j = 0; i < n; ++i) {
  148.         int bit;
  149.         for (bit = n >> 1; j >= bit; bit >>= 1)
  150.             j -= bit;
  151.         j += bit;
  152.         if (i < j)
  153.             swap(a[i], a[j]);
  154.     }
  155. }
  156.  
  157. void fft(vector < complex < double > > &a, bool inv = false) {
  158.     int n = (int)a.size();
  159.     rev(a);
  160.    
  161.     for (int k = 2, p = 1; k <= n; k <<= 1, p <<= 1) {
  162.         double ang = 2 * M_PI / k * (inv ? -1 : 1);
  163.         complex < double > wn(cos(ang), sin(ang));
  164.         for (int i = 0; i < n; i += k) {
  165.             complex < double > w(1);
  166.             for (int j = 0; j < p; ++j) {
  167.                 complex < double > x = a[i + j];
  168.                 complex < double > y = w * a[i + j + p];
  169.                 a[i + j] = x + y;
  170.                 a[i + j + p] = x - y;
  171.                 w *= wn;
  172.             }
  173.         }
  174.     }
  175.  
  176.     if (inv) {
  177.         for (int i = 0; i < n; ++i)
  178.             a[i] /= n;
  179.     }
  180. }
  181.  
  182. int main() {
  183.     freopen("duel.in", "r", stdin);
  184.     freopen("duel.out", "w", stdout);
  185.    
  186.    
  187.     readLine(s);
  188.     //scanf("%s", s);
  189.     int len = (int)strlen(s);
  190.     for (int i = 0; i < len; ++i)
  191.         a.push_back(s[i] - '0');
  192.  
  193.     int n = 1;
  194.     while (n < len)
  195.         n <<= 1;
  196.     n <<= 1;
  197.     a.resize(n);
  198.     b.resize(n);
  199.    
  200.     for (int i = 0; i < n; ++i)
  201.         b[i] = a[i];
  202.    
  203.     fft(b);
  204.     for (int i = 0; i < n; ++i)
  205.         b[i] *= b[i];
  206.     fft(b, true);
  207.    
  208.     for (int i = 0; i < n; ++i)
  209.         res[i] = (int)(b[i].real() + 0.5);
  210.    
  211.     long long ans = 0;
  212.     for (int i = 0; i < n; ++i)
  213.         ans += res[2 * i] * a[i] - a[i] * a[i];
  214.    
  215.     printf("%lld\n", ans / 2);
  216.     /*for (int i = n; i >= 0; --i)
  217.         writeInt(res[i]);
  218.     writeChar('\n');
  219.     */
  220.     //flush();
  221.     return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement