# preface

I think this set of questions is a little difficult. In contrast, bloggers are real vegetable dogs.
But as long as Captain lm Make it clear MUV LUV EXTRASister Huan Kill Forest Program , if I torture one by one, it will be a great victory for the vegetable dog blogger!

# Decimal (number theory)

## General idea of the topic

Give an integer n n n. Ask 1 n \frac{1}{n} n1 is an infinite decimal.

## thinking

if n n n is 2 2 2 m m m power( m > = 0 m>=0 m> = 0) or by a a a 2 2 2 and b b b 5 5 Multiply by 5, then 1 n \frac{1}{n} n1 is a finite decimal.

## AC code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+100;

int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
while(n%2==0){
n/=2;
}
while(n%5==0){
n/=5;
}
if(n==1) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
}


# Invoker (dynamic programming)

## General idea of the topic

In the game Dota2, there is a character called Invoker. He has three basic skills: Quas, Wex and export.
Invoker has three element slots. At the beginning of the game, the element slot is empty.

• When Invoker uses the Quas skill, he will obtain the Q element;
• When Invoker uses Wex skill, he will obtain W element;
• When Invoker uses the export skill, he will obtain the E element;

When the element slot is full of elements, the element obtained by the next skill release will replace the element with the earliest element obtained in the element slot.

In addition to these three basic skills, Invoker also has 10 10 10 special skills. Each special skill has three elements:

• Rapid cooling Cold Snap requires element QQQ, represented by Y;
• Ghost Walk requires the element QQW, represented by V;
• Ice Wall requires element QQE, represented by G;
• Electromagnetic pulse EMP requires the element WWW, represented by C;
• Strong attack on Tornado requires element QWW, represented by X;
• Agile Alacrity requires element WWE, represented by Z;
• When Yangyan strikes Sun Strike, it needs element EEE, expressed by T;
• Forge Spirit, a furnace spirit, requires the element QEE, which is represented by F;
• Chaos Meteor requires element WEE, represented by D;
• The element QWE, represented by B, is required for the Deafening Blast;

If players want to release special skills M M M. You need to adjust the element slot of Invoker to a special skill first M M M the required element type, and then press R R Press the R key to use the element call to fuse the elements obtained by yourself to release special skills M M M.

The three basic skills correspond to three independent keys. You need to press them every time you release a special skill R R R key. Elements will not disappear after being used to release special skills, and the chronological order will not change.

At the beginning of the game, the system gives a release sequence of special skills s t r str str, you need to cast all special skills in order.
How many times do you need to press the key at least?

## thinking

Bloggers are not good at it d p dp dp, this is also a description of huge length and many situations d p dp dp. When I looked at the problem, I didn't want to smash the table. I thought which guy made the problem.
It's very nice. The boss drinks tea (● '◡' ●).

The topic is very interesting. I also went to have a look at it Introduction to this hero in Dota2.
Unexpectedly, there are so many skills in Invoker. I'm afraid bloggers can't play.
No nonsense, let's look at the problem.

First, we need to understand when Invoker can release special skills.
If we want to release the skill "wall of ice" at the beginning, the element required by "wall of ice" is QQE. Then a question arises: can the order of elements be changed? For example, can EQQ release the "wall of ice"?
The answer is yes. The question mentioned unordered element combination, which means unordered sequence. From this, we can know that as long as the elements in the current element slot are the same as the elements required by the skill, we can press the R key to release the skill regardless of the order in the element slot.

Then we assume: press 1 to release Quas, press 2 to release Wex, and press 3 to release export.
In this case, the key sequence of each skill is as follows:

After we convert the release of each skill into numbers, it is not difficult to see that all key schemes of a skill are the full arrangement of its corresponding number sequence.

In this case, for special skills M M For M, we can get at least one release mode of it, so as to find out all key schemes through full arrangement. Obviously, the title has given us:

Then we need to design a function to obtain a key scheme for the special skills to be used:

int press[5]; //press[1]: first key, press[2]: second key, press[3]: third key

