AtCoder Regular Contest 064 F - Rotated Palindromes
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
4 2
Sample Output 1
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
1 10
Sample Output 2
10
Sample Input 3
6 3
Sample Output 3
75
Sample Input 4
1000000000 1000000000
Sample Output 4
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的更多相关文章
- AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图
AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...
- AtCoder Regular Contest 074 F - Lotus Leaves
题目传送门:https://arc074.contest.atcoder.jp/tasks/arc074_d 题目大意: 给定一个\(H×W\)的网格图,o是可以踩踏的点,.是不可踩踏的点. 现有一人 ...
- AtCoder Regular Contest 081 F - Flip and Rectangles
题目传送门:https://arc081.contest.atcoder.jp/tasks/arc081_d 题目大意: 给定一个\(n×m\)的棋盘,棋盘上有一些黑点和白点,每次你可以选择一行或一列 ...
- AtCoder Regular Contest 066 F Contest with Drinks Hard
题意: 你现在有n个题目可以做,第i个题目需要的时间为t[i],你要选择其中的若干题目去做.不妨令choose[i]表示第i个题目做不做.定义cost=∑(i<=n)∑(i<=j<= ...
- AtCoder Regular Contest 076 F - Exhausted?
题意: n个人抢m个凳子,第i个人做的位置必须小于li或大于ri,问最少几个人坐不上. 这是一个二分图最大匹配的问题,hall定理可以用来求二分图最大匹配. 关于hall定理及证明,栋爷博客里有:ht ...
- AtCoder Regular Contest 067 F - Yakiniku Restaurants
题意: 有n个餐厅排成一排,第i个与第i+1个之间距离是Ai. 有m种食物,每种食物只能在一个餐厅里吃,第j种食物在第i个餐厅里吃的收益是$b[i][j]$. 选择每种食物在哪个餐厅里吃,使收益减去走 ...
- AtCoder Regular Contest 059 F Unhappy Hacking
Description 题面 Solution 我们发现如果一个位置需要被退掉,那么是 \(0\) 或 \(1\) 都没有关系 于是我们想到把 \(0,1\) 归为一类 问题转化为每一次可以添加和删除 ...
- 【推导】【模拟】AtCoder Regular Contest 082 F - Sandglass
题意:有个沙漏,一开始bulb A在上,bulb B在下,A内有a数量的沙子,每一秒会向下掉落1.然后在K个时间点ri,会将沙漏倒置.然后又有m个询问,每次给a一个赋值ai,然后询问你在ti时刻,bu ...
- AtCoder Regular Contest 082 F
Problem Statement We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contai ...
随机推荐
- windows系统下Eclipse启动界面更改
前段日子看到有人修改了linux系统下Eclipse的启动界面,因此自己试着修改了一下windows平台的启动界面.本文总结一下修改Eclipse 4.5(代号Mars)启动界面的方法. 方法一:修改 ...
- 洛谷 U3348 A2-回文数
题目背景 方方方很喜欢回文数,于是就有了一道关于回文数的题目. 题目描述 求从小到大第n(1<=n<=10^18)个回文数. 注释:出题人认为回文数不包括0. 输入输出格式 输入格式: 一 ...
- 随记:UWP开发中怎么使当前页面拓展到标题栏
public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); CoreAp ...
- 感觉单链表是实现BCL ICollection 的最佳方式,所有操作都能以最小的时间复杂度完成
public interface ICollection<T> : IEnumerable<T>, IEnumerable { int Count { get; }// ...
- [CV笔记]inRange对图像进行分割
先把图像转为hsv空间,然后对图像进行inrange取到hsv范围内的图像,我这里要做的是取到图中的几个白色区域以及里面的手写数字,方法可能不是最好的,因为刚入门cv没几天,先试着用所学取到这几个区域 ...
- 删除Chrome地址栏记录中自动补全的网址
为了删除某个自动补全的网站,多年的历史纪录没了,还浪费我十多分钟,蠢哭_(:з」∠)_ 不是历史记录.不是清除浏览器数据.不是myactivity(谷歌账号)中的历史纪录,直接在书签中搜索,删除,OK ...
- N17_判断树B是不是树A的子结构
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) package new_offer; /** * 输入两棵二叉树A,B,判断B是不是A的子结构.( ...
- Django models多表操作
title: Django models多表操作 tags: Django --- 多表操作 单独创建第三张表的情况 推荐使用的是使用values/value_list,selet_related的方 ...
- Nginx: ubuntu系统上如何判断是否安装了Nginx?
问题描述:ubuntu系统上,如何查看是否安装了Nginx? 解决方法:输入命令行:ps -ef | grep nginx master process后面就是Nginx的安装目录. 延伸:1. 如何 ...
- mount命令的用法,以及技巧光盘镜像文件、移动硬盘及U盘的方法
本文介绍mount命令的用法,以及技巧光盘镜像文件.移动硬盘及U盘的方法. 一,挂接命令(mount) 挂接(mount)命令的使用方法. 命令格式: 复制代码 代码如下: mount [-t vfs ...