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;
}

最新文章

  1. UBER的故事
  2. 高质量,高效率的多国语言软件开发(Web/PC/Mobile),使用接口约束/调用不同语言资源
  3. [转]ASP.NET MVC 4 (九) 模型绑定
  4. nginx配置(解释)
  5. PCIE体系结构
  6. Android StrictMode介绍
  7. python链接mysql以及常用语法
  8. Python 串口通信操作
  9. bzoj 4025: 二分图
  10. HDU - 6444 Neko&#39;s loop(循环节+最大子段和)
  11. initializer_list 列表初始化
  12. php无限极分类递归与普通
  13. java 队列的使用(转载)
  14. C关键字volatile总结
  15. 189. Rotate Array(两次反转)
  16. java中String的内存位置
  17. CentOS 7安装与配置jdk-8u162
  18. 使用Redis的五个注意事项(命名)
  19. w3cschool在线教程
  20. webpack 3.x plugins

热门文章

  1. sls文件
  2. BS架构下使用消息队列的工作流程
  3. 7月19日day11总结
  4. CMOS与BIOS
  5. [POJ3237]Tree解题报告|树链剖分|边剖
  6. mysql 等 null 空值排序
  7. 【Python实例二】BeautifulSoup爬虫简单实践
  8. DWM.EXE进程(Desktop Window Manager)不能删除
  9. Linux上使用程序相对路径访问文件【转】
  10. POJ3466(01背包变形)