Advertisement
Guest User

Untitled

a guest
Jan 17th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int long long
  6.  
  7. const int MOD = 1e9 + 7;
  8.  
  9. vector<int> prefix_function(const string s) {
  10.     int n = s.size();
  11.     vector<int> pi(n);
  12.     for (int i = 1; i < n; i++) {
  13.         int j = pi[i - 1];
  14.         while (j > 0 && s[i] != s[j]) j = pi[j - 1];
  15.         if (s[i] == s[j]) j++;
  16.         pi[i] = j;
  17.     }
  18.     return pi;
  19. }
  20.  
  21. int mul(int a, int b) {
  22.     a %= MOD; b %= MOD;
  23.     return a * b % MOD;
  24. }
  25.  
  26. signed main() {
  27.     ios_base::sync_with_stdio(false); cin.tie(0);
  28.  
  29.     int k;
  30.     string s;
  31.     cin >> k >> s;
  32.     int n = s.size();
  33.  
  34.     vector<int> pi = prefix_function(s);
  35.     int t = n - pi[n - 1];
  36.     if (n % t == 0) {
  37.         k *= n / t;
  38.         n = t;
  39.         s = s.substr(0, n);
  40.     }
  41.  
  42.     pi = prefix_function(s + s);
  43.     int result = 0;
  44.     for (int i = 0; i < n; i++) {
  45.         result += pi[i];
  46.         result %= MOD;
  47.     }
  48.  
  49.     if (k == 1) {
  50.         cout << result << '\n';
  51.         return 0;
  52.     }
  53.  
  54.     for (int i = n; i < 2 * n; i++) {
  55.         result += pi[i];
  56.         result %= MOD;
  57.     }
  58.  
  59.     t = mul((mul(k - 1, n) + 1), mul(k - 1, n)) - mul(n + 1, n);
  60.     t = t * 500000004 % MOD;
  61.     result += t;
  62.     result %= MOD;
  63.     if (result < 0) result += MOD;
  64.  
  65.     cout << result << '\n';
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement