AtCoder Beginner Contest 161 (D (priority queue), E (simulation), F (mathematical derivation))

Title link

D - Lunlun Number

Topic: It is good to define a number: the absolute value of the difference between each adjacent bit is less than or equal to 1, enter k, and ask you to find the kth smallest good number.

In practice, the priority queue is put into 1,2,3,4,5,6,7,8,9 one at a time, there are three variants that can be put back:

Connect the current single digit at the end, the single digit minus one digit plus one

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int a[200],len;
int main()
{int k;
   cin>>k;
   if(k<=12){
      cout<<k;
      return 0;
   }
   priority_queue<ll,vector<ll>,greater<ll>> pq;
   for(int i=1;i<=9;i++)
   pq.push(i);

   int cnt=0;
   ll ans;
   while(true){
      ll num=pq.top();
      pq.pop();
      cnt++;

      if(cnt==k){
         ans=num;
         break;
      }

      int ld=num%10;
      ll num1=num*10+ld;
      pq.push(num1);

      if(ld!=0){
         num1=num*10+ld-1;
         pq.push(num1);
      }
      if(ld!=9){
         num1=num*10+ld+1;
         pq.push(num1);
      }
   }
   cout<<ans;
}

E-Yutori

Topic: Enter the string s of length n, k, c and n.

You have to choose the 'o' character in the s string to work for k days. The protagonist is a slacker, and you have to rest for c days every time you work.

Q. When working for at least k days, which days are required to work?

Practice: Simulate greedy, anyway, simulating greedy, there is a simulation of working days greater than k then there is no answer. Otherwise it is just the intersection of the two simulations.

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N=2e5+10;
int n,k,c,pre[N];
char s[N];
int main()
{
    cin>>n>>k>>c;
    cin>>s+1;
    set<int>st1,st2;
    for(int i=1;i<=n;++i){
        if(s[i]=='x') continue;
        else{
            st1.insert(i);
            i+=c;
        }
    }

    for(int i=n;i>=1;--i){
        if(s[i]=='x') continue;
        else st2.insert(i),i-=c;
    }
    if(st1.size()>k||st2.size()>k) return 0;
    else{
        for(int v:st1){
            if(st2.count(v)) printf("%d\n",v);
        }
    }
}

F - Division or Substraction

Topic: Give you an N and ask for your K's number of legal solutions.

Selection conditions for K:

If K divides N by N, then N = N / K

Otherwise N = NK

Continue to operate so that N finally equals 1.

Practice: No, the search solution comes from: this

Just think about it: when n = 0, N% (N-1) == 1 Then the factor of N% (N-1) is also equal to 1.

When n> 0, the only factor that can divide N is N. After reading the problem, the solution is very simple. If you do n’t look at the problem, you wo n’t think about it.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n;cin>>n;

    set<ll> st;
    for(ll i=2;i*i<=n;i++){
        if(n%i==0){
            ll t=n;
            while(t%i==0) t/=i;
            if(t%i==1) st.insert(i);
        }
    }
    for(ll i=1;i*i<=n-1;i++){
        if((n-1)%i==0){
            st.insert(i);
            st.insert((n-1)/i);
        } 
    }
    st.erase(1);
    st.insert(n);

    cout<<st.size()<<"\n";
    
    return 0;
}

 

Published 519 original articles · praised 69 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_41286356/article/details/105329129