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]);
}

UOJ Round #1 [数论 | DP 排列]的更多相关文章

  1. 【UOJ Round #1】

    枚举/DP+排列组合 缩进优化 QAQ我当时一直在想:$min\{ \sum_{i=1}^n (\lfloor\frac{a[i]}{x}\rfloor + a[i] \ mod\ x) \}$ 然而 ...

  2. 【UOJ Round #5】

    构造+贪心/数论 为什么只有两个标题呢……因为第二题我不会…… 怎样提高智商 构造题……然而一开始半天我都yy不出来…… 后来我想:这题应该不会特别麻烦,而且既然样例只给了1,可能再给大一点就让人发现 ...

  3. UOJ Round #15 [构造 | 计数 | 异或哈希 kmp]

    UOJ Round #15 大部分题目没有AC,我只是水一下部分分的题解... 225[UR #15]奥林匹克五子棋 题意:在n*m的棋盘上构造k子棋的平局 题解: 玩一下发现k=1, k=2无解,然 ...

  4. 【BZOJ】2111: [ZJOI2010]Perm 排列计数 计数DP+排列组合+lucas

    [题目]BZOJ 2111 [题意]求有多少1~n的排列,满足\(A_i>A_{\frac{i}{2}}\),输出对p取模的结果.\(n \leq 10^6,p \leq 10^9\),p是素数 ...

  5. 【BZOJ】4559: [JLoi2016]成绩比较 计数DP+排列组合+拉格朗日插值

    [题意]n位同学(其中一位是B神),m门必修课,每门必修课的分数是[1,Ui].B神碾压了k位同学(所有课分数<=B神),且第x门课有rx-1位同学的分数高于B神,求满足条件的分数情况数.当有一 ...

  6. 【UOJ Round #8】

    A 一道不错的题,虽然大家都觉得是水题,然而蒟蒻我想出来的好慢……Orz alpq 发现其实就是一个网格图,每一个大块都是同一颜色……横纵坐标互不干扰…… //UOJ Round #8 A #incl ...

  7. 【bzoj1408】[Noi2002]Robot 数论+dp

    题目描述 输入 输出 样例输入 3 2 1 3 2 5 1 样例输出 8 6 75 题解 语文题+数论+dp 花了大段讲述什么叫mu,什么叫phi,只是新定义的mu将2看作有平方因子,新定义的phi( ...

  8. [LOJ#516]「LibreOJ β Round #2」DP 一般看规律

    [LOJ#516]「LibreOJ β Round #2」DP 一般看规律 试题描述 给定一个长度为 \(n\) 的序列 \(a\),一共有 \(m\) 个操作. 每次操作的内容为:给定 \(x,y\ ...

  9. LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律

    二次联通门 : LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 /* LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 set ...

随机推荐

  1. Linux编译安装Mariadb数据库

    一.安装cmake cd /usr/local/src tar zxvf cmake-2.8.12.1.tar.gz cd cmake-2.8.12.1 ./configure 注意报错需要安装gcc ...

  2. Spring框架学习笔记(10)——Spring中的事务管理

    什么是事务 举例:A给B转500,两个动作,A的账户少500,B的账户多500 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 一.注解添加事务管理方 ...

  3. Dev中GridControl的GridView 基本样式设置

    基本样式图: 代码如下: /// <summary> /// gridView样式 /// </summary> /// <param name="gdv&qu ...

  4. 番外篇--Moddule Zero启动模板

    1.3 ABPZero - 启动模板 1.3.1 简介 使用ABP和moudle-zero开始一个新项目的最简单的方式是在模板页创建模板.记住要勾选 Include module zero. 在创建并 ...

  5. asp.net网站管理工具 遇到错误。请返回上一页并重试。

    原因:项目的路径里有“#”号.

  6. APACHE服务器出现No input file specified.的完美解决方案

    转自:http://www.upupw.net/server/n53.html 启用REWRITE的伪静态功能的时候,首页可以访问,而访问内页的时候,就提示:"No input file s ...

  7. https和http有什么区别

    在URL前加https://前缀表明是用SSL加密的. 你的电脑与服务器之间收发的信息传输将更加安全. Web服务器启用SSL需要获得一个服务器证书并将该证书与要使用SSL的服务器绑定. http和h ...

  8. SSH问题:系统启动时,spring配置文件解析失败,报”cvc-elt.1: 找不到元素 'beans' 的声明“异常

    现象:spring加载配置文件applicationContext.xml出错,抛出nested exception is og.xml.sax.SAXParseException; lineNumb ...

  9. Spring 数据库连接(Connection)绑定线程(Thread)的实现

    最近在看spring事务的时候在想一个问题:spring中的很多bean都是单例的,是非状态的,而数据库连接是一种有状态的对象,所以spring一定在创建出connection之后在threadloc ...

  10. 配置SESSION超时与请求超时

    <!--项目的web.xml中 配置SESSION超时,单位是min.用户在线时间.如果不设置,tomcat下的web.xml的session-timeout为默认.--><sess ...