您的位置:首页 > 产品设计 > UI/UE

ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) Codeforces932 A. Palindromic Supersequence(水题) B.Recursive Queries(前缀和) C.Permutation Cycle(数学)

2018-02-20 23:17 661 查看
占坑,明天写,想把D补出来一起写。2/20/2018 11:17:00 PM



----------------------------------------------------------我是分割线-------------------------------------------------------

我来了,本来打算D题写到一起的,但是有新的东西要写,D就单独写一篇,这里写A,B,C;

开启智障模式:(看我咸鱼突刺的厉害( • ̀ω•́ )✧) 2/21/2018 10:46:00 PM

A. Palindromic Supersequence

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

You are given a string A. Find a string B, where B is a palindrome and A is a subsequence of B.

A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the order of the remaining characters. For example, "cotst" is a subsequence of "contest".

A palindrome is a string that reads the same forward or backward.

The length of string B should be at most 104. It is guaranteed that there always exists such string.

You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 104.

Input
First line contains a string A (1 ≤ |A| ≤ 103) consisting of lowercase Latin letters, where |A| is a length of A.

Output
Output single line containing B consisting of only lowercase Latin letters. You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 104. If there are many possible B, print any of them.

Examples

input
aba


output
aba


input
ab


output
aabaa


Note
In the first example, "aba" is a subsequence of "aba" which is a palindrome.

In the second example, "ab" is a subsequence of "aabaa" which is a palindrome.

这道题完全直接就OK,emnnn,哈哈哈,直接倒着再来一遍就可以。

代码:

1 //A. Palindromic Supersequence-水题
2 #include<iostream>
3 #include<cstring>
4 #include<cstdio>
5 #include<cmath>
6 #include<algorithm>
7 #include<queue>
8 using namespace std;
9 const int maxn=1000+10;
10 char a[maxn],b[2*maxn];
11 int main(){
12     while(~scanf("%s",a)){
13             memset(b,0,sizeof(b));
14         int len=strlen(a);
15         int h=0;
16         for(int i=0;i<len;i++)
17             b[h++]=a[i];
18         for(int i=len-1;i>=0;i--)
19             b[h++]=a[i];
20         printf("%s\n",b);
21     }
22     return 0;
23 }


B. Recursive Queries

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Let us define two functions f and g on positive integer numbers.





You need to process Q queries. In each query, you will be given three integers l, r and k. You need to print the number of integers xbetween l and r inclusive, such that g(x) = k.

Input
The first line of the input contains an integer Q (1 ≤ Q ≤ 2 × 105) representing the number of queries.

Q lines follow, each of which contains 3 integers l, r and k (1 ≤ l ≤ r ≤ 106, 1 ≤ k ≤ 9).

Output
For each query, print a single line containing the answer for that query.

Examples

input
Copy

4
22 73 9
45 64 6
47 55 7
2 62 4


output
1
4
0
8


input
Copy

4
82 94 6
56 67 4
28 59 9
39 74 4


output
3
1
1
5


Note
In the first example:

g(33) = 9 as g(33) = g(3 × 3) = g(9) = 9

g(47) = g(48) = g(60) = g(61) = 6

There are no such integers between 47 and 55.

g(4) = g(14) = g(22) = g(27) = g(39) = g(40) = g(41) = g(58) = 4

这道题就是直接暴力应该会超时,用前缀和处理一下就可以了(吐槽,一开始都没看懂题,本咸鱼就没读懂过带公式的题(╥╯^╰╥))

代码:

//B. Recursive Queries-前缀和
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stdlib.h>
using namespace std;
const int maxn=1e6+10;
int a[10][maxn];
int fun(int x){
if(x<10)return x;
int sum=1;
while(x){
sum*=x%10>0?x%10:1;
x/=10;
}
return fun(sum);
}
void qianzhuihe(){
for(int i=1;i<=1000000;i++)
a[fun(i)][i]++;
for(int i=1;i<10;i++){
for(int j=1;j<=1000000;j++)
a[i][j]+=a[i][j-1];
}
}
int main(){
qianzhuihe();
int t;
scanf("%d",&t);
while(t--){
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",a[k][r]-a[k][l-1]);
}
return 0;
}


