Monday, 4 January 2016

B. Quantity of Strings

B. Quantity of Strings

Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two!
Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left.
Input
The first and only line contains three integers: nm and k (1 ≤ n, m, k ≤ 2000).
Output
Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7).
Sample test(s)
input
1 1 1
output
1
input
5 2 4
output
2
Note
In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a").
In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".
--------------------------------------------------editorial------------------------------------------------
We can offer you two solitions:
  1. You can build a graph with positions in sting as a nodes and equality in any substring of length k as edges. Lets denote e the number of components in the graph. The answer is me.
  2. Analyze four cases:
    • k = 1 or ะบ > n, the answer is mn.
    • k = n, the answer is m(n + 1) / 2.
    • k mod 2 = 1, any string like abababab... is ok, so the answer is m2.
    • k mod 2 = 0, all symbols coincide and the answer is m.
    • ------------------------------------------or we can use mst ---------------------------------------
    • #include<iostream>
    • #include<bits/stdc++.h>
    • using namespace std;
    • int rank[20000];
    • int par[20000];
    • int has[20000];
    • int  find(int  a)
    •       {
    •         if(par[a]!=par[par[a]])
    •          {
    •           par[a]=find(par[a]);
    •          }
    •      
    •          return par[a];
    •      }
    •      void add(int a , int b)
    •      {
    •       int x=find(a);
    •       int y=find(b);
    •       if(rank[x]>rank[y])
    •       {
    •        par[y]=x;
    •      }
    •      else
    •      {
    •         par[x]=y;
    •         if(rank[x]==rank[y])
    •         {
    •           rank[y]++;
    •     }
    •      }
    •      }

    • int main()
    •  {
    •   int n,m,kk;
    •   cin>>n>>m>>kk;
    •   for(int i=1;i<=n;i++)
    •   {
    •   par[i]=i;
    •   rank[i]=0;
    • }
    •   for(int i=1;i<=n-kk+1;i++)
    •   {
    •   int j=i,k=i+kk-1;
    •     while(j<k)
    •      {
    •     
    •       if(find(j)!=find(k))
    •        {
    •          // cout<<"add "<<j<<" "<<k<<endl;
    •         add(j,k);
    • }
    • j++;
    • k--;
    •  }
    •   // 7 4 20

    •  }
    •   int ans=0;
    •   for(int i=1;i<=n;i++)
    •    {
    •     if(has[find(i)]==0)
    •     {
    •     has[find(i)]=1;
    •     ans++;
    • }
    • }
    • ///cout<<ans<<endl;
    • long long int fin=1;
    • long long int mod=1000000007;
    • // for(int i=1;i<=n;i++) cout<<par[i]<<" ";
    • // cout<<endl;
    • for(int i=1;i<=ans;i++)
    • {
    • fin=(fin*m) %mod;
    • }
    • cout<<fin<<endl;
    • //else cout<<"0"<<endl;
    •  }

No comments:

Post a Comment