[BZOJ 5330][SDOI2018] 反回文串
怎么说呢,一道不可多得的反演题吧,具体解释之后再补
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll;
;
ll mul(ll x,ll y,ll p) {
x%=p; y%=p;
return (x*y-(ll)((long double)x/p*y+0.5)*p+p)%p;
}
ll _pow(ll x,ll n,ll p) {
ll ret=;
,x=mul(x,x,p)) ) ret=mul(ret,x,p);
return ret;
}
ll tp[]={2LL,3LL,5LL,7LL,13LL,61LL};
bool MR(ll n) {
) return false;
rep(i,,) if(n==tp[i]) return true;
rep(i,,) ) return false;
rep(i,,) {
ll tmp=n-;)) tmp>>=;
ll s=_pow(tp[i],tmp,n);
&&s!=&&tmp!=n-) tmp<<=,s=mul(s,s,n);
&&!(tmp&)) return false;
}
return true;
}
ll PR(ll n,ll c) {
ll i=,k=2LL,x,y; x=y=1LL+rand()%(n-);
) {
x=(mul(x,x,n)+c)%n;
ll d=__gcd((y-x+n)%n,n);
&&d!=n) return d;
if(x==y) return n;
;
}
}
int op[maxn],len,cnt,T;
ll n,P,K,ans,gt[maxn];
inline void fct(ll n) {
) return;
if(MR(n)){gt[++len]=n;return;}
ll p=n;
;p==n;--c) p=PR(p,c);
fct(p); fct(n/p);
}
ll fpow(ll x,ll n,ll p) {
ll ret=;
,x=x*x%p)
) ret=ret*x%p;
return ret;
}
ll g(ll n) {,P);}
ll f(ll n) {?n%P:(n>>)%P;}
inline void dfs(int dp,ll d,ll pro) {
) {
)&&(d&)==) return;
(ans+=1LL*g(n/d)*f(n/d)%P*pro%P)%=P;
return;
}
dfs(dp+,d,pro); pro=1LL*pro*(+P-gt[dp]%P)%P;
rep(i,,op[dp]) d*=gt[dp],dfs(dp+,d,pro);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("25.in","r",stdin);
#endif
scanf();
while(T--) {
scanf("%lld%lld%lld",&n,&K,&P);K%=P;
len=cnt=;++cnt;
memset(gt,,,sizeof(op));
fct(n);
sort(gt+,gt++len);
rep(i,,len) {
;
++op[cnt];
}
ans=;dfs(,1LL,1LL);printf("%lld\n",ans);
}
;
}

[BZOJ 5330][SDOI2018] 反回文串的更多相关文章
- BZOJ 5330 Luogu P4607 [SDOI2018]反回文串 (莫比乌斯反演、Pollard Rho算法)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=5330 (Luogu) https://www.luogu.org/prob ...
- [BZOJ5330][SDOI2018]反回文串
luogu bzoj sol 枚举一个长度为\(n\)为回文串,它的所有循环位移都可以产生贡献. 但是这样算重了.重复的地方在于可能多个回文串循环同构,或者可能有的回文串经过小于\(n\)次循环位移后 ...
- [SDOI2018]反回文串
题意 问有多少个长度为\(N\)且字符集大小为\(K\)的字符串可以通过回文串旋转 (把第一个字符移到最后)若干次得到.\(K\le N≤10^{18}\) 做法 ARC64F的加强版 设\(h(d) ...
- 【SDOI2018】反回文串(【ARC064 F】Rotated Palindromes 加强版)
题意 给你一个正整数 \(n\),求有多少字符集为 \(1\) 到 \(k\) 之间整数的字符串,使得该字符串可以由一个长度为 \(n\) 的回文串循环移位得到. ARC原题 \(100\%\) 的数 ...
- BZOJ 3676: [Apio2014]回文串
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2013 Solved: 863[Submit][Status ...
- bzoj 3676: [Apio2014]回文串 回文自动机
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 844 Solved: 331[Submit][Status] ...
- 字符串(马拉车算法,后缀数组,稀疏表):BZOJ 3676 [Apio2014]回文串
Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...
- ●BZOJ 3676 [Apio2014]回文串
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题解: 后缀数组,Manacher,二分 首先有一个结论:一个串的本质不同的回文串的个 ...
- 「SDOI 2018」反回文串
题目大意: 求字符集大小为$k$长度为$n$的经循环移位后为回文串的数量. 题解: 这题是D1里最神的吧 考虑一个长度为$n$回文串,将其循环移位后所有的串都是满足要求的串. 但是显然这样计算会算重. ...
随机推荐
- 616. Add Bold Tag in String加粗字符串
[抄题]: Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b&g ...
- 2-javascript::笔记
0.位置: HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head& ...
- Android中如何区分界面组件创建和销毁的类型
本文主要描述: 1.分辨系统杀掉退出还是用户主动退出2.分辨全新的创建还是系统恢复性的创建 1.分辨系统杀掉退出还是用户主动退出 当一个组件失去焦点后,系统有可能为了释放资源而杀掉这个组件,这个时候系 ...
- 前端基础 之 Bootstrap框架
浏览目录 Bootstrap介绍 为什么要使用Bootstrap? Bootstrap环境搭建 布局容器 栅格系统 Bootstrap全局样式 一.Bootstrap介绍 Bootstrap是Twit ...
- hdu 4768 Flyer (异或操作的应用)
2013年长春网络赛1010题 继巴斯博弈(30分钟)签到后,有一道必过题(一眼即有思路). 思路老早就有(40分钟):倒是直到3小时后才被A掉.期间各种换代码姿态! 共享思路: unlucky st ...
- delphi让exe开机自启动
procedure AutoRunOnSystemStart(Title, FileName: String);const _Software_Microsoft_Windows_CurrentVe ...
- .net Stream篇(五)
MemoryStream 目录: 1 简单介绍一下MemoryStream 2 MemoryStream和FileStream的区别 3 通过部分源码深入了解下MemoryStream 4 分析Mem ...
- 匿名函数和lamda表达式
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- HackTwo
使用延迟加载以及避免代码重复 一.概要: <include />标签是整理布局的有效工具,提供了合理组织XML布局文件的有效方法. ViewStub是实现延迟加载视图的优 ...
- C#导出Excel-利用特性自定义数据
网上C#导出Excel的方法有很多.但用来用去感觉不够自动化.于是花了点时间,利用特性做了个比较通用的导出方法.只需要根据实体类,自动导出想要的数据 1.在NuGet上安装Aspose.Cells或 ...