void init(int pos)//Update the value of press array according to the key scheme given in the title
{
if(str[pos]=='Y')
press[1]=press[2]=press[3]=1;
else if(str[pos]=='V')
press[1]=press[2]=1,press[3]=2;
else if(str[pos]=='G')
press[1]=press[2]=1,press[3]=3;
else if(str[pos]=='C')
press[1]=press[2]=press[3]=2;
else if(str[pos]=='X')
press[1]=1,press[2]=press[3]=2;
else if(str[pos]=='Z')
press[1]=press[2]=2,press[3]=3;
else if(str[pos]=='T')
press[1]=press[2]=press[3]=3;
else if(str[pos]=='F')
press[1]=1,press[2]=press[3]=3;
else if(str[pos]=='D')
press[1]=2,press[2]=press[3]=3;
else if(str[pos]=='B')
press[1]=1,press[2]=2,press[3]=3;
}


Next, let's talk about the meaning of dp array. The value of dp[i][x][y][z] is the minimum number of operations required to complete the first I skills in the skill sequence str in the order of xyz. xyz belongs to one of the key schemes of the i-th skill.

int dp[maxn][5][5][5];//dp[i][x][y][z]: the minimum number of operations required to type the first I skills in xyz order


Take the example XDTBVV as an example.
The first skill is "storm hurricane" (X). Its key schemes include 122, 212 and 221 (because the R key is a key that must be pressed, it is omitted here). Then d p [ 0 ] [ 1 ] [ 2 ] [ 2 ] = d p [ 0 ] [ 2 ] [ 1 ] [ 2 ] = d p [ 0 ] [ 2 ] [ 2 ] [ 1 ] = 4 dp[0][1][2][2]=dp[0][2][1][2]=dp[0][2][2][1]=4 dp[0][1][2][2]=dp[0][2][1][2]=dp[0][2][2][1]=4.
( 4 4 The reason for 4 is that at the beginning, the element slot is empty, so you have to press the skill key three times and the R key once anyway 4 4 (4 times)

The following is the key part. If you can't grasp it, please read the question again and again + see the explanation above.

Assuming that we have released the ith skill, we need to release the (i+1) th skill. Since the (i+1) skill has multiple key schemes, we need to analyze them one by one.
Suppose we want to use the first release method of the (i+1) skill, and list all the possible keys:

### 1, We are lucky that after releasing the i-th skill, the last two elements in the element slot are exactly the first two elements required by the first release method.

At this point, we just need to cast the skill corresponding to the element we lack, and then press R R R key, you can cast the (i+1) skill.

for(int x=1; x<=3; x++)//Enumerate which element is the first in the element slot
dp[pos][press[1]][press[2]][press[3]]=min(dp[pos-1][x][press[1]][press[2]]+2,dp[pos][press[1]][press[2]][press[3]]);


### 2, Luck is OK. After releasing the i-th skill, the last element in the chronological order in the element slot is exactly the first element required by the first release method.

At this point, we need to cast the skills corresponding to the two missing elements in order, and then press R R R key, you can cast the (i+1) skill.

for(int x=1; x<=3; x++)//Enumerate which element is the first in the element slot
for(int y=1; y<=3; y++)//Enumerate which is the second element in the element slot
dp[pos][press[1]][press[2]][press[3]]=min(dp[pos-1][x][y][press[1]]+3,dp[pos][press[1]][press[2]][press[3]]);


### 3, Unfortunately, after releasing the i-th skill, there is nothing to use in the element slot. We need to cast the skill again.

For example, in the current element slot 312, the next required elements and their order are 333. No element in this 312 will be left in the casting of the next skill (the first one) 3 3 3 will be acquired by you next 3 3 3) replace it.
At this point, we need to cast the skills corresponding to the three elements in the first key scheme in order, and then press R R R key, you can cast the (i+1) skill.

for(int x=1; x<=3; x++)//Enumerate which element is the first in the element slot
for(int y=1; y<=3; y++)//Enumerate which is the second element in the element slot
for(int z=1; z<=3; z++)//Enumerate which is the third element in the element slot
dp[pos][press[1]][press[2]][press[3]]=min(dp[pos-1][x][y][z]+4,dp[pos][press[1]][press[2]][press[3]]);


### 4, Luckily, the i skill and (i+1) are the same skill, okay

At this point, we just need to press again R R R key, you can cast the (i+1) skill.

