Rotated Palindromes

问题 J: Rotated Palindromes

时间限制: 1 Sec   内存限制: 128 MB
提交: 4   解决: 3
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

Takahashi and Aoki are going to together construct a sequence of integers.
First, Takahashi will provide a sequence of integers a, satisfying all of the following conditions:
The length of a is N.
Each element in a is an integer between 1 and K, inclusive.
a is a palindrome, that is, reversing the order of elements in a will result in the same sequence as the original.
Then, Aoki will perform the following operation an arbitrary number of times:
Move the first element in a to the end of a.
How many sequences a can be obtained after this procedure, modulo 109+7?

Constraints
1≤N≤109
1≤K≤109

输入

The input is given from Standard Input in the following format:
N K

输出

Print the number of the sequences a that can be obtained after the procedure, modulo 109+7.

样例输入

4 2

样例输出

6

提示

The following six sequences can be obtained:
(1,1,1,1)
(1,1,2,2)
(1,2,2,1)
(2,2,1,1)
(2,1,1,2)
(2,2,2,2)


题意 : 有一个长度为 n 的回文串,每一位上可以填 1~k 中的一个数,现在有一种操作,将该串中的第一数字放到最后面组成新串,求最终能组成多少串,答案取模1e9+7。

思路 :用 先预处理每个循环节长的种类数,如果由操作可以推出,若循环节长度为偶数,他对答案的贡献为 种类数*循环节/2 , 若为奇数,则贡献为 种类数*循环节长度。 那么对循环节而言,可能会有重复计算的情况,那么我们用容斥去一下重复的种类数即可。

代码如下:

#include 
using namespace std;
const int maxn=1e6+10,mod=1e9+7;
typedef long long ll;
ll fac[maxn],inv[maxn];
ll Pow_mod(ll a,ll n){
    ll res=1;
    while(n){
        if(n&1) res=res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return res;
}
ll dp[maxn],n,k,sta[maxn],top=0;
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie();
    cin>>n>>k;
    for (int i=1; i*i<=n;i++){
        if(n%i==0){
        sta[++top]=i;
        if(i*i!=n) sta[++top]=n/i;
        }
    }
    sort(sta+1,sta+1+top);
    for (int i=1; i<=top ;i++) dp[i]=Pow_mod(k,(sta[i]+1)/2);
//    for (int i=1; i<=top ;i++){
//        cout<

你可能感兴趣的:(dp,容斥,dp,容斥)