Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <string>
- #include <complex>
- #include <vector>
- #include <stdlib.h>
- using namespace std;
- #include <cstdio>
- /** Interface */
- template <class T = int>
- inline T readInt(); // skip spaces, read signed int
- inline int readUInt(); // skip spaces, read unsigned int
- inline int readChar(); // skip spaces, read char
- inline void readWord( char *s ); // skip spaces, read word
- inline bool readLine( char *s ); // read line (do not save '\n')
- inline bool isEof();
- inline int peekChar();
- inline bool seekEof();
- template <class T>
- inline void writeInt( T x );
- inline void writeChar( int x ); // you may use putchar() instead of it
- inline void writeWord( const char *s );
- inline void flush();
- /** Read */
- static const int buf_size = 4096;
- static char __buf[buf_size];
- static int __len = 0, __pos = 0;
- inline bool isEof() {
- if (__pos == __len) {
- __pos = 0, __len = fread(__buf, 1, buf_size, stdin);
- if (__pos == __len)
- return 1;
- }
- return 0;
- }
- inline int getchar_fast() { // you may use getchar() instead of it
- return isEof() ? -1 : __buf[__pos++];
- }
- inline int peekChar() {
- return isEof() ? -1 : __buf[__pos];
- }
- inline bool seekEof() {
- int c;
- while ((c = peekChar()) != -1 && c <= 32)
- __pos++;
- return c == -1;
- }
- inline int readChar() {
- int c = getchar_fast();
- while (c != -1 && c <= 32)
- c = getchar_fast();
- return c;
- }
- inline int readUInt() {
- int c = readChar(), x = 0;
- while ('0' <= c && c <= '9')
- x = x * 10 + c - '0', c = getchar_fast();
- return x;
- }
- template <class T>
- inline T readInt() {
- int s = 1, c = readChar();
- T x = 0;
- if (c == '-')
- s = -1, c = getchar_fast();
- while ('0' <= c && c <= '9')
- x = x * 10 + c - '0', c = getchar_fast();
- return s == 1 ? x : -x;
- }
- inline void readWord( char *s ) {
- int c = readChar();
- while (c > 32)
- *s++ = c, c = getchar_fast();
- *s = 0;
- }
- inline bool readLine( char *s ) {
- int c = getchar_fast();
- while (c != '\n' && c != -1)
- *s++ = c, c = getchar_fast();
- *s = 0;
- return c != -1;
- }
- /** Write */
- static int write_pos = 0;
- static char write_buf[buf_size];
- inline void writeChar( int x ) {
- if (write_pos == buf_size)
- fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
- write_buf[write_pos++] = x;
- }
- inline void flush() {
- if (write_pos)
- fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
- }
- template <class T>
- inline void writeInt( T x ) {
- if (x < 0)
- writeChar('-'), x = -x;
- char s[24];
- int n = 0;
- while (x || !n)
- s[n++] = '0' + x % 10, x /= 10;
- while (n--)
- writeChar(s[n]);
- }
- inline void writeWord( const char *s ) {
- while (*s)
- writeChar(*s++);
- }
- const int MAXN = 1 << 20;
- char s[MAXN];
- int n1, n2, n;
- int res[MAXN];
- vector < complex < double > > b;
- vector < int > a;
- void rev(vector < complex < double > > &a) {
- int n = (int)a.size();
- for (int i = 1, j = 0; i < n; ++i) {
- int bit;
- for (bit = n >> 1; j >= bit; bit >>= 1)
- j -= bit;
- j += bit;
- if (i < j)
- swap(a[i], a[j]);
- }
- }
- void fft(vector < complex < double > > &a, bool inv = false) {
- int n = (int)a.size();
- rev(a);
- for (int k = 2, p = 1; k <= n; k <<= 1, p <<= 1) {
- double ang = 2 * M_PI / k * (inv ? -1 : 1);
- complex < double > wn(cos(ang), sin(ang));
- for (int i = 0; i < n; i += k) {
- complex < double > w(1);
- for (int j = 0; j < p; ++j) {
- complex < double > x = a[i + j];
- complex < double > y = w * a[i + j + p];
- a[i + j] = x + y;
- a[i + j + p] = x - y;
- w *= wn;
- }
- }
- }
- if (inv) {
- for (int i = 0; i < n; ++i)
- a[i] /= n;
- }
- }
- int main() {
- freopen("duel.in", "r", stdin);
- freopen("duel.out", "w", stdout);
- readLine(s);
- //scanf("%s", s);
- int len = (int)strlen(s);
- for (int i = 0; i < len; ++i)
- a.push_back(s[i] - '0');
- int n = 1;
- while (n < len)
- n <<= 1;
- n <<= 1;
- a.resize(n);
- b.resize(n);
- for (int i = 0; i < n; ++i)
- b[i] = a[i];
- fft(b);
- for (int i = 0; i < n; ++i)
- b[i] *= b[i];
- fft(b, true);
- for (int i = 0; i < n; ++i)
- res[i] = (int)(b[i].real() + 0.5);
- long long ans = 0;
- for (int i = 0; i < n; ++i)
- ans += res[2 * i] * a[i] - a[i] * a[i];
- printf("%lld\n", ans / 2);
- /*for (int i = n; i >= 0; --i)
- writeInt(res[i]);
- writeChar('\n');
- */
- //flush();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement