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, ...
随机推荐
- k8s 集群部署--学习
kubernetes是google开源的容器集群管理系统,提供应用部署.维护.扩展机制等功能,利用kubernetes能方便管理跨集群运行容器化的应用,简称:k8s(k与s之间有8个字母) Pod:若 ...
- nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)
现象 1.centos6.9 用rpm包安装nginx 2.修改Nginx的多个配置文件和配置项 3.service nginx restart 报错: nginx: [emerg] open() & ...
- php 判断图片文件的真实类型
/** * * 检测文件的真实类型 * * @param string $srcPath 文件路径 * * @return string $realType 文件真实类型 * */ $imgurl = ...
- oracle 的自定义的存储函数遇到的 package or function is in an invalid state
转: oracle 的自定义的存储函数遇到的 package or function is in an invalid state 2017-10-28 11:08:17 major_tom 阅读数 ...
- Python3入门(十三)——常用内置模块之摘要模块hashlib/hmac
(1)hashlib hashlib提供了常见摘要算法:如MD5,SHA1等等 一个md5的加密示例如下: import hashlib m = hashlib.md5() m.update(&quo ...
- C# WinForm程序中使用Unity3D控件 (转)
https://www.cnblogs.com/cnxkey/articles/5394378.html 最近在自学Unity3D,打算使用这个时髦.流行.强大的游戏引擎开发一个三维业务展示系统,不过 ...
- 【DeepCTR】
DeepFM: https://www.jianshu.com/p/6f1c2643d31b CCPM,FGCNN: https://cloud.tencent.com/developer/artic ...
- kafka删除topic后再创建同名的topic报错(ERROR org.apache.kafka.common.errors.TopicExistsException)
[hadoop@datanode3 logs]$ kafka-topics.sh --delete --zookeeper datanode1:2181 --topic firstTopic firs ...
- 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...
- 【ARTS】01_43_左耳听风-201900902~201900908
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...