UOJ Round #1 [数论 | DP 排列]

UOJ Round #1

难度很良心啊!
做出了前两题,第三题看到仙人掌就吓哭了。


【UR #1】缩进优化

就是求
\[ \sum_{i=1}^n a_i - (x-1)\sum_{i=1}^n\lfloor \frac{a_i}{x} \rfloor \]
最小值。

调和级数\(O(nlogn)\)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2e6+5, mo = 998244353;
inline int read() {
    char c=getchar(); int x=0,f=1;
    while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}

int n, a[N], m, s[N];
ll ans = 1e18, sum = 0;
ll cal(int x) { //printf("cal %d\n", x);
    int lim = m/x;
    ll ans = 0;
    for(int i=1; i<=lim; i++) ans += i * (s[(i+1) * x - 1] - s[i * x -1]);
    //printf("ans %lld   %lld\n", ans, sum - (x-1) * ans);
    return sum - (x-1) * ans;
}
int main() {
    freopen("in", "r", stdin);
    n = read();
    for(int i=1; i<=n; i++) a[i] = read(), m = max(m, a[i]), s[a[i]]++, sum += a[i];
    for(int i=1; i<=m<<1; i++) s[i] += s[i-1];// printf("%d ", s[i]); puts("  s");
    for(int x=1; x<=m; x++) ans = min(ans, cal(x));
    printf("%lld\n", ans);
}

【UR #1】外星人

题意:

n序列\(a_i\),对于一个排列,x按顺序分别对他们取模,最后得到y,求\(\mid x-y\mid\)最小值以及对应的排列方案数。

题解:

看到排列,当然想到按某种顺序一个个插入

从小到大插入,\(f[i][j]\)表示前i个一开始数是j,最后得到的最大值和方案数

第i个数要产生影响只能放在开头

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
#define fir first
#define sec second
const int N = 1005, M = 5005, mo = 998244353;
inline int read() {
    char c=getchar(); int x=0,f=1;
    while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}

int n, x, a[N];
pair<int, int> f[N][M];
int main() {
    freopen("in", "r", stdin);
    n = read(); x = read();
    for(int i=1; i<=n; i++) a[i] = read();
    sort(a+1, a+n+1);
    
    for(int j=0; j<=x; j++) f[1][j] = make_pair(j % a[1], 1);
    for(int i=2; i<=n; i++)
        for(int j=0; j<=x; j++) {
            pair<int, int> x(f[i-1][j].fir, (ll) (i-1) * f[i-1][j].sec %mo), y = f[i-1][j % a[i]];
            if(x.fir == y.fir) f[i][j] = make_pair(x.fir, (x.sec + y.sec) %mo);
            else f[i][j] = max(x, y);
        }
    
    printf("%d\n%d", f[n][x].fir, f[n][x].sec);
}

【UR #1】跳蚤国王下江南

20分暴力!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
#define fir first
#define sec second
const int N = 1005, mo = 998244353;
inline int read() {
    char c=getchar(); int x=0,f=1;
    while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}

int n, m, u, v;
struct edge {int v, ne;} e[N<<2];
int cnt, h[N];
inline void ins(int u, int v) {
    e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
    e[++cnt] = (edge) {u, h[v]}; h[v] = cnt;
}
bool vis[N];
int ans[N];
void dfs(int u, int l) {
    ans[l] ++;
    vis[u] = 1;
    for(int i=h[u]; i; i=e[i].ne) 
        if(!vis[e[i].v]) dfs(e[i].v, l+1);
    vis[u] = 0;
}
int main() {
    freopen("in", "r", stdin);
    n = read(); m = read();
    for(int i=1; i<=m; i++) ins(read(), read());
    dfs(1, 0);
    for(int i=1; i<n; i++) printf("%d\n", ans[i]);
}
posted @ 2017-05-14 11:24 Candy? 阅读(...) 评论(...) 编辑 收藏