semipal.in / semipal.out

Por Costel the pig, our programmer in-training, has recently returned from the Petrozaporksk training camp. There, he learned a lot of things: how to boil a cob, how to scratch his belly using his keyboard, etc... He almost remembers a programming problem too:

A semipalindrome is a word  for which there exists a subword  such that  is a prefix of  and  (reverse ) is a suffix of . For example, 'ababba' is a semipalindrom because the subword 'ab' is prefix of 'ababba' and 'ba' is suffix of 'ababba'.

Let's consider only semipalindromes that contain letters 'a' and 'b'. You have to find the -th lexicographical semipalindrome of length .

Por Costel doesn't remember if the statement was exactly like this at Petrozaporksk, but he finds this problem interesting enough and needs your help to solve it.

Input

On the first line of the file semipal.in, there is an integer  () representing the number of test cases. On the next  lines there are 2 numbers,  ( and K  where  is the number of semipalindromes of length .

Output

In the output file semipal.out, there should be  lines, the -th of which should contain the answer for the -th test.

Example

Input
2
5 1
5 14
Output
aaaaa
bbabb

因为卡内存,所以不能把答案的表全打出来,但是可以每隔100记录一次答案,这样只需要开10w的数组。然后每次询问的时候,从最近的记录的答案开始暴力,不超过100次就能得到答案。

#include<cstdio>
using namespace std;
#define MOD 10000003
typedef long long ll;
int n,a,b,x1,q,q1;
int anss[100010];
int main()
{
freopen("pocnitoare.in","r",stdin);
freopen("pocnitoare.out","w",stdout);
// freopen("k.in","r",stdin);
scanf("%d%d%d%d%d%d",&n,&a,&b,&x1,&q,&q1);
int now=x1;
anss[1]=now;
for(int i=2;i<=10000003;++i)
{
now=(int)((((ll)now*(ll)(i-1)%(ll)n)%(ll)n+(ll)a%(ll)n)%(ll)n);
if(i%100==1)
anss[i/100+1]=now;
}
// int now=anss[q1/100+1];
// int tmp=q1%100-1;
// for(int i=1;i<=tmp;++i)
// now=(int)((((ll)now*(ll)(i-1)%(ll)n)%(ll)n+(ll)a%(ll)n)%(ll)n);
// printf("%d\n",now);
for(int i=1;i<=q;++i)
{
if(i!=1)
q1=((int)((ll)(i-1)*(ll)now%(ll)MOD)+b%MOD)%MOD+1;
now=anss[(q1-1)/100+1];
for(int j=(q1-1)/100*100+2;j<=q1;++j)
now=(int)((((ll)now*(ll)(j-1)%(ll)n)%(ll)n+(ll)a%(ll)n)%(ll)n);
printf("%d\n",now);
}
return 0;
}

【分块打表】Gym - 100923K - Por Costel and the Firecracker的更多相关文章

  1. 【Heap-dijkstra】Gym - 100923B - Por Costel and the Algorithm

    algoritm.in / algoritm.out Even though he isn't a student of computer science, Por Costel the pig ha ...

  2. 【找规律】Gym - 100923L - Por Costel and the Semipalindromes

    semipal.in / semipal.out Por Costel the pig, our programmer in-training, has recently returned from ...

  3. 【数形结合】Gym - 100923I - Por Costel and the Pairs

    perechi3.in / perechi3.out We don't know how Por Costel the pig arrived at FMI's dance party. All we ...

  4. 【并查集】Gym - 100923H - Por Costel and the Match

    meciul.in / meciul.out Oberyn Martell and Gregor Clegane are dueling in a trial by combat. The fight ...

  5. 【动态规划】Gym - 100923A - Por Costel and Azerah

    azerah.in / azerah.out Por Costel the Pig has received a royal invitation to the palace of the Egg-E ...

  6. 【带权并查集】Gym - 100923H - Por Costel and the Match

    裸题. 看之前的模版讲解吧,这里不再赘述了. #include<cstdio> #include<cstring> using namespace std; int fa[10 ...

  7. 【Gym - 100923A】Por Costel and Azerah(思维水题)

    Por Costel and Azerah Descriptions 给你n个数 问你,有多少个子序列 的和是偶数 Example Input 233 10 124 2 Output 33 题目链接 ...

  8. 【Gym - 100923I】Por Costel and the Pairs(思维题)

    Por Costel and the Pairs Descriptions 有T组测试样例 有n个男的,n个女的,第i个人都有为当前一个大小为i的懒惰值,当一男一女懒惰值的乘积<=n他们就就可以 ...

  9. 洛谷P4240 毒瘤之神的考验 【莫比乌斯反演 + 分块打表】

    题目链接 洛谷P4240 题解 式子不难推,分块打表真的没想到 首先考虑如何拆开\(\varphi(ij)\) 考虑公式 \[\varphi(ij) = ij\prod\limits_{p | ij} ...

随机推荐

  1. CSS中z-index全解析

    一.z-index解释 z-index属性决定了一个HTML元素的层叠级别,元素层叠级别是相对于元素在Z轴上(与X轴Y轴相对照)的位置而言.一个更高的z-index值意味着这个元素在叠层顺序中会更靠近 ...

  2. Spring任务调度<task:scheduled-tasks>【含cron参数详解】 (转载)

    Spring内部有一个task是Spring自带的一个设定时间自动任务调度 task使用的时候很方便,但是他能做的东西不如quartz那么的多! 可以使用注解和配置两种方式,配置的方式如下 引入Spr ...

  3. 【Foreign】树 [prufer编码][DP]

    树 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 3 2 2 1 Sample Outp ...

  4. Xcode5根控制器使用xib展示的步骤

    #error:Xcode5根控制器使用xib展示,步骤 ⓵取消mainInterface ⓶右击file's owner对xib进行view-view连线,否则: Terminating app du ...

  5. [bzoj3994][SDOI2015]约数个数和-数论

    Brief Description 计算\(\sum_{i\leqslant n}\sum_{j\leqslant m}\sigma_0(ij)\). Algorithm Design 首先证明一个结 ...

  6. 硬币问题 tarjan缩点+DP 莫涛

    2013-09-15 20:04 题目描述 有这样一个游戏,桌面上摆了N枚硬币,分别标号1-N,每枚硬币有一个分数C[i]与一个后继硬币T[i].作为游戏参与者的你,可以购买一个名为mlj的小机器人, ...

  7. bzoj 1089 SCOI2003严格n元树 递推

    挺好想的,就是一直没调过,我也不知道哪儿的错,对拍也拍了,因为数据范围小,都快手动对拍了也不知道 哪儿错了.... 我们定义w[i]代表深度<=i的严格n元树的个数 那么最后w[d]-w[d-1 ...

  8. Django-内置Admin

    Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: 依赖APP: django.contrib.auth django.contrib.contenttyp ...

  9. 使用Redirector插件解决googleapis公共库加载的问题

    最近访问一些面向国外的网站总是会出现ajax.googleaips.com无法加载的情况.以下为加载stackoverflow时的情境: 图1 -无法加载的google公共库 问题的原因是谷歌没有在国 ...

  10. Linux下git源码安装【转】

    转自:http://blog.csdn.net/u012889638/article/details/51167123 版权声明:本文为博主原创文章,未经博主允许不得转载. 版本信息:CentOS r ...