A. in either
Topic describes
XXX planet banknotes face denominations only: 100100100 yuan, 555 yuan, 222 yuan, 111 yuan, a total of 444 kinds.
Xiao Ming went to XXX star to travel, he only had 222 XXX star coins of 100100100100 yuan in hand, it was too inconvenient, just pass by XXX star bank to change.
Xiao Ming is a bit obsessive-compulsive. He insisted that the number of 222 yuan notes in the 200,200,200 yuan banknotes should be exactly 101,010 times the number of 111 yuan notes. The rest, of course, were all 555 yuan notes.
The bank staff is a little embarrassed, can you help to calculate: under the premise of meeting Xiao Ming’s requirements, how many banknotes should be changed to him at least?
(555 yuan, 222 yuan, 111 yuan must have denominations, can not be 000)
Train of thought
Let the number of unary items be XXX, and the number of two items be 10x10x10x. Number five yuan zhang is (10-200 – x x ⋅ 2) 5 \ frac {\ left (200 – x – 10 x \ : \ cdot \ : \ : 2, right)} {5} 5 (10-200 – x x ⋅ 2)
Can the function of the number to all expressions of f (x) = (2-200 – x 10 x ⋅) 5 + 11 xf \ left \ \ right (x) : = \ : \ frac {\ left (200 – x to 10 x \ : \ cdot 2 \ \ “: \ \ : right)} {5} + 11 xf (x) = 5 (10-200 – x x ⋅ 2) + 11 x
Further reduction could get: f (x) = 40 + 34 x5f \ left \ \ right (x) : = \ : 40 + \ frac {34 x} {5} f (x) = 40 + 534 x
If x=5x=5x=5, f(x)f\left(x\right)f(x) can take the minimum value 747474
code
#include<bits/stdc++.h>
using namespace std;
int main(a) {
cout << 74 << endl;
return 0;
}
Copy the code
B. Laser style
Topic describes
XXX planet’s grand festival to add to the atmosphere, with 303,030 light machines lined up, to the space out of the light column.
Installation and debugging only found that, for some reason, two adjacent lasers can not be opened at the same time!
The king would like to know how many laser effects can be produced under the current bugbugbug condition.
Obviously, if there are only 333 machines, 555 styles can be made, namely:
Shut it all down (sorrysorrysorry)
One, 333 in all
Open two, only 111
303030 is not good enough, the king had to ask for help.
Train of thought
Dynamic programming DP
- Use 0 to indicate that the laser lamp is off
- The laser light is indicated by 1
A light can be 000, 111 immediately off or on; For 2 lights it can be 000000, 010101, 101010 (side by side) for 3 lights it can be 000000000, 010010010, 100100100, 001001001, 101101101 (side by side)
So you can see that the number of types of three lamps is made up of two parts,
- The first part is that the first two lights don’t work, and the third light doesn’t work
- The second part is, if the second light is off, the third light is on, and the number of styles when the second light is off is equal to the number of styles when there is only one light.
Dp [I] DP [I] indicates the type of lamp when there are three lamps in total
Can get DP initial conditions: DP [1] = 2, DP [2] = 3 DP \ left 1 \ [right] \ : = \ : 2, \ : DP \ left 2 \ [right] \ : = \ : 3 DP [1] = 2, DP [2] = 3
State transition equation: Dp [I] = dp [I – 1) + dp [I] – 2 (I 3 or higher) dp \ left \ [I \ right] : = \ : dp \ left [] I \ : – \ : 1 \ right \ : + \ : dp \ left [\ : I – \ : 2 \ right] \ : \ left (I \ : \ Ge \ : 3 \ right) dp [I] = dp [I – 1) + dp [I] – 2 (I 3 or higher)
code
#include<bits/stdc++.h>
using namespace std;
int main(a) {
int dp[31];
dp[1] = 2;
dp[2] = 3;
for (int i = 3; i <= 30; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
cout << dp[30] << endl;
return 0;
}
Copy the code
C. gray code
Topic describes
Gray codes are n bits of binary to represent numbers.
Unlike normal binary representation, it requires that two adjacent digits differ by only one digit.
The first and last digits should also be separated by only one digit.
There are many algorithms to generate gray codes. Here is one of the more common:
Start with all zeros.
When the odd number is generated, only the last digit of the current number is changed (0 becomes 1,1 becomes 0).
When producing the even number, find the rightmost 1 and change the number to the left of it.
Using this rule produces the following sequence of 4-bit gray codes:
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
Copy the code
Train of thought
A = a ^ ((a & (-a)) << 1); / / fill in the blanks
(a << 1) ^ a (a << 1) ^ a (a << 1) ^ a (a << 1) ^ a)
0101XOR0111= 00100101\:XOR\:0111\:=\:00100101XOR0111=0010 So you need to find a way to change 0111 to 0010 and take the negative number 1001 and XORXORXOR You get 0010 and you plug in the other odd numbers and it works.
code
#include <stdio.h>
void show(int a,int n)
{
int i;
int msk = 1;
for(i=0; i<n- 1; i++) msk = msk << 1;
for(i=0; i<n; i++){
printf((a & msk)? "1" : "0");
msk = msk >> 1;
}
printf("\n");
}
void f(int n)
{
int i;
int num = 1;
for(i=0; i<n; i++) num = num<<1;
int a = 0;
for(i=0; i<num; i++){
show(a,n);
if(i%2= =0){
a = a ^ 1;
}
else{
a = a ^ ((a & (-a)) << 1);/ / fill in the blanks}}}int main(a)
{
f(4);
return 0;
}
Copy the code
D. the watch
Topic describes
Xiao Ming has bought a high-end, elegant electronic watch and he is about to set the time.
In M78, time is measured in different units than on Earth, with an hour in M78 having NNN minutes.
As you all know, the watch has only one button to increase the current number by one. If the number currently displayed is 000 when you set the minutes, pressing the button will change it to 111, and pressing the button again will change it to 222. If the current number is N − 1n-1n −1, the number becomes 000 after you press it once.
As a compulsive patient, Xiao Ming had to set the right time on his watch. If the time on the watch is 111 more than the current time, you need to press the n− 1N-1N −1 plus button to set the correct time.
Xiao Ming thought, if the watch can add a button, which means to add the current number KKK how good ah…
He wanted to know how many times, if he had the +k+k+ K button, he would have to press the optimal strategy button to move from any one minute to any other minute.
Note that when pressing the +k+ K + K button, if the number exceeds N −1n-1n−1 after KKK is added, NNN will be modulo.
For example, if n=10n=10n=10 and k=6k=6k=6, assume that the current time is 000 and press the +k+k+ K button 222 times, then the current time is 222.
The input
Two integers, NNN, KKK. 0
The output
An integer in a row indicates the maximum number of times a key must be pressed from one time to another in accordance with the optimal policy.
Train of thought
Dynamic programming DP
\left[0,\:n\:-\:1\right][0,n−1] \left[0,\:n\:-\:1\right]
Dp [I]dp[I]dp[I]dp[I] indicates the number of times + I + I + I. By default, only one +1+1+1 button is available.
Then need to initialize the number of k + k +, k + dp [I] = dp [I] – k + 1 dp \ left \ [I \ right] : = \ : dp \ left \ [I \ : – \ : k right] \ : + \ : 1 dp [I] = dp [I] – k + 1
State transition equation
code
#include<bits/stdc++.h>
using namespace std;
int dp[100005];
int main(a) {
int n, k;
cin >> n >> k;
// Initializes the dp state
for (int i = 0; i < n; i++) {
dp[i] = i; The default value is once a minute.
}
for (int i = k; i < n; i += k) {
dp[i] = dp[i - k] + 1; // Starting at 0, +1 for every k step
}
int sum = - 1;
while (1) { // If the result converges, stop the loop.
int now_sum = 0;
for (int i = 1; i < n; i ++) {
now_sum += dp[i];
// State transition equation
dp[i] = min(dp[(i + n - k) % n] + 1, dp[i - 1] + 1);
}
if (now_sum == sum) break;
sum = now_sum;
}
int ans = dp[0];
for (int i = 0; i < n; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
return 0;
}
Copy the code
E. piling up his building blocks
Topic describes
Xiao Ming is very interested in building blocks. His blocks are all cubes of the same size.
When building blocks, Xiaoming selects MMM blocks as the foundation, and places them in a line on the table with no gaps in the middle, calling it the 0 layer.
Then, Xiao Ming can put the 111th layer, the 222nd layer on it… , can be placed at most to layer NNN. Three rules must be followed when placing the blocks:
Rule 111: Each block must be placed immediately above one block, aligned with the block next to it;
Rule 222: Blocks in the same floor must be placed consecutively, with no gaps between them.
Rule 333: Do not place blocks where Ming does not like them.
Among them, the position that Xiao Ming does not like is marked on the drawing. The drawing has a total of NNN lines, and each line from bottom to top corresponds to layer 1 to layer NNN of the building block.
Each line has MMM characters, which can be ‘. ‘or’ X ‘, where ‘X’ indicates that this position is not xiaoming’s preference.
Now, Ming wants to know how many different ways to place the blocks. He found you at the Bluebridge Cup to help him figure it out.
Because the answer may be large, you only need to answer the answer to 100000000710000000071000000007 (1000000007) the results of modulus.
Note: Leaving nothing on the foundation is one option.
The input
The first line of input data has two positive integers n and M, which indicate the size of the drawing. N < = n < = n < = 100, 100 100 m < = 100 m < = 100 m < = 100
Then n lines, each with MMM characters, describe the drawing. Each character can only be ‘. ‘or’ X ‘.
The output
Output an integer, says the answer the results of 100000000710000000071000000007 after the modulus.
The sample input
2, 3,
..X
.X.
Sample output
4
The sample explain
Successful placement (where O stands for placing blocks) :
(1).. X .X. (2) .. X OX. (3) O.X OX. (4) .. X .XOCopy the code
Train of thought
Dynamic programming
Check [I][j]check[I][j]check[I]
= VDP dp [l] [r] [r] [l] = VDP [l] [r] = v: according to the current layer in [l, r] [l, r] [l, r] are VVV number method
Checkcheckcheck is a prefix and operation that performs the prefix and operation on each layer
We specify that the blocks from bottom to top are layer NNN, layer N − 1n-1N −1, and layer 1 at the top
We first update the value of the lowest DPDPDP with checkCheckCheck
Then from the lowest level to pass upward, that is, from the large interval enumeration to the cell after the number of methods
The transfer equation is: Dp [l] [r] + = dp/l – 1 + dp [l] [r] [r + 1) – dp/l – 1 dp [r + 1] [l] [r] + = dp/l – 1 + dp [l] [r] [r + 1] – dp/l – 1 dp [r + 1] [l] [r] + = dp [r] [l – 1] + dp [l] [r + 1) – dp/l – 1] [r + 1)
Since our L and R start from both ends and move inward, we update the interval from the outside to the inside. When updating the interval [L,r][L,r][L, R], We know the value of the interval [- 1 l] [r] [l – 1] [r] [r] [l – 1] and interval [l] [r + 1] [l] [r + 1] [l] [r + 1] interval [- 1 l] [r + 1] [l – 1] [r + 1] [l – 1] (r + 1), Because dp [r] [l – 1] dp [r] [l – 1] dp [r] and [l – 1] dp [l] [r + 1] [l] [r + 1] [l] [r + 1) is included in the [l – 1, r + 1] + [l, r] [r + l – 1, 1] + [l, r] [l – 1, r + 1] + [l, r] so to lose
code
#include<bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
typedef long long ll;
const int maxn = 110;
ll dp[maxn][maxn];
int check[maxn][maxn];
int main(a) {
int m, n;
char str[maxn];
cin >> n >> m;
// Read in data
for (int i = 1; i <= n; i++) {
scanf("%s", str + 1);
for (int j = 1; j <= m; j++) {
// check preprocesses the number of X's in the first j of layer I
check[i][j] = check[i][j - 1];
if (str[j] == 'X') check[i][j]++; }}// Default is also a result
ll ans = 1;
// Initializes the lowest level of data, using both sides to move towards the middle
for (int i = 1; i <= m; i++) {
for (int j = m; j >= i; j--) {
// check[n][j] -check [n][i-1] == 0
if (check[n][j] - check[n][i - 1] = =0) {
ans ++;
/ / l = I r = j this interval can be species = = [l] I [r = j + 1] + [j] [l = I - 1] - [l = I - 1] [r = j + 1) + 1
dp[i][j] = dp[i][j + 1] + dp[i - 1][j] - dp[i - 1][j + 1] + 1; }}}// Start state transition, cycle the number of layers
for (int t = n - 1; t > 0; t--) {
// Have the same state transition expression as initializing the underlying data
// Enumerate all cases at each level
for (int i = 1; i <= m; i++) {
for (int j = m; j >= i; j--) {
// [I,j] no X is pushed up
// [I,j] can't stack blocks with X
if (check[t][j] - check[t][i - 1] = =0) {
ans = (ans + dp[i][j]) % mod;
dp[i][j] = (dp[i][j] + dp[i - 1][j] + dp[i][j + 1] - dp[i - 1][j + 1]) % mod;
} else dp[i][j] = 0;
}
}
}
cout << ans << endl;
return 0;
}
Copy the code
F. Matrix summation
Topic describes
Through the test of all written interview, xiao Ming into MacrohardMacrohardMacrohard company success.
Today Xiao Ming’s task is to fill in this form:
The table has NNN rows and NNN columns, and both rows and columns are numbered from 111.
Where the value of the JJJ element in row III is GCD (I,j) GCD (I,j) GCD (I,j) squared,
GCDGCDGCD represents the greatest common divisor. Here are the first four columns of the first four rows of this table:
1 1 1 1 1 4 1 4 1 9 1 1 4 1 16Copy the code
Xiao Ming suddenly had a strange idea. He wanted to know the sum of all the elements in the table.
The input
A line of positive integers NNN see the meaning of the problem. n<=107n <= 10^7n<=107
The output
A number in a row representing the sum of all elements. Since the answer is large, please output the result after the module (109+710^9 + 7109+7)(i.e. : 1.007 billion)
The input
The output
Train of thought
This problem violent idea must TLE search the big guy’s idea, need to use euler function and Mobius inversion to do
The problem formula expression ans = ∑ ∑ I = 1 n I = 1 n the GCD (I, j) 2 ans \ : = \ sum _ ^ n \ {I = 1} : \ sum _ ^ n \ {I = 1} : the GCD \ left (I, \ : j \ right) ^ 2 ans = ∑ ∑ I = 1 n I = 1 NGCD (I, j) 2
The GCD (I, j) = d the GCD \ left (I, \ : j, right), : = \ : d \ : the GCD (I, j) = d (I, j) \ : \ left (I, \ : j, right), : (I, j) the greatest common divisor of d \ : dd
Count (d) \ : count \ left \ \ right (d) : count d (d) it means greatest common divisor of \ “d \ : d by the number of pairs
Count (d) ⋅ ⋅ d d count \ left \ \ right (d) : \ cdot \ “d \ : \ cdot \” d \ : count (d) ⋅ ⋅ d d is solving for the greatest common divisor for d \ : d \ : d and the number of all, Count (d)\:count (d)\ left(d)\ right)count(d)
Make 𝑖 = 𝑖 d, 𝑗 = 𝑗 d. \ : 𝑖 = \ frac {𝑖} {d}, \ “𝑗 = \ frac {𝑗} {d}. I = di, j = DJ. Substitute 𝑔𝑐𝑑(𝑖,𝑗)=1. \ : 𝑔 𝑐 𝑑 \ left (𝑖, 𝑗 \ right) = 1. The GCD (I, j) = 1.
The maximum common divisor ranges from [1,n][1, n][1,n], and DDD is any of [1,n][1, n][1,n].
Iii, the value of JJJ is in [1,n/d][1, n/d][1, n/d], and the number of all mutual pairs in the interval is solved. However, the data range of this problem is relatively large, so the euler function (linear sieve method), namely, the Euler function in 1− N1 − N1 − N, is solved by sieve method. The time complexity can be controlled within 𝑂(𝑛)𝑂(𝑛)O(n).
The derivation is as follows:
code
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1e7 + 10, mod = 1e9 + 7;
int n, cnt;
LL primes[N], eulers[N], s[N];
bool st[N];
// Linear sieve method
void get_eulers(int n)
{
eulers[1] = 1;
for (int i = 2; i <= n; ++i) {
if(! st[i]) { primes[cnt++] = i;// I is prime
eulers[i] = i - 1;
}
// Filter non-prime numbers
for (int j = 0; primes[j] <= n / i; ++j) {
int t = primes[j] * i;
st[t] = true;
if (i % primes[j] == 0) {
eulers[t] = eulers[i] * primes[j];
break;
}
eulers[t] = eulers[i] * (primes[j] - 1); }}// open LL to avoid overflow
s[1] = 1;
// I [I]
for (int i = 2; i <= n; ++i) {
s[i] = s[i - 1] + 2* eulers[i]; }}int main(a)
{
cin >> n;
get_eulers(n);
int res = 0;
for (int d = 1; d <= n; ++d) {
res = (res + (LL) s[n / d] * d % mod * d) % mod;
}
cout << res << endl;
return 0;
}
Copy the code