dp[i][press[1]][press[2]][press[3]]=min(dp[i][press[1]][press[2]][press[3]],dp[i-1][press[1]][press[2]][press[3]]+1);


Since we don't know which of the above possibilities will actually happen, we must take them all into account.

void solve(int pos,int cnt) //To the i-th character, suppose you need to press the cnt key now
{
if(cnt==1)
{
for(int x=1; x<=3; x++)
dp[pos][press[1]][press[2]][press[3]]=min(dp[pos-1][x][press[1]][press[2]]+2,dp[pos][press[1]][press[2]][press[3]]);
}
else if(cnt==2)
{
for(int x=1; x<=3; x++)
for(int y=1; y<=3; y++)
dp[pos][press[1]][press[2]][press[3]]=min(dp[pos-1][x][y][press[1]]+3,dp[pos][press[1]][press[2]][press[3]]);
}
else if(cnt==3)
{
for(int x=1; x<=3; x++)
for(int y=1; y<=3; y++)
for(int z=1; z<=3; z++)
dp[pos][press[1]][press[2]][press[3]]=min(dp[pos-1][x][y][z]+4,dp[pos][press[1]][press[2]][press[3]]);
}
}

for(int j=1; j<=3; j++)
{
solve(i,j);
//Press the R key only
dp[i][press[1]][press[2]][press[3]]=min(dp[i][press[1]][press[2]][press[3]],dp[i-1][press[1]][press[2]][press[3]]+1);
}


Then it's done. Finally, traverse the dp value corresponding to all key schemes of the last skill and find the minimum.
Bloggers are lazy and don't want to list all key schemes corresponding to a skill, so I use next_permutation skipped class.
(because it is to find the full arrangement of 3 numbers, you can skip class)

do
{
/**Code body**/
}while(next_permutation(press+1,press+4));


## AC code

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e5+100;

int dp[maxn][5][5][5];//dp[i][x][y][z]: the minimum number of operations required to type the ith skill in xyz order
int press[5];
string str;
void init(int pos)
{
if(str[pos]=='Y')
press[1]=press[2]=press[3]=1;
else if(str[pos]=='V')
press[1]=press[2]=1,press[3]=2;
else if(str[pos]=='G')
press[1]=press[2]=1,press[3]=3;
else if(str[pos]=='C')
press[1]=press[2]=press[3]=2;
else if(str[pos]=='X')
press[1]=1,press[2]=press[3]=2;
else if(str[pos]=='Z')
press[1]=press[2]=2,press[3]=3;
else if(str[pos]=='T')
press[1]=press[2]=press[3]=3;
else if(str[pos]=='F')
press[1]=1,press[2]=press[3]=3;
else if(str[pos]=='D')
press[1]=2,press[2]=press[3]=3;
else if(str[pos]=='B')
press[1]=1,press[2]=2,press[3]=3;
}

void solve(int pos,int cnt)
{
if(cnt==1)
{
for(int x=1; x<=3; x++)
dp[pos][press[1]][press[2]][press[3]]=min(dp[pos-1][x][press[1]][press[2]]+2,dp[pos][press[1]][press[2]][press[3]]);
}
else if(cnt==2)
{
for(int x=1; x<=3; x++)
for(int y=1; y<=3; y++)
dp[pos][press[1]][press[2]][press[3]]=min(dp[pos-1][x][y][press[1]]+3,dp[pos][press[1]][press[2]][press[3]]);
}
else if(cnt==3)
{
for(int x=1; x<=3; x++)
for(int y=1; y<=3; y++)
for(int z=1; z<=3; z++)
dp[pos][press[1]][press[2]][press[3]]=min(dp[pos-1][x][y][z]+4,dp[pos][press[1]][press[2]][press[3]]);
}
}
int main()
{
while(cin>>str)
{
int len=str.size();
for(int i=0; i<len; i++)
for(int x=1; x<=3; x++)
for(int y=1; y<=3; y++)
for(int z=1; z<=3; z++)
dp[i][x][y][z]=inf;
///Initialize the first skill
init(0);
do
{
dp[0][press[1]][press[2]][press[3]]=4;

}
while(next_permutation(press+1,press+4));
for(int i=1; i<len; i++)
{
init(i);
int cnt=1;
do
{
for(int j=1; j<=3; j++)
{
solve(i,j);
dp[i][press[1]][press[2]][press[3]]=min(dp[i][press[1]][press[2]][press[3]],dp[i-1][press[1]][press[2]][press[3]]+1);
}
}
while(next_permutation(press+1,press+4));
//cout<<endl;
}
int ans=inf;
init(len-1);
do
{
ans=min(ans,dp[len-1][press[1]][press[2]][press[3]]);
}
while(next_permutation(press+1,press+4));
cout<<ans<<endl;
}
}