C. Permutation Cycle

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:



Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.

For given N, A, B, find a permutation P of integers from 1 to N such that for 1 ≤ i ≤ N, g(i) equals either A or B.

Input
The only line contains three integers N, A, B (1 ≤ N ≤ 106, 1 ≤ A, B ≤ N).

Output
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.

Examples

input
Copy

9 2 5


output
6 5 8 3 4 1 9 2 7


input
Copy

3 2 1


output
1 2 3


Note
In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5

In the second example, g(1) = g(2) = g(3) = 1

这道题真的是要吐槽啊,欺负人(;´д`)ゞ,欺负我读不懂公式题。本来感觉要用exgcd写,简直是浪费,直接for个循环就出来了。

解释一下样例1,就是9个数,要求有周期为2的循环圈和周期为5的循环圈,这里g(i)就分别是2和5。这个题的公式里的j-1就是周期循环的时候用到的。

首先怎么解决有几个圈呢?其实就是一个方程,Ax+By=N,直接for一下就可以得到x和y,这里样例1就是2x+5y=9,算出来x=2,y=1。

所以有2个以2为周期的圈和1个以5为周期的圈。然后就是1~N的数就用一遍,所以直接自己瞎构造就可以。

继续,解释一下样例为什么是6 5 8 3 4 1 9 2 7

从6开始,这个数列里面第6个数为1,然后再从1开始,第1个数为6,这个周期为2,是一个圈,然后继续往后,。

从5开始,第5个数是4,然后从4开始,第4个数是3,然后从3开始,第3个数是8,然后从8开始,第8个数是2,然后从2开始,第2个数是5,循环回来了,这个周期为5,是一个圈。继续。。。

一直到9之前都是已经存在的圈了,从9开始,第9个数是7,从7开始,第7个数是9,循环成一个圈了,这个周期为2,就满足推出来的是2个2周期的圈,1个5周期的圈。

答案不唯一,满足条件的还有其他的数列,2 1 4 3 6 7 8 9 5也满足条件。2 1是一个圈,4 3是一个圈,6 7 8 9 5是一个圈。

对于怎么构造这个数列,你会发现,直接成圈这样的好理解,瞎写一下就可以了,我不知道样例上的是怎么构造出来的。

代码:

1 //C. Permutation Cycle-数学题,脑子不行-(本来感觉要用exgcd写)
2 #include<iostream>
3 #include<string.h>
4 #include<stdio.h>
5 #include<cmath>
6 #include<cstring>
7 #include<cstdio>
8 #include<algorithm>
9 #include<queue>
10 using namespace std;
11 int main(){
12     int n,a,b;
13     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
14     cin>>n>>a>>b;
15     if(a==1||b==1){
16         cout<<"1";
17         for(int i=2;i<=n;i++)
18             cout<<" "<<i;
19         cout<<endl;
20         return 0;
21     }
22     int x=0,y=0;
23     for(int i=0;i<=n/a;i++){
24         if((n-a*i)%b==0){
25             x=i;
26             y=(n-a*i)/b;
27         }
28     }
29     if(x==0&&y==0) {
30         cout<<"-1"<<endl;
31         return 0;
32     }
33     int i=1;
34     for(int k=0;k<x;k++){
35         for(int j=0;j<a-1;j++)
36             cout<<i+j+1<<" ";
37         cout<<i<<" ";
38         i+=a;
39     }
40     for(int k=0;k<y;k++){
41         for(int j=0;j<b-1;j++)
42             cout<<i+j+1<<" ";
43         cout<<i<<" ";
44         i+=b;
45     }
46     return 0;
47 }


先这样,等我看会了LCA和RMQ,本咸鱼就去写D了。(本来今天可以写D的题解,但是发生了国际间谍行动(╬◣д◢),我也没看完。但是昨天说好了今天要来写题解的,就写A,B,C的题解强行掩饰一波)



溜了溜了。。。(/~0~)/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