ICM Technex 2018 and Codeforces Round #463 ABC

本文介绍了解决两个编程挑战的方法:构造一个包含特定子序列的最短回文串,及处理一系列递归查询,每个查询涉及计算特定范围内整数中满足特定条件的数量。

A. Palindromic Supersequence

 

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
Copy
aba
output
Copy
aba
input
Copy
ab
output
Copy
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.

 

 s+reverse(s)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char str[1010];
 4 int main() {
 5     cin >> str;
 6     int len = strlen(str);
 7     cout << str;
 8     for(int i = len-1; i >= 0; i --) cout << str[i];
 9     cout << endl;
10     return 0;
11 }

 

B. Recursive Queries

 

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 lr 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 lr 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
Copy
1
4
0
8
input
Copy
4
82 94 6
56 67 4
28 59 9
39 74 4
output
Copy
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

 dfs预处理1e6,然后查询的话就直接查询了。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e6+10;
 4 int g[N], vis[N][10];
 5 int dfs(int x) {
 6     if(x < 10) return x;
 7     int ans = 1, tmp = x;
 8     while(tmp) {
 9         if(tmp%10) ans *= tmp%10;
10         tmp /= 10;
11     }
12     g[x] = ans>=10?dfs(ans):ans;
13     return g[x];
14 }
15 int main() {
16     for(int i = 1; i <= N; i ++) {
17         if(!g[i]) g[i] = dfs(i);
18     }
19     for(int i = 1; i <= N; i ++) {
20         for(int j = 1; j < 10; j ++) {
21             vis[i][j] = vis[i-1][j] + (j==g[i]);
22         }
23     }
24     int q, l, r, k;
25     cin >> q;
26     while(q--) {
27         cin >> l >> r >> k;
28         cout << vis[r][k] - vis[l-1][k] << endl;
29     }
30     return 0;
31 }

C. Permutation Cycle

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 ≤ Ng(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
Copy
6 5 8 3 4 1 9 2 7
input
Copy
3 2 1
output
Copy
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

 

 求循环节。只要有存在ax+by=n就不是-1,然后按a、b的循环。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e6+10;
 4 int p[N];
 5 int main() {
 6     int n, a, b, x, y;
 7     cin >> n >> a >> b;
 8     for(x = 0; x <= n; x ++) {
 9         int ans = n-x*a;
10         if(ans < 0) break;
11         if(ans >= 0 && ans%b==0) {
12             y = ans/b;
13             break;
14         }
15     }
16     if(x*a > n) printf("-1\n");
17     else{
18         for(int i = 0; i < x; i ++) {
19             p[i*a+1] = (i+1)*a;
20             for(int j = i*a+2; j <= (i+1)*a; j ++) {
21                 p[j] = j-1;
22             }
23         }
24         for(int i = 0; i < y; i ++) {
25             p[a*x+i*b+1] = (i+1)*b+a*x;
26             for(int j = x*a+i*b+2; j <= (i+1)*b+a*x; j ++) {
27                 p[j] = j-1;
28             }
29         }
30         for(int i = 1; i <= n; i ++) {
31             printf("%d ",p[i]);
32         }
33     }
34     return 0;
35 }

 

转载于:https://www.cnblogs.com/xingkongyihao/p/8885211.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值