数学(容斥计数):LNOI 2016 方
Description
Input
Output
仅一行一个正整数, 代表正方形个数对 100000007( 10^8 + 7) 取模之后的值
Sample Input
1 0
1 2
0 1
2 1
Sample Output
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
const int Mod=;
typedef long long LL;
LL Abs(LL x){return x<?-x:x;}
LL n,m,k,tot,x[N],y[N];
LL Ask(LL a,LL b,LL c){
LL ret,L;
if(!a||!b||!c)return ;
if(a>c-)a=c-;
if(b>c-)b=c-;
ret=(*c-a-)*a/;
if(c->=b+){
L=min(a,c-b-);
ret+=b*L;
ret-=(*c-L-)*L/;
}
return (ret%Mod+Mod)%Mod;
}
LL Solve1(){
LL ret=,L,R,U,D;
for(int i=;i<=k;i++){
L=min(x[i]-,y[i]-);ret+=L;
L=min(n+-x[i],y[i]-);ret+=L;
L=min(x[i]-,m+-y[i]);ret+=L;
L=min(n+-x[i],m+-y[i]);ret+=L;
}
for(int i=;i<=k;i++){
U=x[i]-;D=n+-x[i];
L=y[i]-;R=m+-y[i];
ret+=Ask(L,R,U);
ret+=Ask(L,R,D);
ret+=Ask(U,D,L);
ret+=Ask(U,D,R);
}
return ret%Mod;
} LL Solve2(){
LL ret=,L;
LL px,py,dx,dy;
for(int i=;i<=k;i++)
for(int j=i+;j<=k;j++){
dx=x[j]-x[i];
dy=y[j]-y[i]; px=x[j]+dy;py=y[j]-dx;
if(px>=&&px<=n+&&py>=&&py<=m+){
px=px-dx;py=py-dy;
if(px>=&&px<=n+&&py>=&&py<=m+)ret+=;
} px=x[j]-dy;py=y[j]+dx;
if(px>=&&px<=n+&&py>=&&py<=m+){
px=px-dx;py=py-dy;
if(px>=&&px<=n+&&py>=&&py<=m+)ret+=;
} if((x[i]+x[j])%==(y[i]+y[j])%){
dx=*x[i]-(x[i]+x[j]);
dy=*y[i]-(y[i]+y[j]);
px=(x[i]+x[j]+dy)/;
py=(y[i]+y[j]-dx)/;
if(px>=&&px<=n+&&py>=&&py<=m+){
dx=-dx;dy=-dy;
px=(x[i]+x[j]+dy)/;
py=(y[i]+y[j]-dx)/;
if(px>=&&px<=n+&&py>=&&py<=m+)ret+=;
}
}
}
return ret%Mod;
} LL hshx[N],hshy[N];
int cntx,cnty,vis[N][N]; void Prepare(){
for(int i=;i<=k;i++){
hshx[++cntx]=x[i];
hshy[++cnty]=y[i];
}
sort(hshx+,hshx+cntx+);
cntx=unique(hshx+,hshx+cntx+)-hshx-;
sort(hshy+,hshy+cnty+);
cnty=unique(hshy+,hshy+cnty+)-hshy-;
for(int i=;i<=k;i++){
LL x_=lower_bound(hshx+,hshx+cntx+,x[i])-hshx;
LL y_=lower_bound(hshy+,hshy+cnty+,y[i])-hshy;
vis[x_][y_]=i;
}
} int Q(LL x,LL y){
LL x_=lower_bound(hshx+,hshx+cntx+,x)-hshx;
LL y_=lower_bound(hshy+,hshy+cnty+,y)-hshy;
return (hshx[x_]==x&&hshy[y_]==y)*vis[x_][y_];
} LL Solve3(){
LL ret=;
LL px1,py1,px2,py2,dx,dy;
for(int i=;i<=k;i++)
for(int j=i+;j<=k;j++){
dx=x[j]-x[i];
dy=y[j]-y[i]; px1=x[j]+dy;py1=y[j]-dx;
if(px1>=&&px1<=n+&&py1>=&&py1<=m+){
px2=px1-dx;py2=py1-dy;
if(px2>=&&px2<=n+&&py2>=&&py2<=m+){
if(Q(px1,py1)>j)ret+=;
if(Q(px2,py2)>j)ret+=;
}
} px1=x[j]-dy;py1=y[j]+dx;
if(px1>=&&px1<=n+&&py1>=&&py1<=m+){
px2=px1-dx;py2=py1-dy;
if(px2>=&&px2<=n+&&py2>=&&py2<=m+){
if(Q(px1,py1)>j)ret+=;
if(Q(px2,py2)>j)ret+=;
}
} if((x[i]+x[j])%==(y[i]+y[j])%){
dx=*x[i]-(x[i]+x[j]);
dy=*y[i]-(y[i]+y[j]);
px1=(x[i]+x[j]+dy)/;
py1=(y[i]+y[j]-dx)/;
if(px1>=&&px1<=n+&&py1>=&&py1<=m+){
dx=-dx;dy=-dy;
px2=(x[i]+x[j]+dy)/;
py2=(y[i]+y[j]-dx)/;
if(px2>=&&px2<=n+&&py2>=&&py2<=m+){
if(Q(px1,py1)>j)ret+=;
if(Q(px2,py2)>j)ret+=;
}
}
}
}
return ret%Mod;
} LL Solve4(){
LL ret=;
LL px1,py1,px2,py2,dx,dy;
for(int i=;i<=k;i++)
for(int j=i+;j<=k;j++){
dx=x[j]-x[i];
dy=y[j]-y[i]; px1=x[j]+dy;py1=y[j]-dx;
if(px1>=&&px1<=n+&&py1>=&&py1<=m+){
px2=px1-dx;py2=py1-dy;
if(px2>=&&px2<=n+&&py2>=&&py2<=m+)
if(Q(px1,py1)>j&&Q(px2,py2)>j)ret+=;
} px1=x[j]-dy;py1=y[j]+dx;
if(px1>=&&px1<=n+&&py1>=&&py1<=m+){
px2=px1-dx;py2=py1-dy;
if(px2>=&&px2<=n+&&py2>=&&py2<=m+)
if(Q(px1,py1)>j&&Q(px2,py2)>j)ret+=;
} if((x[i]+x[j])%==(y[i]+y[j])%){
dx=*x[i]-(x[i]+x[j]);
dy=*y[i]-(y[i]+y[j]);
px1=(x[i]+x[j]+dy)/;
py1=(y[i]+y[j]-dx)/;
if(px1>=&&px1<=n+&&py1>=&&py1<=m+){
dx=-dx;dy=-dy;
px2=(x[i]+x[j]+dy)/;
py2=(y[i]+y[j]-dx)/;
if(px2>=&&px2<=n+&&py2>=&&py2<=m+)
if(Q(px1,py1)>j&&Q(px2,py2)>j)ret+=;
}
}
}
return ret%Mod;
} int main(){
freopen("square.in","r",stdin);
freopen("square.out","w",stdout);
scanf("%lld%lld%lld",&n,&m,&k);
for(LL L=min(m,n);L>=;L--){
tot+=(n-L+)*(m-L+)%Mod*L%Mod;
if(tot>=Mod)tot-=Mod;
}
for(int i=;i<=k;i++){
scanf("%lld%lld",&x[i],&y[i]);
x[i]+=;y[i]+=;
}
Prepare();
tot-=Solve1();tot%=Mod;
tot+=Solve2();tot%=Mod;
tot-=Solve3();tot%=Mod;
tot+=Solve4();tot%=Mod;
tot=(tot+Mod)%Mod;
printf("%lld\n",tot);
return ;
}
运算有些多,常数比较大,不开O2只有50分,优化的话,可以把LL改成int,Mod的时候有些地方可以改成减。
数学(容斥计数):LNOI 2016 方的更多相关文章
- 【BZOJ 4455】 [Zjoi2016]小星星 容斥计数
dalao教导我们,看到计数想容斥……卡常策略:枚举顺序.除去无效状态.(树结构) #include <cstdio> #include <cstring> #include ...
- BZOJ 2005 [Noi2010]能量采集 (数学+容斥 或 莫比乌斯反演)
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 4493 Solved: 2695[Submit][Statu ...
- Codeforces.997C.Sky Full of Stars(容斥 计数)
题目链接 那场完整的Div2(Div1 ABC)在这儿.. \(Description\) 给定\(n(n\leq 10^6)\),用三种颜色染有\(n\times n\)个格子的矩形,求至少有一行或 ...
- 一本通 1783 矩阵填数 状压dp 容斥 计数
LINK:矩阵填数 刚看到题目的时候感觉是无从下手的. 可以看到有n<=2的点 两个矩形. 如果只有一个矩形 矩形外的方案数容易计算考虑 矩形内的 必须要存在x这个最大值 且所有值<=x. ...
- CF1487G String Counting (容斥计数)
传送门 考虑$c[i]>n/3$这个关键条件!最多有2个字母数量超过$n/3$! 没有奇数回文?长度大于3的回文串中间一定是长度为3的回文串,所以合法串一定没有长度=3的回文,也就是$a[i]\ ...
- ABC209F. Deforestation——DP(、数学容斥)
题面 有 n n n 棵树排成一排,每棵树高度为 h i ( i ∈ [ 1 , n ] ) h_i~(i\in[1,n]) hi (i∈[1,n]) ,你现在要按照一个排列 P P P 的顺序去砍 ...
- 【BZOJ 4818】 4818: [Sdoi2017]序列计数 (矩阵乘法、容斥计数)
4818: [Sdoi2017]序列计数 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 560 Solved: 359 Description Al ...
- Co-prime HDU - 4135_容斥计数
Code: #include<cstdio> #include<cstring> #include<cmath> #include<iostream> ...
- How many integers can you find HDU - 1796_容斥计数
Code: #include<cstdio> using namespace std; typedef long long ll; const int R=13; ll a[R]; ll ...
随机推荐
- ORACLE 数据库用户备份及表备份
表备份模式备份:exp system/pwd@127.0.0.1:1521/db owner=(user) file=E:\DB\db20150326.dmp tables=(table);还原 ...
- swing容器继承重绘问题解决
swing容器继承重绘问题解决 以JPanel为例,继承JPanel,想动态为器更换背景,这就涉及到重绘问题.一下是本人重写代码: package ui; import java.awt.Grap ...
- ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器
遍历文件夹的子文件夹下的所有文件,将上个月的文件集中到一起,然互压缩,并copy到服务器的映射磁盘. static void Main(string[] args) { //原始文件存放的位置 Dir ...
- Codevs 1217 借教室 2012年NOIP全国联赛提高组
1217 借教室 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在大学期间,经常需要租借教 ...
- javascript 节点属性详解
javascript 节点属性详解 根据 DOM,html 文档中的每个成分都是一个节点 DOM 是这样规定的:整个文档是一个文档节点每个 html 标签是一个元素节点包含在于 html 元素中的文本 ...
- Spring Mvc Controller间跳转 重定向 传参 (转)
原文链接:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景 需求:spring MVC框架con ...
- VS2015 + QT5.7 中文的坑
试验1: #if _MSC_VER >= 1600 #pragma execution_character_set("utf-8") #endif #include < ...
- WPF中如何使用图像API进行绘制
首先,由于WPF中不象GDI+中有Graphics对象,因此你无法使用Graphics进行绘图了,取而代之的是:DrawingContext:类似地,GDI+中的OnPaint已被OnRender取代 ...
- [swift]可选类型
可选类型 <Swift权威指南>第2章千里之行始于足下——Swift语言基础,本章挑选了Swift语言的最基本特性加以介绍.尽管这些特性只占Swift全部特性的很少一部分,但却是所有的Sw ...
- 1500: [NOI2005]维修数列
Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...