【AtCoder】ARC064

ARC064

C - Boxes and Candies

先把每一個盒子都消到xc++

而後從前日後推,要求第二個的上界是x-前一個spa

由於咱們要求靠後的那個儘可能小,會對後面的修改影響儘可能小code

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 +c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}
int N;
int64 a[MAXN],x,ans;
void Solve() {
    read(N);read(x);
    for(int i = 1 ; i <= N ; ++i) {
    read(a[i]);
    if(a[i] > x) {
        ans += a[i] - x;
        a[i] = x;
    }
    }
    int64 t = 0;
    for(int i = 2 ; i <= N ; ++i) {
    int64 m = x - a[i - 1];
    if(a[i] > m) {t += a[i] - m;a[i] = m;}
    }
    out(ans + t);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

D - An Ordinary Game

最後的串長度的奇偶性是必定的orm

例如兩端是a和c,最後的串必定是acac,或者acacacacget

兩端是a的話最後的必定是aba,或者ababa,b能夠換成任意字母it

咱們看看兩個串的長度的差值是奇數仍是偶數class

而後根據奇偶性判斷勝負便可date

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 +c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}
char s[MAXN];
int L;
void Solve() {
    scanf("%s",s + 1);
    L = strlen(s + 1);
    if(s[1] == s[L]) {
    --L;
    if(L & 1) puts("First");
    else puts("Second");
    }
    else {
    if(L & 1) puts("First");
    else puts("Second");
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

E - Cosmic Rays

就是跑一遍dij就行,兩個圓之間的距離要麼是0要麼是圓心距減兩個半徑循環

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 1005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 +c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}
struct Point {
    db x,y;
    Point(db _x = 0.0,db _y = 0.0) {x = _x;y = _y;}
    friend Point operator + (const Point &a,const Point &b) {
    return Point(a.x + b.x,a.y + b.y);
    }
    friend Point operator - (const Point &a,const Point &b) {
    return Point(a.x - b.x,a.y - b.y);
    }
    db norm() {
    return sqrt(x * x + y * y);
    }
}P[MAXN],S,T;
db R[MAXN],dis[MAXN];
int N;
bool vis[MAXN];
void Solve() {
    scanf("%lf%lf%lf%lf",&S.x,&S.y,&T.x,&T.y);
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
    scanf("%lf%lf%lf",&P[i].x,&P[i].y,&R[i]);
    dis[i] = max(0.0,(S - P[i]).norm() - R[i]);
    }
    for(int i = 1 ; i <= N ; ++i) {
    int u = -1;
    for(int j = 1 ; j <= N ; ++j) {
        if(!vis[j]) {
        if(u == -1) u = j;
        else if(dis[j] < dis[u]) u = j;
        }
    }
    vis[u] = 1;
    for(int j = 1 ; j <= N ; ++j) {
        if(!vis[j]) {
        dis[j] = min(dis[j],dis[u] + max(0.0,(P[u] - P[j]).norm() - R[u] - R[j]));
        }
    }
    }
    db ans = (S - T).norm();
    for(int i = 1 ; i <= N ; ++i) {
    ans = min(ans,dis[i] + max(0.0,(P[i] - T).norm() - R[i]));
    }
    printf("%.10lf\n",ans);
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

F - Rotated Palindromes

\(f(i)\)爲長度爲i個迴文串個數,不含循環節map

\(f(i) = K^{\lceil \frac{i}{2} \rceil} - \sum_{d|i}f(d)\)

因爲i只有\(O(\sqrt{N})\)個且轉移關係並很少,因此\(f(i)\)均可以很快求出來

而後咱們答案就是

\(\sum_{d|N}d\cdot f(d)[d \%2 == 1] + \sum_{d|N}\frac{d}{2} \cdot f(d)[d \% 2 == 0]\)

由於d若是是2的倍數,轉一半的時候會構成一個新的d長度迴文串,重複統計了

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 1005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 +c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N,K;
map<int,int> zz;
int inc(int a,int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
void update(int &x,int y) {
    x = inc(x,y);
}
int fpow(int x,int c) {
    int res = 1,t = x;
    while(c) {
    if(c & 1) res = mul(res,t);
    t = mul(t,t);
    c >>= 1;
    }
    return res;
}
int f(int x) {
    if(x == 1) return K;
    if(zz.count(x)) return zz[x];
    int res = fpow(K,x / 2);
    if(x & 1) res = mul(res,K);
    for(int i = 1 ; i <= x / i ; ++i) {
    if(x % i == 0) {
        update(res,MOD - f(i));
        int j = x / i;
        if(j != i && j != x) update(res,MOD - f(j));
    }
    }
    zz[x] = res;
    return res;
}
void Solve() {
    read(N);read(K);
    int ans = 0;
    for(int i = 1 ; i <= N / i; ++i) {
    if(N % i == 0) {
        int t = mul(f(i),i);
        if(i % 2 == 0) t = mul(t,(MOD + 1) / 2);
        update(ans,t);
        int j = N / i;
        t = mul(f(j),j);
        if(j % 2 == 0) t = mul(t,(MOD + 1) / 2);
        if(j != i) update(ans,t);
    }
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}
相關文章
相關標籤/搜索