# Angle beats (computational geometry)

## General idea of the topic

First give the coordinates of n points, and then ask q times.
Each inquiry will give a new point A. how many combinations of n+1 points (including point A) can form a right triangle and contain point a?

## thinking

Big man's method https://www.cnblogs.com/carcar/p/11688108.html , after reading it, it opened up.

It is similar to the D question of the previous ABC (rest assured, the ABC is already writing, and there will be all that should be).
Problem solving ideas start from the knowledge learned in high school: if vector A=(x1, y1) is perpendicular to vector B=(x2,y2), then x1*x2+y1*y2=0.
If three points can form A right triangle, we should consider where point A is:

### 2, Point A is not A right angle point.

At this time, A has two positions, but does it affect? No, it's not A big problem.

Due to the small amount of data( n < = 2000 , q < = 2000 n<=2000,q<=2000 N < = 2000, Q < = 2000), so we can solve this problem by enumerating right angles.
How to store vectors is very clear in the boss's blog (the same is true for the D question of ABC).

Finally, one more point. Although the time given was 15s, I actually ran down twice.
Finally, it was found that it was a map [] delay, so we need to find someone to ask about efficiency. I remember once that map.find() was much faster than my map [].

//T dropped code
if(mp[getk(-y,x)])
ans[i]+=mp[getk(-y,x)];

//AC code (14086ms, quite extreme)
if(mp.count(getk(-y,x)))
ans[i]+=mp[getk(-y,x)];


## AC code

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e5+100;

int ans[2050];
pair<int,int> a[2050],b[2050];

pair<int,int> getk(int x,int y){
int m=__gcd(x,y);
x/=m,y/=m;
if(x<0) x=-x,y=-y;
else if(x==0&&y<0) y=-y;
return make_pair(x,y);
}
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q)){
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].first,&a[i].second);
for(int i=1;i<=q;i++)
scanf("%d%d",&b[i].first,&b[i].second),ans[i]=0;
map<pair<int,int>,int> mp;
for(int i=1;i<=q;i++){
mp.clear();
for(int j=1;j<=n;j++){
int x=a[j].first-b[i].first;
int y=a[j].second-b[i].second;
mp[getk(x,y)]++;
if(mp.count(getk(-y,x)))
ans[i]+=mp[getk(-y,x)];
}
}
for(int i=1;i<=n;i++){
mp.clear();
for(int j=1;j<=n;j++){
if(i==j) continue;
int x=a[i].first-a[j].first;
int y=a[i].second-a[j].second;
mp[getk(x,y)]++;
}
for(int j=1;j<=q;j++){
int x=b[j].first-a[i].first;
int y=b[j].second-a[i].second;
if(mp.count(getk(-y,x)))
ans[j]+=mp[getk(-y,x)];
}
}
for(int i=1;i<=q;i++)
printf("%d\n",ans[i]);
}
}


# Later words

Thank you for reading. I hope it can be of some use to you.

The following lines are taken from the silver soul: the end chapter - the house of all things forever:
(if you are in a bad mood, go to see the silver soul. If an episode is not enough, go to a chapter, or go to the theater version.)

"The sin I should bear" "No matter how many times I do it again, I'll show you." "Your dirty curses" "No matter how many times I will bear it and show you" "No matter how many times you intend to curse me" "No matter how many times these hands intend to destroy the world" "My world will not be destroyed" "With your curse and my sinful hands" "You can't separate me from these stupid friends" I think twice every day: is it better? Brush questions or not? Happy or not? Updated, but not daily; Brushed; Excited Although the road is endless and faraway, I still want to pursue the truth in the world.

Tags: C++ ICPC

Posted on Thu, 11 Nov 2021 13:49:23 -0500 by beeman000