UVALive 3295
题目大意:见刘汝佳《算法竞赛入门经典——训练指南》P173
解题思路:
每一个合法的三角形的三个顶点都不在同一直线上,那么问题其实就是在求所有不全在同一直线上的三点的组合数。
我们可以利用容斥原理,先求出所有的三个顶点的组合数C[(n+1)*(m+1)][3]。全在同一直线上的三个网格顶点有三种:三点在同一水平线上的,三点在同一竖直线上的,三点在同一斜线上的。前两种不难求,因此不再赘述,这里重点讲解第三种。
设三点坐标为(x1,y1),(x2,y2),(x3,y3),且x1 < x2 < x3,y1 < y2 < y3,设 x2-x1 = i,x3-x2 = j,当且仅当 i*(y3-y2) = j*(y2-y1)时,三点在同一斜线上。那么我们可以枚举 i 和 j ,求出 g = gcd(i,j),对于每一个(i,j),再从 1 开始枚举 k ,则此时 y3-y2 = k*j/g,y2-y1 = k*i/g(当 y3 - y1 >= m+1时退出循环),这样就可以知道 x3-x1 和 y3-y1 的值了,接下来就只需要求出放得下多少条这样的斜线就可以。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn=+;
ll C[maxn][];
void init(){
C[][]=;
C[][]=C[][]=;
for(int n=;n<maxn;n++){
C[n][]=;
if(n<=) C[n][n]=;
for(int m=;m<min(,n);m++){
C[n][m]=C[n-][m-]+C[n-][m];
}
}
}
int gcd(int a,int b){
if(b==) return a;
return gcd(b,a%b);
}
int main(){
init();
int n,m;
int kase=;
while(scanf("%d%d",&n,&m)==&&n&&m){
n++,m++;
if(n>m) swap(n,m);
ll ans=C[n*m][];
ans-=C[n][]*m;
ans-=C[m][]*n;
ll t=;
for(int i=;i<n;i++){
for(int j=;i+j<n;j++){
int gd=gcd(i,j);
for(int k=;;k++){
int id=k*j/gd,ij=k*i/gd;
if(id+ij>=m) break;
t+=(n-(i+j))*(m-(k*(i+j)/gd));
}
}
}
ans-=t*;
printf("Case %d: %lld\n",kase++,ans);
}
return ;
}
UVALive 3295的更多相关文章
- UVALive 3295 Counting Triangles
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- BZOJ 3295: [Cqoi2011]动态逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3865 Solved: 1298[Submit][Sta ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
随机推荐
- 天大福利!世界第一科技出版公司 Springer 免费开放 400 多本电子书!
前几天,世界著名的科技期刊/图书出版公司施普林格(Springer)宣布:免费向公众开放 400 多本正版的电子书!! Springer 即施普林格出版社,于1842 年在德国柏林创立,20 世纪60 ...
- solr7.4创建core,导入MySQL数据,中文分词
#solr版本:7.4.0 一.新建Core 进入安装目录下得server/solr/,创建一个文件夹,如:new_core 拷贝server/solr/configsets/_default/con ...
- python笔记 函数初识
1. 函数: 封装一个功能 def my_len(形参): -> def 声明定义一个函数 my_len 函数名命名规则同变量 ······ - ...
- CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)
Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...
- CF思维联系–CodeForces -224C - Bracket Sequence
ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...
- 补题Codeforces 1102E. Monotonic Renumeration
这个题还是不太懂,下面附上的是大佬的题解(https://zhanghuimeng.github.io/post/codeforces-1102e-monotonic-renumeration/) E ...
- FileStream提示文件正在由另一进程使用的解决方法
文件正在由另一进程使用…… FileStream fs = new FileStream(strFilePath, FileMode.Open,FileAccess.Read,FileShare.Re ...
- Collection接口【集合】和Iterator迭代器类
1.1集合的概述 前面基础学习并使用过集合ArrayList<E>,那么集合究竟是什么呢? 集合:集合是Java中提供的一种容器,可以用来存储多个数据. 那么意思就是说集合是容器,但是容器 ...
- 【FPGA篇章六】FPGA编译向导:详解编译预处理功能
欢迎大家关注我的微信公众账号,支持程序媛写出更多优秀的文章 Verilog HDL语言和C语言一样也提供了编译预处理功能. Verilog HDL允许在程序中使用特殊的编译预处理语句. 在编译时,通常 ...
- 加速你的网络!软路由构建 去AD+国内域名加速解析+抗污染+速度优选 与PSW无缝集成 综合方案
本方案利用OpenWrt搭建4级DNS,实现 去AD + 国内域名加速解析 + 抗污染(域名解析按地区分流)+ 访问速度优选. 方案涉及部分软件配置细节可以参照之前博文:https://www.cnb ...