每日刷題記錄 11-18

xxb3.1415發表於2020-11-19

Number Sequence

Number Sequence
kmp裸題

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  }
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e6+5;
int nx[N], a[N], b[N];
int kmp(int n, int m) {
  nx[1] = 0;
  int k = 0;
  for(int i = 1; i < m; ++i) {
    while(k && b[k] != b[i]) k = nx[k];
    if(b[k] == b[i]) k++;
    nx[i+1] = k;
  }
  for(int i = 0, j = 0; i < n;++i) {
    while(j && a[i] != b[j]) j = nx[j];
    if(a[i] == b[j]) j++;
    if(j == m) {
      return i - m + 2;
    }
  }
  return -1;
}
int main() {
  int t; scanf("%d", &t);
  b[0] = INF;
  while(t--) {
    int n, m; scanf("%d %d", &n, &m);
    for(int i = 0; i < n; ++i) scanf("%d", a + i);
    for(int i = 0; i < m; ++i) scanf("%d", b + i);
    printf("%d\n", kmp(n, m));
  }
   
	return 0;
}

The Super Powers

The Super Powers
如果一個數可以表示成兩個不同數的次方形式,那麼這個數就是滿足條件的,求所有滿足條件的數。
可以發現任何數的合數次方都是滿足條件的,用vector存下來,再去重即可。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  } 
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
bool vi[65];
int main() {
  
  vector<__int128> ans = {1};
  
  for(int i = 2; i <= 64; i++) {
    for(int j = i + i; j <= 64; j += i) {
      vi[j] = 1;
    }
  }
  
  __int128 mm = 1;
  mm <<= 64;
  mm--;
  for(__int128 i = 2; i <= 100000; i++) {
    __int128 an = 1;
    for(int j = 1; j <= 64; j++) {
      an *= i;
      if(an > mm) break;
      if(vi[j]) ans.push_back(an);
    }
  }
  sort(ans.begin(), ans.end());
  ans.erase(unique(ans.begin(), ans.end()), ans.end() );
  for(auto it : ans) {
    io::write(it);
    putchar('\n');
  }
}

Bi-shoe and Phi-shoe

Bi-shoe and Phi-shoe
裸上 尤拉函式

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  }
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 2e6;
int tot, prime[N+5], phi[N];
bool vis[N+5];
int qiu[N+5];
void init() {
  phi[1] = 1;
  for(int i = 2; i <= N; ++i) {
    if(!vis[i]) {
      prime[++tot] = i;
      phi[i] = i-1;
    }
    for(int j = 1; j <= tot && prime[j] * i <= N; ++j) {
      vis[i*prime[j]] = 1;
      if(i % prime[j]) {
        phi[i * prime[j]] = phi[i] * phi[prime[j]];
      } else {
        phi[i * prime[j]] = phi[i] * prime[j];
        break;
      }
    }
  }
  
  
  for(int i = 2; i <= N; ++i) {
    if(!qiu[phi[i]]) {
      qiu[phi[i]] = i;
    }
  }
  int i = N-1;
  while(!qiu[i]) i--;
  i--;
  for(i; i >= 1; i--) {
    if(!qiu[i]) qiu[i] = qiu[i+1];
    qiu[i] = min(qiu[i], qiu[i+1]);
  }  
}
int main() {
  init(); int t, ca = 1; scanf("%d", &t);
  while(t--) {
    int n, x;
    LL ans = 0; scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
      scanf("%d", &x);
      ans += qiu[x];
    }
    printf("Case %d: %lld Xukha\n", ca++, ans);
  }
	return 0;
}

Beautiful numbers

