1. 819B Mister B and PR Shifts

大意: 给定排列$p$, 定义排列$p$的特征值为$\sum |p_i-i|$, 可以循环右移任意位, 求最小特征值和对应移动次数.

右移过程中维护增加的个数和减少的个数即可.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head const int N = 1e6+;
int n, a[N];
int dl[N], dr[N]; int main() {
scanf("%d",&n);
REP(i,,n) scanf("%d",a+i);
ll ret = , ans = ;
int L = , R = ;
REP(i,,n) {
ret += abs(a[i]-i);
if (a[i]>i) {
++L;
--dl[a[i]-i];
++dr[a[i]-i];
}
else if (a[i]<=i) {
++R;
if (a[i]!=) {
--dl[n-i+a[i]];
++dr[n-i+a[i]];
}
}
}
ans = ret;
int pos = ;
REP(i,,n-) {
ret += R-L;
R += dr[i];
L += dl[i];
if (a[n-i+]!=) --R,++L;
ret -= abs(a[n-i+]-n-);
ret += abs(a[n-i+]-);
if (ret<ans) pos = i, ans = ret;
}
printf("%lld %d\n", ans, pos);
}

2. 819C Mister B and Beacons on Field

大意: 给定两个平面点$A(m,0),B(0,n)$

  • 求$A$移向原点过程中, 有多少个时刻, 存在一个点$C$使得ABC面积为$S$
  • $A$在原点, $B$移向原点过程中, 有多少个时刻, 存在一个点$C$使得ABC面积为$S$

对于第一问, 假设$C$坐标为$(x,y)$, $A$返回时坐标为$(t,0)$

那么有$xn+ty=2S+tn, 0\le t\le m$

就等价于求$[0,m]$中有多少个$t$满足$gcd(n,t)|2S$

对于第二问, 假设$B$返回时坐标为$(0,t)$

那么有$xt=2S, 1\le t\le m$

等价于求$[1,n]$中有多少个$t$满足$t|2S$

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head const int N = 1e6+;
int gpf[N];
vector<pii> B,C,S;
ll ans,n,m,s; void solve(vector<pii> &A, int x) {
while (x!=) {
int p = gpf[x], cnt = ;
while (x%p==) x/=p,++cnt;
A.pb(pii(p,cnt));
}
}
void repr(vector<pii> &A) {
sort(A.begin(),A.end());
vector<pii> ret;
for (auto &t:A) {
if (ret.empty()||t.x!=ret.back().x) ret.pb(t);
else ret.back().y += t.y;
}
A = ret;
}
void init() {
int n1,n2,n3,m1,m2,m3,s1,s2,s3;
scanf("%d%d%d%d%d%d%d%d%d",&n1,&n2,&n3,&m1,&m2,&m3,&s1,&s2,&s3);
B.clear(),C.clear(),S.clear();
n = (ll)n1*n2*n3, m = (ll)m1*m2*m3, s = (ll)s1*s2*s3;
solve(B,n1),solve(B,n2),solve(B,n3),repr(B);
S.pb(pii(,));
solve(S,s1),solve(S,s2),solve(S,s3),repr(S);
}
void dfs(int d, ll num, int z) {
if (!num) return;
if (d==C.size()) return ans+=num*z,void();
dfs(d+,num,z);
REP(i,,C[d].y+) num/=C[d].x;
dfs(d+,num,-z);
}
void dfs2(int d, ll num) {
if (num>n) return;
if (d==S.size()) return ++ans,void();
dfs2(d+,num);
REP(i,,S[d].y) num*=S[d].x,dfs2(d+,num);
}
void work() {
init();
int sz = S.size(), now = ;
REP(i,,sz-) {
while (now<B.size()&&B[now].x<S[i].x) C.pb(pii(B[now++].x,));
if (now<B.size()&&B[now].x==S[i].x) {
if (B[now].y>S[i].y) C.pb(S[i]);
++now;
}
}
while (now<B.size()) C.pb(pii(B[now++].x,));
ans = ;
dfs(,m,),dfs2(,);
printf("%lld\n", ans);
} int main() {
gpf[] = ;
REP(i,,N-) if (!gpf[i]) {
for(int j=i;j<N;j+=i) gpf[j]=i;
}
int t;
scanf("%d", &t);
while (t--) work();
}

