Problem Statement

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

Input

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

N K

Output

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


Sample Input 1

Copy
4 2

Sample Output 1

Copy
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)

Sample Input 2

Copy
1 10

Sample Output 2

Copy
10

Sample Input 3

Copy
6 3

Sample Output 3

Copy
75

Sample Input 4

Copy
1000000000 1000000000

Sample Output 4

Copy
875699961

用1-k填序列,序列可以左移n次变为回文

不考虑移位总共有k^(n/2)个回文,所以就是一个容斥问题了

若回文串的最小循环节长度为c,经过c次操作后,得到c个序列
当c为偶数时,重复数为一半,奇数时,无重复

所以这个问题就解决了,其循环节只可能是其因子

#include <bits/stdc++.h>
using namespace std;
const int N=,MD=1e9+;
int v[N],f[N];
int Pow(int x,int y)
{
int ans=;
for(;y;x=x*1LL*x%MD,y>>=)if(y&)ans=1LL*ans*x%MD;
return ans;
}
int main()
{
int n,k,tot=,ans=,i;
cin>>n>>k;
for(i=; i*i<n; i++)
if(n%i==)v[tot++]=i,v[tot++]=n/i;
if(i*i==n)v[tot++]=i;
sort(v,v+tot);
for(int i=; i<tot; i++)
{
f[i]=Pow(k,(v[i]+)/);
for(int j=; j<i; j++)if(v[i]%v[j]==) f[i]=(f[i]-f[j])%MD;
if(v[i]&) ans=(ans+1LL*v[i]*f[i])%MD;
else ans=(ans+v[i]*1LL*Pow(,MD-)%MD*f[i])%MD;
}
cout<<(ans+MD)%MD;
}

AtCoder Regular Contest 064 F - Rotated Palindromes的更多相关文章

  1. AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图

    AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...

  2. AtCoder Regular Contest 074 F - Lotus Leaves

    题目传送门:https://arc074.contest.atcoder.jp/tasks/arc074_d 题目大意: 给定一个\(H×W\)的网格图,o是可以踩踏的点,.是不可踩踏的点. 现有一人 ...

  3. AtCoder Regular Contest 081 F - Flip and Rectangles

    题目传送门:https://arc081.contest.atcoder.jp/tasks/arc081_d 题目大意: 给定一个\(n×m\)的棋盘,棋盘上有一些黑点和白点,每次你可以选择一行或一列 ...

  4. AtCoder Regular Contest 066 F Contest with Drinks Hard

    题意: 你现在有n个题目可以做,第i个题目需要的时间为t[i],你要选择其中的若干题目去做.不妨令choose[i]表示第i个题目做不做.定义cost=∑(i<=n)∑(i<=j<= ...

  5. AtCoder Regular Contest 076 F - Exhausted?

    题意: n个人抢m个凳子,第i个人做的位置必须小于li或大于ri,问最少几个人坐不上. 这是一个二分图最大匹配的问题,hall定理可以用来求二分图最大匹配. 关于hall定理及证明,栋爷博客里有:ht ...

  6. AtCoder Regular Contest 067 F - Yakiniku Restaurants

    题意: 有n个餐厅排成一排,第i个与第i+1个之间距离是Ai. 有m种食物,每种食物只能在一个餐厅里吃,第j种食物在第i个餐厅里吃的收益是$b[i][j]$. 选择每种食物在哪个餐厅里吃,使收益减去走 ...

  7. AtCoder Regular Contest 059 F Unhappy Hacking

    Description 题面 Solution 我们发现如果一个位置需要被退掉,那么是 \(0\) 或 \(1\) 都没有关系 于是我们想到把 \(0,1\) 归为一类 问题转化为每一次可以添加和删除 ...

  8. 【推导】【模拟】AtCoder Regular Contest 082 F - Sandglass

    题意:有个沙漏,一开始bulb A在上,bulb B在下,A内有a数量的沙子,每一秒会向下掉落1.然后在K个时间点ri,会将沙漏倒置.然后又有m个询问,每次给a一个赋值ai,然后询问你在ti时刻,bu ...

  9. AtCoder Regular Contest 082 F

    Problem Statement We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contai ...

随机推荐

  1. GetOpenFileName 选择文件夹的解决方法

    某些环境下(如WIN PE)windows自带的选择文件夹api不能使用或者体验不佳.现在利用GetOpenFileName的回掉参数实现了选择文件夹选择功能. #include <Window ...

  2. Java异常之RuntimeException

    人生不如意十有八九.在打Core Java里面的例子的时候总是一遍就过,但是实际上只要是自己想着动手去打造自己想要的东西,异常的状况也是十有八九的. 在Java中会使用异常处理的错误捕获机制处理这些异 ...

  3. fork新建进程

    #include <sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<stdio.h> ...

  4. Java Marker Interface

    先看看什么是标记接口?标记接口有时也叫标签接口(Tag interface),即接口不包含任何方法. 在Java里很容易找到标记接口的例子,比如JDK里的Serializable接口就是一个标记接口. ...

  5. python之函数的初识

    1. 面向过程编程的缺点 代码重复 代码可可读性不高 2. 函数的定义*** ​ 函数是以功能为导向,一个函数封装一个功能.登录,注册,文件的改的操 3.函数的作用*** ​ 函数减少代码的重复性,增 ...

  6. javaweb基础(20)_JavaBean总结

    一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须私有化. 私有化的属性必须通过public类型的方法 ...

  7. C#基于联通短信Sgip协议构建短信网关程序

    此软件基于中国联通Sgip协议程序接口,适合在中国联通申请了短信发送端口的公司使用.短信群发已经成为现在软件系统.网络营销等必不可少的应用工具.可应用在短信验证.信息群发.游戏虚拟商品购买.事件提醒. ...

  8. 利用ss5服务搭建代理服务器

    利用ss5服务搭建代理服务器 1. 下载ss5-3.8.9-8.tar.gz ###官网下载http://ss5.sourceforge.net/ 2. 安装ss5 yum -y install gc ...

  9. [bzoj]1930 pacman吃豆豆

    Description 两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会吃掉它.PACMAN行走的路线很奇怪,只能向右走或者向上走,他 ...

  10. 转 Keras 保存与加载网络模型

    https://blog.csdn.net/qq_28413479/article/details/77367665