思维训练
技巧
快读
因为scanf和cin很慢,我们发现使用getchar读取字符非常快,所以我们读取数字也用getchar来加快读取速度。
1 | inline ll read() |
Multiply by 2, divide by 6
You are given an integer nn. In one move, you can either multiply nn by two or divide nn by 66 (if it is divisible by 66 without the remainder).
Your task is to find the minimum number of moves needed to obtain 11 from nn or determine if it’s impossible to do that.
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.
The only line of the test case contains one integer nn (1≤n≤1091≤n≤109).
Output
For each test case, print the answer — the minimum number of moves needed to obtain 11 from nn if it’s possible to do that or -1 if it’s impossible to obtain 11 from nn.
Example
Input
1 | 7 |
Output
1 | 0 |
Note
Consider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer 1511654415116544:
Divide by 66 and get 25194242519424;
divide by 66 and get 419904419904;
divide by 66 and get 6998469984;
divide by 66 and get 1166411664;
multiply by 22 and get 2332823328;
divide by 66 and get 38883888;
divide by 66 and get 648648;
divide by 66 and get 108108;
multiply by 22 and get 216216;
divide by 66 and get 3636;
divide by 66 and get 66;
divide by 66 and get 11.
分析:
×2÷6等价于÷3,而÷6等价于÷2÷3,分解因子,设x能分解成sum2个2和sum3个3,如果sum2的个数大于sum3的个数,则说明不能得到1,因为要÷2只能通过÷6这个操作,而这个操作本身又在÷3,所有直接输出-1即可。
否则,3的个数大于2,那么可以通过×2乘回来,它们的差加上3的个数就是答案。
另一种思路,也是最容易想到的,先一值除以6,如果不能整除了,就一直除以3,如果不能整除,则输出NO,否则输出yes
参考代码
1 |
|
Zero Remainder Array
You are given an array aa consisting of nn positive integers.
Initially, you have an integer x=0x=0. During one move, you can do one of the following two operations:
- Choose exactly one ii from 11 to nn and increase aiai by xx (ai:=ai+xai:=ai+x), then increase xx by 11 (x:=x+1x:=x+1).
- Just increase xx by 11 (x:=x+1x:=x+1).
The first operation can be applied no more than once to each ii from 11 to nn.
Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by kk (the value kk is given).
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.
The first line of the test case contains two integers nn and kk (1≤n≤2⋅105;1≤k≤1091≤n≤2⋅105;1≤k≤109) — the length of aa and the required divisior. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the ii-th element of aa.
It is guaranteed that the sum of nn does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).
Output
For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by kk.
Example
Input
1 | 5 |
Output
1 | 6 |
Note
Consider the first test case of the example:
- x=0x=0, a=[1,2,1,3]a=[1,2,1,3]. Just increase xx;
- x=1x=1, a=[1,2,1,3]a=[1,2,1,3]. Add xx to the second element and increase xx;
- x=2x=2, a=[1,3,1,3]a=[1,3,1,3]. Add xx to the third element and increase xx;
- x=3x=3, a=[1,3,3,3]a=[1,3,3,3]. Add xx to the fourth element and increase xx;
- x=4x=4, a=[1,3,3,6]a=[1,3,3,6]. Just increase xx;
- x=5x=5, a=[1,3,3,6]a=[1,3,3,6]. Add xx to the first element and increase xx;
- x=6x=6, a=[6,3,3,6]a=[6,3,3,6]. We obtained the required array.
Note that you can’t add xx to the same element more than once.
题目大意:最开始给你一个x=0,还有一个长度为n的数组,你可以执行两个操作:
①要么x+1
②要么ai+x,然后x再+1
最后要让数组a的所有元素都能整除k,问最少的操作次数。
这个题目有点绕,就是先对ai求余,然后找到相同余数x出现最多的次数maxx,如果出现次数相同那就找最小的,
比如余数3 3 2 2 1,假设k=4,这里2和3都出现2次,找2,因为2要变成4,要加2,3要变成4,要加1,处理2要比处理3麻烦,所以要找最小的。因为有两个2,x是递增的,第一个2加2,就能变成4,而第二个只能变成8,即2k,要加上6,(maxx-1)*k,答案就是(maxx-1)×k+k-余数x+1
参考代码
1 |
|
Social Distance
Polycarp and his friends want to visit a new restaurant. The restaurant has nn tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 11 to nn in the order from left to right. The state of the restaurant is described by a string of length nn which contains characters “1” (the table is occupied) and “0” (the table is empty).
Restaurant rules prohibit people to sit at a distance of kk or less from each other. That is, if a person sits at the table number ii, then all tables with numbers from i−ki−k to i+ki+k (except for the ii-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than kk.
For example, if n=8n=8 and k=2k=2, then:
- strings “10010001”, “10000010”, “00000000”, “00100000” satisfy the rules of the restaurant;
- strings “10100100”, “10011001”, “11111111” do not satisfy to the rules of the restaurant, since each of them has a pair of “1” with a distance less than or equal to k=2k=2.
In particular, if the state of the restaurant is described by a string without “1” or a string with one “1”, then the requirement of the restaurant is satisfied.
You are given a binary string ss that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string ss.
Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of “0” that can be replaced by “1” such that the requirement will still be satisfied?
For example, if n=6n=6, k=1k=1, s=s= “100010”, then the answer to the problem will be 11, since only the table at position 33 can be occupied such that the rules are still satisfied.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test. Then tt test cases follow.
Each test case starts with a line containing two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of tables in the restaurant and the minimum allowed distance between two people.
The second line of each test case contains a binary string ss of length nn consisting of “0” and “1” — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two “1” is more than kk.
The sum of nn for all test cases in one test does not exceed 2⋅1052⋅105.
Output
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 00.
Example
Input
1 | 6 |
Output
1 | 1 |
参考代码:
1 |
|
XXXXX
Ehab loves number theory, but for some reason he hates the number xx. Given an array aa, find the length of its longest subarray such that the sum of its elements isn’t divisible by xx, or determine that such subarray doesn’t exist.
An array aa is a subarray of an array bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
The first line contains an integer tt (1≤t≤5)(1≤t≤5) — the number of test cases you need to solve. The description of the test cases follows.
The first line of each test case contains 2 integers nn and xx (1≤n≤1051≤n≤105, 1≤x≤1041≤x≤104) — the number of elements in the array aa and the number that Ehab hates.
The second line contains nn space-separated integers a1a1, a2a2, ……, anan (0≤ai≤1040≤ai≤104) — the elements of the array aa.
Output
For each testcase, print the length of the longest subarray whose sum isn’t divisible by xx. If there’s no such subarray, print −1−1.
Example
Input
1 | 3 |
Output
1 | 2 |
参考代码
1 |
|
Most socially-distanced subsequence
Given a permutation pp of length nn, find its subsequence s1s1, s2s2, ……, sksk of length at least 22 such that:
- |s1−s2|+|s2−s3|+…+|sk−1−sk||s1−s2|+|s2−s3|+…+|sk−1−sk| is as big as possible over all subsequences of pp with length at least 22.
- Among all such subsequences, choose the one whose length, kk, is as small as possible.
If multiple subsequences satisfy these conditions, you are allowed to find any of them.
A sequence aa is a subsequence of an array bb if aa can be obtained from bb by deleting some (possibly, zero or all) elements.
A permutation of length nn is an array of length nn in which every element from 11 to nn occurs exactly once.
Input
The first line contains an integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer nn (2≤n≤1052≤n≤105) — the length of the permutation pp.
The second line of each test case contains nn integers p1p1, p2p2, ……, pnpn (1≤pi≤n1≤pi≤n, pipi are distinct) — the elements of the permutation pp.
The sum of nn across the test cases doesn’t exceed 105105.
Output
For each test case, the first line should contain the length of the found subsequence, kk. The second line should contain s1s1, s2s2, ……, sksk — its elements.
If multiple subsequences satisfy these conditions, you are allowed to find any of them.
Example
Input
1 | 2 |
Output
1 | 2 |
参考代码
1 |
|
博弈论
Road to Arabella
Ayoub and Kilani felt board while they are going to ArabellaCPC in (Amman-Irbid) road, so Kilani invented a new game to play with Ayoub.
The game is described by the following rules :
Ayoub picks a random integer $$$n$$$ $$$(1 \leq n \leq 10^{9})$$$ , and Kilani picks a random integer $$$k$$$ $$$(1 \leq k \leq n)$$$, then they will start playing. In each turn a player can choose any number $$$x$$$ $$$(1 \leq x \leq max(1 , m-k) )$$$ (which $$$m$$$ is the current value of $$$n$$$) and subtract it from $$$n$$$. if $$$n$$$ equals zero then the player can’t make a move. The player who can’t make a move is considered to lose the game.
If Kilani starts, and each player played optimally, who would be the winner?
题目炸了,这里给出链接:
Road to Arabella - Gym 102263B - Virtual Judge (vjudge.net)
题解:
考虑极端情况,如果n-k<=1,这种情况每次只能减1,可以改变奇偶性,这时,如果n是奇数,先手就会获胜,否则后手获胜
其他情况先手总能找到一个x使一轮以后出现极端情况并且让n是奇数,这种情况下先手必胜
代码:
1 |
|
Check The Text
Roze has a special keyboard which consists only of 29 keys:
-26 alphabetic a-z keys, which prints the 26 lowercase Latin letters.
-“Space” key, which prints a single space.
-“CapsLock” key, which converts the status of the letters keys from lowercase to uppercase and vice versa. The status initially is lowercase.
-“Backspace” key, which deletes the last letter/space that was written on the screen.
If Roze presses “Backspace” and there is nothing to delete on the screen, nothing will happen.
Given the text that Roze had to print and the order of the keys she has pressed on the keyboard, check if Roze has printed the text correctly (including exactly one space between every two words).
由于题目还是乱码,我先放个链接
Check The Text - Gym 102263C - Virtual Judge (vjudge.net)
简单模拟即可,不过如果像我这样写很多分支的话,一定要注意,第一次这样卡在45个样例,贴一下WA代码:
1 |
|
AC代码:
1 |
|
Kefa and Company
题目链接:http://codeforces.com/contest/580/problem/B
1 |
|