Codeforces Round #421 (Div. 1) (BC)的更多相关文章

  1. Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts

    Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts 题意:给一个长度为\(n\)的排列,每次可以向右循环移位一次,计算\(\sum_{i= ...

  2. Codeforces Round #500 (Div. 2) BC

    CodeForces 1013B And CodeForces 1013C  Photo of The Sky B 可以发现只有一次与操作是有意义的,所以答案只有-1,0,1,2四种情况 #inclu ...

  3. 【Codeforces Round #421 (Div. 2) B】Mister B and Angle in Polygon

    [题目链接]:http://codeforces.com/contest/820/problem/B [题意] 给你一个正n边形; 然后让你在这正n边行中选3个点,组成一个角; 找出角的大小和所给的角 ...

  4. 【Codeforces Round #421 (Div. 2) A】Mister B and Book Reading

    [题目链接]:http://codeforces.com/contest/820/problem/A [题意] 每天看书能看v页; 且这个v每天能增加a; 但是v有上限v1; 然后每天还必须往回看t页 ...

  5. Codeforces Round #421 (Div. 2) - B

    题目链接:http://codeforces.com/contest/820/problem/B 题意:给定一个正n边形,然后让你选择3个不同的顶点,使得这3个顶点形成的角度尽可能的接近a. 思路:首 ...

  6. Codeforces Round #421 (Div. 2) - A

    题目链接:http://codeforces.com/contest/820/problem/A 题意:一个人在看一本书,书一共C页,这个人每天看v0页,但是他又开始加速看这本书,每天都比前一天多看a ...

  7. Codeforces Round #421 (Div. 2)

    A: 题意:给你一本书共c页,第一天看v0页,第二天看v0+a,第二天看v0+2a以此类推,每天最多看v1页,但是后一天要重复看前一天的后l页. 代码: #include<stdio.h> ...

  8. Codeforces Round #421 (Div. 2)D - Mister B and PR Shifts(模拟)

    传送门 题意 给出n个数,计算在进行n-1次右移中\(min\sum_{i=1}^nabs(p_i-i)\) 分析 我们设置cnt[p[i]-i]为一个数p[i]与它标准位置(如1的标准位置为1)的左 ...

  9. Codeforces Round #421 (Div. 2)B. Mister B and Angle in Polygon(模拟+精度控制)

    传送门 题意 给出正n多边形和一个数a,寻找与a最接近的角,输出角编号 分析 找出多边形上所有角,一一比对即可 trick 1.判断的时候注意精度,i.e.x-eps>0 2.double与do ...

随机推荐

  1. PCA python 实现

    PCA 实现: 参考博客:https://blog.csdn.net/u013719780/article/details/78352262 from __future__ import print_ ...

  2. 【Java/Csv/Regex】用正则表达式去劈分带引号的csv文件行,得到想要的行数据

    csv文件是用引号分隔的文本行,为了完善内容人们又用引号把每个区块的内容又包了起来,于是形成下面的文件: "1","2","3"," ...

  3. Eclipse 的快捷键以及文档注释、多行注释的快捷键 一、多行注释快捷键

    一.多行注释快捷键 1.选中你要加注释的区域,用ctrl+shift+C 或者ctrl+/ 会加上//注释2.先把你要注释的东西选中,用shit+ctrl+/ 会加上/*    */注释 3.以上快捷 ...

  4. flutter 页面布局 Paddiing Row Column Expanded 组件

    Flutter Paddiing 组件 在 html 中常见的布局标签都有 padding 属性,但是 Flutter 中很多 Widget 是没有 padding 属 性.这个时候我们可以用 Pad ...

  5. Java中字符串操作的基本方法总结:

    1.字母大小写转换: package com.imooc; public class SortDemo { public static void main(String[] args) { char ...

  6. 将C++资源文件读取出来

    HRSRC   hResource   =   FindResource(GetModuleHandle(NULL),     MAKEINTRESOURCE(IDR_CALC),   TEXT(&q ...

  7. locust设置断言的方法

    https://blog.csdn.net/panyu881024/article/details/80146088 这里同样以测试百度首页为例. catch_response = True :布尔类 ...

  8. SQL语句 update 字段=字段+字符串 拼接

    update user_info set user_name = concat(user_name,'呵呵呵') where user_name = '哈哈哈';

  9. jQuery调用WebService返回JSON数据

    相信大家都比较了解JSON格式的数据对于ajax的方便,不了解的可以从网上找一下这方面的资料来看一下,这里就不多说了,不清楚的可以在网上查一下,这里只说一下因为参数设置不当引起的取不到返回值的问题. ...

  10. adb中文乱码问题怎么解决?

    1.chcp 65001      执行完命令后 2.在cmd命令弹出的终端页面,右键单击属性设置成字体Lucida Console ,设置完成就解决了 adb 中文乱码问题