【分块打表】Gym - 100923K - Por Costel and the Firecracker

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

25 15 14

Output

aaaaabbabb

因为卡内存,所以不能把答案的表全打出来,但是可以每隔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;
}
时间: 2024-10-14 11:40:17

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

【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 has started to study Graph Theory. Today he's learning about Bellman-Ford, an algorithm that calculates the minimum cost path from a source node (for in

【并查集】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 is extremely important, as the life of Tyrion Lannister is on the line. Oberyn and Gregor are measuring their skill in combat the only way the two be

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

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 r

Por Costel and the Match Gym - 100923H(经典种类并查集)

Por Costel and the Match Gym - 100923H 题目链接:https://vjudge.net/problem/Gym-100923H 题目: Oberyn Martell and Gregor Clegane are dueling in a trial by combat. The fight is extremely important, as the life of Tyrion Lannister is on the line. Oberyn and Gr

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

Por Costel and Azerah Descriptions 给你n个数 问你,有多少个子序列 的和是偶数 Example Input 233 10 124 2 Output 33 题目链接 https://vjudge.net/problem/Gym-100923A 恶心死了   freopen("azerah.in","r",stdin); freopen("azerah.out","w",stdout); 必须加

BZOJ 3758 数数 分块打表

题目大意:定义一个数是完美的,当且仅当这个数的每一位可以分成两个集合,使这两个集合之和相等,求[a,b]区间内有多少个数是完美的 数位DP?--不大好搞 分块打表大法好! 首先考虑验证一个数是不是完美的怎么搞 求出数字和 如果是奇数肯定不是 如果是偶数就跑一下背包 背包很慢?没关系,由于最大的和只能有9*8/2=36 所以我们直接状压 令f=1 然后对于每一位x有 f|=f<<x 最后返回f&(1<<sum/2)即可 时间复杂度O(lgx) 但是这数据范围是10^9诶!总不

【分块打表】bzoj 3758 数数

[题目描述] Description 神犇最近闲来无事,于是就思考哲学,研究数字之美.在神犇看来,如果一个数的各位能够被分成两个集合,而且这两个集合里的数的和相等,那么这个数就是优美的(具体原因就只有神犇才知道了).现在神犇在思考另一个问题,在区间[a,b]中有多少个数是优美的?这个问题对于神犇来说很简单,相信对于你来说也不难. Input 输入只有一行,包含两个整数a和b. Output 输出只有一行,包含一个整数,代表区间[a,b]中优美的数的个数. Sample Input 1 11 Sa

【分块打表】bzoj3758 数数

验证一个数是不是优美的:设数位之和为sum,若sum mod 2 != 0,则不优美.否则考虑枚举这个数的每一位,将之前所有位任意相加产生的所有 数字和 和 当前位 的和塞到集合里,最终判断集合中是否存在sum/2即可,若暴力,复杂度是1+2+4+...,无法承受,所以可以压位. 然后对10^9分块,暴力处理每个块内的答案,然后打个表,对零散的部分暴力即可. #include<cstdio> using namespace std; const int table[]={0,376413,83

BZOJ 3798 特殊的质数 分块打表

题目大意:求[l,r]区间内有多少个质数可以分解为两个正整数的平方和 考虑到对于一个数Check一下是O(√n)的 我们可以将3*10^8分成3000块 每块10W 对于整块的打表求出有多少个质数 块内暴力 令n为块的大小 则时间复杂度为O(n√n) 打表时忘加优化忘开O2 打了一下午 各种酸爽 #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include