留坑(p.339) 已填(膜汪)

每条直线至少经过两个点,我们不妨在经过的所有点中的第二个点统计它

设f[i][j]表示i * j的答案,那么显然可以用f[i][j] = f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1] + 以(i, j)这个点为第二个经过的点的直线

这样的直线的数量与(1, 1) ~ (i - 1, j - 1)中与(i, j)横纵坐标差与(i, j)互质的点的数量有关(还要再减掉一写)

而这个数量等于(1, 1) ~ (i - 1, j - 1)中与(0, 0)横纵坐标差与(i, j)互质的点的数量

具体的参照http://blog.csdn.net/accelerator_/article/details/26132853

预处理时间复杂度O(n2)后可以做到O(1)回答询问

rjl的算法:枚举有效包围盒的数量

这样每次都是O(n2)

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream> using namespace std; void setIO(const string& s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
template<typename Q> Q read(Q& x) {
static char c, f;
for(f = ; c = getchar(), !isdigit(c); ) if(c == '-') f = ;
for(x = ; isdigit(c); c = getchar()) x = x * + c - '';
if(f) x = -x;
return x;
}
template<typename Q> Q read() {
static Q x; read(x); return x;
} const int N = + ; int g[N][N]; int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif for(int i = ; i <= ; i++) {
for(int j = i; j <= ; j++) {
g[i][j] = g[j][i] = __gcd(i, j);
}
} int n, m;
while(scanf("%d%d", &n, &m), n) {
int ans = ;
for(int a = ; a <= m; a++) {
for(int b = ; b <= n; b++) {
if(g[a][b] == ) {
int c = max(, m - (a << )) * max(, n - (b << ));
ans += (m - a) * (n - b) - c;
}
}
}
printf("%d\n", ans << );
} return ;
}

uva1393 Highways的更多相关文章

  1. H:Highways

    总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...

  2. Highways(prim & MST)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23421   Accepted: 10826 Descri ...

  3. poj2485 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  4. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

  5. poj 2485 Highways

    题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...

  6. POJ 1751 Highways (最小生成树)

    Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. POJ 1751 Highways (最小生成树)

    Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...

  8. UVa 1393 (容斥原理、GCD) Highways

    题意: 给出一个n行m列的点阵,求共有多少条非水平非竖直线至少经过其中两点. 分析: 首先说紫书上的思路,编程较简单且容易理解.由于对称性,所以只统计“\”这种线型的,最后乘2即是答案. 枚举斜线包围 ...

  9. (poj) 1751 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor ...

随机推荐

  1. c、c++混编实现查询本地IP地址

    一.思路 1.要想得到本地IP地址,可以通过本机名来查询,所以首先得得到本机名. 2.牵涉到IP地址,首先想到牵涉到网络协议,因此得加载套接字协议,所以先使用WSAStartup函数完成对Winsoc ...

  2. 过滤HTML

    过滤HTML public static string NoHTML(string html){ string[] strArray2 = new string[] { "font" ...

  3. Fedora 21 设置开机启动脚本

    sudo touch /etc/rc.d/rc.localsudo vim /etc/rc.d/rc.local 在/etc/rc.d/rc.local文件中写入, 然后使用:wq命令 保存并退出. ...

  4. BOM 之 screen history

    /*    avaiHeight // 屏幕的像素高度减去系统部件高度之后的值    var ah = screen.availHeight;     alert(ah); */    /* avai ...

  5. WPF 进度条

    //Create a Delegate that matches the Signature of the ProgressBar's SetValue method private delegate ...

  6. iOS: 学习笔记实例, 用代码控制视图创建与切换

    1. 创建iOS, Single View Application.2. 修改YYViewController.m // // YYViewController.m // DynamicViewDem ...

  7. c++给数组赋值

    c++的基础不牢啊.甚至是c语言也忘记了..所以以后遇到感觉怪异的语法都保存下来,没事翻翻看看 例一 void getSize(int n[]) //把数组名传给函数的形参时候 一维数组[]不用指定大 ...

  8. hexdump——Linux系统的二进制文件查看工具

    hexdump是Linux下的一个二进制文件查看工具,可以将二进制文件转换为ASCII.10进制.16进制或8进制进行查看. 首先我们准备一个测试用的文件test,十六进制如下: 00 01 02 0 ...

  9. JS模块加载器加载原理是怎么样的?

    路人一: 原理一:id即路径 原则.通常我们的入口是这样的: require( [ 'a', 'b' ], callback ) .这里的 'a'.'b' 都是 ModuleId.通过 id 和路径的 ...

  10. Android开源项目发现--- 工具类快速开发篇(持续更新)

    1. Guava Google的基于java1.6的类库集合的扩展项目 包括collections, caching, primitives support, concurrency librarie ...