Past questions that I have never solved, the first time
I understood the D problem by looking at the answer, but it didn't seem like a natural solution for me and it took me a long time to chew ...
nothing special
answerA.py
print("ABC"+input())
You just have to find the largest $ 2 ^ k $ that meets the conditions, so log and finish
answerB.py
import math
n=int(input())
print(2**math.floor(math.log(n,2)))
Since a →? → b, you can find out what the number connected to a and the number connected to b are. TLE occurred frequently because I tried to loop in the loop where the first input was received. (** See the constraints even for simple problems **)
answerC.cc
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
int main(){
int n,m;cin >> n >> m;
vector<int> x;
bool f=false;
for(int i=0;i<m;i++){
int a,b;cin >> a >>b;
if(a==1){
x.push_back(b);
}
if(b==n){
x.push_back(a);
}
}
int l1=x.size();
set<int> y(x.begin(),x.end());
if(l1==y.size()){
cout << "IMPOSSIBLE" << endl;
}else{
cout << "POSSIBLE" << endl;
}
}
answerC.py
n,m=map(int,input().split())
x=[]
for i in range(m):
a,b=map(int,input().split())
if a==1:
x.append(b)
if b==n:
x.append(a)
l=len(x)
if len(set(x))==l:
print("IMPOSSIBLE")
else:
print("POSSIBLE")
answerD1.py、answerD2.py is$2$<=N<=$50$、$0$<=$a_i$<=$50 \times 10^{16}$Because it does not meet each of the above, it has become TLE.
First, looking at the constraints, we can see that it seems better to do as many n as possible. So let's fix ** n to 50 and ** think about it.
Here, it is not possible to simulate the entire operation due to the constraint of $ 10 ^ {16} $, so ** Is it possible to narrow down the patterns that can be constraints during operation? ** or ** What is happening in the final state? You need to think about ** first (joseki). If you do an experiment here, you will find out something if you follow it in reverse. Then, in this problem, on the contrary, you can discover the ** cycle ** by following ** accurately **.
First of all, regarding the final state, it will be easier to understand if you arrange 0 to 49 of length 50 in ascending order. (Experimentally shows that reverse simulation becomes troublesome when there are multiple same numbers.)
Then, if you add 50 in order from 1st to 50th and add -1 to other elements, you can simulate exactly the opposite. Also, since the total increases by 1 for each inverse simulation from 1st to 50th (from $ 50 + (-1) \ times (50-1) $), think first about k divided by 50. You can see that you should simulate the remaining number of times and then consider how much the whole is added in the remaining cycle.
When the above is implemented, it becomes ```answerD3.py```.
#### **`answerD1.py`**
```python
k=int(input())
print(2)
if k%2==0:
print(str(k//2)+" "+str(k//2+1))
else:
print(str(k//2)+" "+str(k//2+2))
answerD2.py
k=int(input())
n=k+1
if n==1:
print("2")
print("0 1")
else:
print(n-1+k*n,end="")
for i in range(n-1):
print(" 0",end="")
print()
answerD3.py
k=int(input())
x=[0]*50
k1=k//50
k2=k%50
for i in range(k2):
x[k2-i-1]=50-i
for i in range(k2+1,50):
x[i]=i-k2
for i in range(50):
x[i]+=k1
print(50)
print(" ".join(map(str,x)))
Recommended Posts