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 ...
随机推荐
- u-boot剖析(一)----Makefile分析
由于u-boot比较庞大,所以我们分开来分析,对于一个大型的项目我们想快速的了解其代码架构和内容,最方便的方法就是分析Makefile,所以我们今天以三星的s3c2440来分析Makefile.我们今 ...
- 挂sqlserver计划,系统自动分配拣货任务
USE [P2WMS_WH43] GO /****** Object: StoredProcedure [dbo].[sp_fru_CalcAllocatePickData] Script Date: ...
- c#和Java中的抽象类
应用场景:当父类中的方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法. 比如:描述一个图形.圆形. 矩形三个类.不管哪种图形都会具备计算面积与周长的行为,但是每种图形计算的方式 ...
- c++文件偏移
#include <iostream> #include <fstream> #include <cassert> using namespace std; int ...
- Java中 Character方法练习:字符串中英文字母个数 5435abc54abc3AHJ5 正则:matches("[a-zA-Z0-9]{1}")
package com.swift; public class String_Letter_Number_Test { public static void main(String[] args) { ...
- C#:CodeSmith根据数据库中的表创建C#数据模型Model + 因为没有钱买正版,所以附加自己写的小代码
对于C#面向对象的思想,我们习惯于将数据库中的表创建对应的数据模型: 但假如数据表很多时,我们手动增加模型类会显得很浪费时间: 这个时候有些人会用微软提供的EntityFrameWork,这个框架很强 ...
- [vijos]P1979 NOIP2015 信息传递
描述 有 n 个同学(编号为 1 到 n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 TiTi 的同学. 游戏开始时,每人都只知道 ...
- 转载:将画布(canvas)图像保存成本地图片的方法
之前我曾介绍过如何将HTML5画布(canvas)内容转变成图片形式,方法十分简单.但后来我发现只将canvas内容转变成图片输出还不够,如何能将转变后的图片保存到本地呢? 其实,这个方法也是非常简单 ...
- Python基础学习总结__Day3
一.集合 1.特性:无序且天生去重,格式为{} 2.作用: (1)去重 (2)关系测试 3.可调用函数(常见对列表操作) (1)取交集:A.intersection(B) (2)取并集:A.union ...
- MySQL迁移升级解决方案
任务背景 由于现有业务架构已不能满足当前业务需求,在保证数据完整的前提下,现需要将原有数据库迁移到另外一台单独的服务器上,在保证原有服务正常的情况下,将原有LAMP环境中mysql数据库版本5.6.3 ...