Beautiful numbers
數位 d p dp dp d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k] i i i位最大公約數位 j j j,模 2520 2520 2520 k k k

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef unsigned long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  }
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
int cnt, a[23], ton[2525];
LL dp[23][50][2520];
int lcm(int a, int b) {
  return a * b / __gcd(a, b);
}
LL dfs(int step, bool limit, int lc, int mod) {
  if(!step) return mod % lc == 0;
  
  if(!limit && ~dp[step][ton[lc]][mod]) return dp[step][ton[lc]][mod];
  int r = limit ? a[step] : 9;
  LL ans = 0;
  for(int i = 0; i <= r; ++i) {
    if(i == 0) ans += dfs(step-1, limit && i == a[step], lc, mod*10%2520);
    else ans += dfs(step-1, limit && i == a[step], lcm(lc, i), (mod * 10 + i) % 2520);
  }
  if(!limit) dp[step][ton[lc]][mod] = ans;
  return ans;
} 
LL solve(LL x) {
  cnt = 0;
  while(x) {
    a[++cnt] = x % 10;
    x /= 10;
  }
  return dfs(cnt, 1, 1, 0);
}
int main() {
  memset(dp, -1, sizeof dp);
  int t; cin >> t;
  for(int i = 1; i <= 2520; i++) if(2520 % i == 0) ton[i] = cnt++;
	while(t--) {
	  LL l, r; cin >> l >> r;
	  cout << solve(r) - solve(l-1) << endl;
  }
  return 0;
}

Qin Shi Huang’s National Road System

Qin Shi Huang’s National Road System
參照次小生成樹的思路,加入一條邊之後就變成了一個環,把環中最大的邊去掉就是題目中的B,A就是加入的這條邊的兩個點的權值和。用Kruskal+啟發式合併來預處理兩個點之間經過邊的最大值,然後再列舉刪的邊即可。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
namespace io{
  template <typename T> inline void read(T &x) {
    x = 0; T f = 1;char s = getchar();
    for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
    for(;  isdigit(s); s = getchar()) x = (x << 3) + (x << 1) + (s ^ 48);
    x *= f;
  }
  template <typename T> inline void write(T x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) write(x/10);
    putchar(x%10+'0');
  }
};
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i <  (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define _srep(n,m,i)for (register int i = (n); i >= (m); i--)
#define _sfor(n,m,i)for (register int i = (n); i >  (m); i--)
#define ef(u, i) for(int i = head[u]; i; i = e[i].next)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) x & (-x)
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e3+5;
double x[N], y[N], p[N];
struct node {
  int u, v;
  double w;
  bool operator < (const node &a) const{
    return w < a.w;
  }
}e[N*N];
int a[N];
double mx[N][N];
int Find(int x) {
  return x == a[x] ? x : a[x] = Find(a[x]);
}
double getdis(double x, double y, double a, double b) {
  return sqrt((x-a)*(x-a) + (y-b) * ((y-b)));
}
vector<int> ve[N];
int main() {
  int t; 
  io::read(t);
  while(t--) {
    int n, tot = 0; 
    io::read(n);
    
    for(int i = 1; i <= n; i++) {
      scanf("%lf %lf %lf", x + i, y + i, p + i);
      a[i] = i;
      ve[i].clear(); ve[i].push_back(i);
      for(int j = 1; j < i; j++) {
        e[tot++] = {i, j, getdis(x[i], y[i], x[j], y[j])};
      }
    }
    
    sort(e, e + tot);
    int num = 0, fx, fy;
    double b = 0;
    for(int i = 0;  i < tot; ++i) {
      fx = Find(e[i].u), fy = Find(e[i].v);
      if(fx != fy) {
        num++;
        b += e[i].w;
        if(ve[fx].size() < ve[fy].size()) swap(fx, fy);
        a[fy] = fx;
        for(auto u : ve[fx]) {
          for(auto v : ve[fy]) {
            mx[u][v] = mx[v][u] = e[i].w;
          }
        } 
        for(auto v : ve[fy]) {
          ve[fx].push_back(v);
        }
        ve[fy].clear();
      }
      if(num == n-1) break;
    }
    
    double ans = 0;
    for(int i = 1; i <= n; ++i) {
      for(int j = 1; j < i; ++j) {
        ans = max(ans, (p[i] + p[j]) / (b - mx[i][j]));
      } 
    }
    printf("%.2lf\n", ans);
  }
  return 0;
}