【AtCoder】【ARC064F】Rotated Palindromes

本文介绍了一种高效计算特定条件下旋转回文序列数量的方法。通过分析序列特性,提出了一种新颖的算法来统计满足旋转后仍为回文条件的序列总数,并提供了详细的解释和实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

求有多少个序列满足以下条件:
1. 序列有n位;
2. 序列的每位为1~m之间的整数;
3. 这个序列经过旋转以后可以变成一个回文串;

Solution

这是一个悲惨的故事…..想了一天多,一直在想怎么减掉不合法的,最后一怒之下瞄了一眼(真的就是瞄一眼)标程,咦标称是直接统计耶,下一瞬间:
WOC这不是大水题吗

对于每个回文串,假设它旋转了x次以后第一次变成回文的,那么它对答案就有x的贡献(转0次~转x-1次),

考虑怎样的回文串转x次会变成回文的,(先假设x为n的约数)
把n切成(n/x)段,也就是每段有x个,
如果n/x为奇数,那么,只有这(n/x)段都相同且为回文的,它转x后还是回文的,
比如:(x=3,n=9)121 121 121;
如果n/x为偶数,那么, 这一段可以为任意,保证每(2x)位为一个回文串即可,
比如:(x=2,n=8)12 21 12 21;

根据上面计算方法,可得出,一个回文串如果转x次为回文串,那么转x的约数次也为回文串,(比如:转6次变成回文的,那么转12、18次也是回文的)

我们真正要统计的是,对于每个回文串,假设它旋转了x次以后第一次变成回文的,所以要减掉重复的,
显然,如果x不为n的约数,贡献为0,

复杂度可以参考枚举子集的复杂度,即O(3x)O(3x)(大概),不会很大。

Code

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define efo(i,q) for(int i=A[q];i;i=B[i][0])
using namespace std;
typedef long long LL;
const int N=100500,mo=1e9+7;
int m,n;
int fj[N][2],fj0;
int d[N];
LL ans;
map<int,int>f;
LL ksm(LL q,int w)
{
    LL ans=1;
    for(;w;w>>=1,q=q*q%mo)if(w&1)ans=ans*q%mo;
    return ans;
}
int ss1(int q,int w,int e)
{
    if(q>w)return f[e];
    int ans=ss1(q+1,w,e);
    fo(i,1,d[q])((ans+=ss1(q+1,w,(e=e*fj[q][0])))>=mo?ans-=mo:0);
    return ans;
}
void ss(int q,int e)
{
    if(q>fj0)
    {
        LL t=(((n/e)%2?ksm(m,(e+1)>>1):ksm(m,e))-ss1(1,q,1)+mo)%mo;
        f[e]=t;
        ans=(ans+t*e)%mo;
        return;
    }
    d[q]=0;ss(q+1,e);
    fo(i,1,fj[q][1])d[q]=i,ss(q+1,(e*=fj[q][0]));
}
int main()
{
    int q,w;
    scanf("%d%d",&n,&m);
    if(n==1)return printf("%d\n",m),0;
    q=n;
    for(int i=2;i*i<=q;++i)if(q%i==0)
    {
        for(fj[++fj0][0]=i;!(q%i);++fj[fj0][1],q/=i);
    }
    if(q>1)fj[++fj0][0]=q,fj[fj0][1]=1;
    ss(1,1);
    printf("%lld\n",(ans+mo)%mo);
    return 0;
}
### 实现旋转锚点生成器 对于目标检测和其他计算机视觉任务中的旋转锚点生成器,通常涉及创建一系列具有不同角度、比例和大小的矩形框。这些锚点框可以更好地适应倾斜的对象,从而提高模型性能。 #### 锚点生成原理 在标准的目标检测框架中,锚点通常是水平排列的边界框。然而,在处理如航空图像或文本识别等场景时,对象可能呈现任意方向。因此引入了带有角度参数的旋转锚点[^1]。 ```python import numpy as np def generate_rotated_anchors(base_size=16, ratios=[0.5, 1, 2], scales=2 ** np.arange(3, 6), angles=np.linspace(-np.pi / 4, np.pi / 4, 9)): """ Generate rotated anchors based on given parameters. :param base_size: Base size of the anchor box :param ratios: Aspect ratio list for generating different shapes :param scales: Scale factors applied to base sizes :param angles: Rotation angle range in radians :return: A set of generated rotated anchors with shape (N, 5) where N is number of total anchors, each row represents an anchor by its center coordinates(x,y), width(w), height(h) and rotation angle(a). """ # Calculate basic properties from input arguments num_ratios = len(ratios) num_scales = len(scales) num_angles = len(angles) # Initialize output array anchors = np.zeros((num_ratios * num_scales * num_angles, 5)) # Iterate over all combinations of scale,ratio & angle idx = 0 for i in range(num_ratios): w = base_size * np.sqrt(ratios[i]) h = base_size / np.sqrt(ratios[i]) for j in range(num_scales): sw = w * scales[j] sh = h * scales[j] for k in range(num_angles): cx = cy = 0 # Center point at origin initially; will be shifted later during sliding window process theta = angles[k] anchors[idx, :] = [cx, cy, sw, sh, theta] idx += 1 return anchors ``` 此函数`generate_rotated_anchors()`接受四个主要参数来控制生成过程: - `base_size`: 定义基础尺寸,默认设置为16像素。 - `ratios`: 控制宽高比的选择范围,默认提供三个选项 `[0.5, 1, 2]`. - `scales`: 调整缩放因子,默认覆盖多个尺度级别。 - `angles`: 设置旋转角度区间,默认从 `-π/4` 到 `+π/4`. 通过调整上述参数组合,可以根据具体应用场景灵活定制所需类型的旋转锚点集。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值