Codeforces Round #415 (Div. 1) (CDE)
1. CF 809C Find a car
大意: 给定一个$1e9\times 1e9$的矩阵$a$, $a_{i,j}$为它正上方和正左方未出现过的最小数, 每个询问求一个矩形内的和.
可以发现$a_{i,j}=(i-1)\oplus (j-1)+1$, 暴力数位$dp$即可
#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 //求 0<=i<=x, 0<=j<=y, 0<=i^j<=k的所有i^j的和
int f[][][][], g[][][][];
void add(int &a, ll b) {a=(a+b)%P;}
//f为个数, g为和
int calc(int x, int y, int k) {
if (x<||y<) return ;
memset(f,,sizeof f);
memset(g,,sizeof g);
f[][][][] = ;
PER(i,,)REP(a1,,)REP(a2,,)REP(a3,,) {
int &r = f[i+][a1][a2][a3];
if (!r) continue;
int l1=a1&&!(x>>i&),l2=a2&&!(y>>i&),l3=a3&&!(k>>i&);
REP(b1,,)REP(b2,,) {
if (l1&&b1) continue;
if (l2&&b2) continue;
if (l3&&(b1^b2)) continue;
int c1=a1&&b1>=(x>>i&),c2=a2&&b2>=(y>>i&),c3=a3&&(b1^b2)>=(k>>i&);
add(f[i][c1][c2][c3],r);
add(g[i][c1][c2][c3],g[i+][a1][a2][a3]*2ll+(b1^b2)*r);
}
}
int ans = ;
REP(a1,,)REP(a2,,)REP(a3,,) add(ans,g[][a1][a2][a3]+f[][a1][a2][a3]);
return ans;
} int main() {
int t;
scanf("%d", &t);
while (t--) {
int x1,y1,x2,y2,k;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
--x1,--y1,--x2,--y2,--x1,--y1,--k;
int ans = (calc(x2,y2,k)-calc(x1,y2,k)-calc(x2,y1,k)+calc(x1,y1,k))%P;
if (ans<) ans += P;
printf("%d\n", ans);
}
}
2. CF 809D
3. CF 809E Surprise me!
大意: 给定树, 点$i$权值$a_i$, $a$为$n$排列, $dis(x,y)$为$x,y$路径上的边数, 求$\frac{1}{n(n-1)}\sum\limits_{x\not = y} \varphi(a_xa_y)dis(x,y)$
设$b=a^{-1}$, 转化为求$\sum\limits_{i=1}^n\sum\limits_{j=1}^n \varphi(ij)dis(b_i,b_j)$
根据$\varphi(ij)=\frac{\varphi(i)\varphi(j)gcd(i,j)}{\varphi(gcd(i,j))}$
反演一下可以化为$\sum\limits_{T=1}^n\Bigg(\sum\limits_{d\mid T}\frac{d}{\varphi(d)}\mu(\frac{T}{d})\Bigg)\sum\limits_{i=1}^{\lfloor\frac{n}{T}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{n}{T}\rfloor}\varphi(iT)\varphi(jT)dis(b_{iT},b_{jT})$
考虑枚举$T$, 左边的$\sum\limits_{d\mid T}\frac{d}{\varphi(d)}\mu(\frac{T}{d})$可以线性筛预处理出来.
右边的就是$\sum\limits_{i=1}^{\lfloor\frac{n}{T}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{n}{T}\rfloor}\varphi(iT)\varphi(jT)({dep}_{b_{iT}}+{dep}_{b_{jT}}-2{dep}_{lca(b_{iT},b_{jT})}) $
预处理一下$\sum\limits_{i=1}^{\lfloor\frac{n}{T}\rfloor}\varphi(iT)$, 那么$\sum\limits_{i=1}^{\lfloor\frac{n}{T}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{n}{T}\rfloor}\varphi(iT)\varphi(jT)({dep}_{b_{iT}}+{dep}_{b_{jT}})$就很容易求出.
现在只需要考虑求$\sum\limits_{i=1}^{\lfloor\frac{n}{T}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{n}{T}\rfloor}\varphi(iT)\varphi(jT){dep}_{lca(b_{iT},b_{jT})}$
有效的点数是$O(\frac{n}{T})$的, 提出来建一棵虚树然后$DP$即可
那么总的复杂度就为$O(nlogn)$
#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,dep[N],fa[N],son[N],top[N];
int phi[N],a[N],b[N],sz[N];
int p[N], cnt, vis[N];
int h[N],L[N],R[N],f[N],s[N],sum[N];
vector<int> g[N],gg[N];
void init() {
phi[] = h[] = ;
for (int i=; i<N; ++i) {
if (!vis[i]) p[++cnt]=i,phi[i]=i-,h[i]=inv(i-);
for (int j=,t; j<=cnt&&i*p[j]<N; ++j) {
vis[t=i*p[j]]=;
if (i%p[j]==) {phi[t]=phi[i]*p[j],h[t]=;break;}
phi[t]=phi[i]*phi[p[j]];
h[t]=(ll)h[i]*h[p[j]]%P;
}
}
}
void dfs(int x, int f, int d) {
L[x]=++*L,dep[x]=d,sz[x]=,fa[x]=f;
for (int y:g[x]) if (y!=f) {
dfs(y,x,d+),sz[x]+=sz[y];
if (sz[y]>sz[son[x]]) son[x]=y;
}
R[x]=*L;
}
void dfs(int x, int tf) {
top[x]=tf;
if (son[x]) dfs(son[x],tf);
for (int y:g[x]) if (!top[y]) dfs(y,y);
}
int lca(int x, int y) {
while (top[x]!=top[y]) {
if (dep[top[x]]<dep[top[y]]) swap(x,y);
x = fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
bool cmp(int x, int y) {
return L[x]<L[y];
} int DP(int x) {
int ans = (ll)f[x]*f[x]%P*dep[x]%P;
sum[x] = f[x];
for (int y:gg[x]) {
ans = (ans+DP(y))%P;
ans = (ans+2ll*sum[y]*sum[x]%P*dep[x])%P;
sum[x] = (sum[x]+sum[y])%P;
}
return ans;
} int solve(vector<int> &a) {
sort(a.begin(),a.end(),cmp);
int sz = a.size();
REP(i,,sz-) a.pb(lca(a[i],a[i-]));
a.pb();
sort(a.begin(),a.end(),cmp);
a.erase(unique(a.begin(),a.end()),a.end());
s[cnt=]=a[],sz=a.size();
REP(i,,sz-) {
while (cnt>=) {
if (L[s[cnt]]<=L[a[i]]&L[a[i]]<=R[s[cnt]]) {
gg[s[cnt]].pb(a[i]);
break;
}
--cnt;
}
s[++cnt]=a[i];
}
int t = DP();
for (auto &x:a) gg[x].clear(),f[x]=sum[x]=;
return t;
} int main() {
init();
scanf("%d",&n);
REP(i,,n) scanf("%d",a+i),b[a[i]]=i;
REP(i,,n) {
int u, v;
scanf("%d%d",&u,&v);
g[u].pb(v),g[v].pb(u);
}
dfs(,,),dfs(,);
int ans = ;
REP(T,,n) {
int ret = , sum = ;
REP(i,,n/T) sum = (sum+phi[i*T])%P;
vector<int> v;
REP(i,,n/T) {
ret=(ret+2ll*sum*phi[i*T]%P*dep[b[i*T]])%P;
v.pb(i*T);
}
for (auto &t:v) f[b[t]] = phi[t], t = b[t];
ret = (ret-2ll*solve(v))%P;
ans = (ans+(ll)h[T]*ret)%P;
}
ans = (ll)ans*inv(n)%P*inv(n-)%P;
if (ans<) ans += P;
printf("%d\n",ans);
}
Codeforces Round #415 (Div. 1) (CDE)的更多相关文章
- Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- Codeforces Round #542(Div. 2) CDE 思维场
C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...
- Codeforces Round#415 Div.2
A. Straight «A» 题面 Noora is a student of one famous high school. It's her final year in school - she ...
- Codeforces Round #762 (Div. 3), CDE
(C) Wrong Addition Problem - C - Codeforces 题意 定义一种计算方式, 对于a+b=c, 给出a和c, 求b 题解 因为求法是从个位求得, 先求出来的最后输 ...
- Codeforces Round #415(Div. 2)-810A.。。。 810B.。。。 810C.。。。不会
CodeForces - 810A A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes i ...
- Codeforces Round #415 Div. 1
A:考虑每对最大值最小值的贡献即可. #include<iostream> #include<cstdio> #include<cmath> #include< ...
- Codeforces Round #415 (Div. 2)C
反正又是一个半小时没做出来... 先排序,然后求和,第i个和第j个,f(a)=a[j]-a[i]=a[i]*(2^(j-i-1))因为从j到i之间有j-i-1个数(存在或者不存在有两种情况) 又有a[ ...
- Codeforces Round #415 (Div. 2) A B C 暴力 sort 规律
A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #415 (Div. 2) C. Do you want a date?
C. Do you want a date? 2 seconds 256 megabytes Leha decided to move to a quiet town Vičkopolis, ...
随机推荐
- Java TreeSet,Collections使用
一.创建TreeSet实例 public static void main(String[] args) { TreeSet set = new TreeSet(); set.add("C& ...
- ubuntu安装IntelliJ Idea及图标创建
一.下载并解压安装 二.创建桌面程序 1. cd /usr/local/applications/ 2. vi idea.desktop 3. 内容如下 [Desktop Entry] Name=In ...
- 如何使能hyper-v的增强功能?
1. 在hyper-v的设置中使能增强功能 2. 运行在hyper-v中的虚拟机(笔者使用ubuntu版本为bionic)中安装xrdp 2.1 获取安装脚本 $ git clone https:// ...
- Typora 精美而强大的Markdown编辑器
Typora 精美而强大的Markdown编辑器 转 https://www.jianshu.com/p/45e284645d30 Markdown编辑器千千万,可是有颜值.功能强并且免费的,就没有 ...
- OctetString 转String
/// <summary> /// OctetString转时间 /// </summary> /// <param name="ss">字符串 ...
- flutter GridView 网格布局
当数据量很大的时候用矩阵方式排列比较清晰.此时我们可以用网格列表组件 GridView 实 现布局. GridView 创建网格列表有多种方式,常用有以下两种. 1.可以通过 GridView.cou ...
- IDEA中spring约束文件报红 的解决办法
- 123457123456#0#-----com.tym.niuniuChengYu05--前拼后广--最牛成语tym
com.tym.niuniuChengYu05--前拼后广--最牛成语tym
- 有哪些可能的原因导致BDE死掉啊啊,求救了,谢谢
一个服务程序在后台跑,用到了BDE访问SQLServer,隔了一段时间就会莫名其妙的与数据库连接失败,查了一下BDE,发现已经死掉了,bde administrator也无法打开,报错,重启或注销机器 ...
- antd 用 customize-cra 方式引入 sass
antd 用 customize-cra 方式引入 sass 只需要安装:node-sass 即可