标题效果:给你个m*n方格,广场格从(1,1)开始。

在树中的每个点,然后让你(0,0)点往下看,问:你能看到几棵树。

解题思路:假设你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊。挡住的话就是这个小的方格内对角线的连线过顶点,如图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHUxMjExMDUwMTEyNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

建设A为(0,0)假设B能遮挡住C,那么B与C组成的矩形中nx, mx是不互质的。

所以x从1開始枚举n假设m中某一项与x互质那么就会有一个格子显示出来,所以这里须要找的是1-n中与枚举的x互质的个数。这里的话我们能够用到分解质因数,然后用状态压缩进行容斥,求出n中与x互质的个数。

PS:我代码里面写的n,m与这里解释的n,m是反过来的。

Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1556    Accepted Submission(s): 645

Problem Description
There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.



If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
 
Input
The first line contains one integer t, represents the number of test cases. Then there are multiple test cases. For each test case there is one line containing two integers m and n(1 ≤ m, n ≤ 100000)
 
Output
For each test case output one line represents the number of trees Farmer Sherlock can see.
 
Sample Input
2
1 1
2 3
 
Sample Output
1
5
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
#define LL long long
//#define LL long long
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007 const int maxn = 100010; using namespace std; struct node
{
int num[8];
int ans;
} p[maxn]; int f[maxn];
int k[maxn];
int t;
void prime()
{
t = 0;
memset(f, 0, sizeof(f));
for(int i = 2; i <= maxn-5; i++)
{
if(!f[i]) k[t++] = i;
for(int j = 0; j < t; j++)
{
if(i*k[j] > maxn-5) break;
f[i*k[j]] = true;
if(i%k[j] == 0) break;
}
}
} LL judge(int n, int x)
{
LL cnt = 0;
for(int i = 0; i < (1<<(p[x].ans)); i++)
{
int ss = 0;
int sx = 1;
for(int j = 0; j < p[x].ans; j++)
{
if((1<<j)&i)
{
ss++;
sx *= p[x].num[j];
}
}
if(ss%2) cnt += (n-n/sx);
else cnt -= (n-n/sx);
}
return cnt;
} int main()
{
prime();
for(int i = 2; i <= maxn-10; i++)
{
int x = i;
p[i].ans = 0;
for(int j = 0; j < t; j++)
{
if(x == 1) break;
if(!f[x])
{
p[i].num[p[i].ans++] = x;
break;
}
if(x%k[j]) continue;
p[i].num[p[i].ans++] = k[j];
while(x%k[j] == 0) x /= k[j];
}
}
int n, m;
int T;
cin >>T;
while(T--)
{
scanf("%d %d",&n, &m);
LL sum = n;
for(int i = 2; i <= m; i++) sum += judge(n, i);
cout<<sum<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 2841 Visible Trees(数论)的更多相关文章

  1. HDU 2841 Visible Trees 数论+容斥原理

    H - Visible Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  2. HDU 2841 Visible Trees(容斥定理)

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  3. hdu 2841 Visible Trees 容斥原理

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pr ...

  4. HDU 2841 Visible Trees(莫比乌斯反演)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可 ...

  5. hdu 2841 Visible Trees(容斥)

    原文链接 There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is stand ...

  6. hdu 2841 Visible Trees

    /** 大意: 求[1,m], [1,n] 之间有多少个数互素...做了 1695 ,,这题就so easy 了 **/ #include <iostream> #include < ...

  7. HDU 2841 Visible Trees(容斥)题解

    题意:有一块(1,1)到(m,n)的地,从(0,0)看能看到几块(如果两块地到看的地方三点一线,后面的地都看不到). 思路:一开始是想不到容斥...后来发现被遮住的地都有一个特点,若(a,b)有gcd ...

  8. HDU - 2814 Visible Trees

    题意: m*n(1<=m,n<=100000)的森林里,起始点在(1,1),某人从(0,0)点开始看,问能看到多少棵树. 题解: 求出1~x中的每个数与1~y的数中互质的数的总和.用素数筛 ...

  9. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

随机推荐

  1. Java 的swing.GroupLayout布局管理器的使用方法和实例(转)

    The following builds a panel consisting of two labels in one column, followed by two textfields in t ...

  2. Object-C 新手教程

    大纲 開始吧 下载这篇教学 设定环境 前言 编译 hello world 创建 Classes @interface @implementation 把它们凑在一起 具体说明... 多重參数 建构子( ...

  3. hdu 4912 Paths on the tree(lca+馋)

    意甲冠军:它使树m路径,当被问及选择尽可能多的路径,而这些路径不相交. 思考:贪心,比較忧伤.首先求一下每对路径的lca.依照lca的层数排序.在深一层的优先级高.那么就能够贪心了,每次选择层数最深的 ...

  4. 通过配置Windows 防火墙允许使用TCP/IP协议远程访问数据库

    原文:通过配置Windows 防火墙允许使用TCP/IP协议远程访问数据库 本文适用于:2005.2008.2008R2所有版本 为了可以通过TCP/IP协议远程访问SQLServer数据库,需要做以 ...

  5. Calendar计算日期

    一.周六三月进去.星期天 Calendar calendar=Calendar.getInstance();//当前日期 Calendar calendar2=Calendar.getInstance ...

  6. java major version(转)

    在jar包中,用winrar解压一个类文件,然后在命令行下面输入 javap -verbose classname 会输出一些信息,大致如下: Compiled from "HtmlCraw ...

  7. “GDI+ 发生一般错误” 解决方法

    System.Runtime.InteropServices.ExternalException: GDI+ 发生一般错误 对应的帐户没有写权限,给予帐户写权限 版权声明:本文博主原创文章,博客,未经 ...

  8. spring mvc中实现csrf安全防御简记

    1.csrf是什么 csrf全称是Cross-site request forgery,http://en.wikipedia.org/wiki/Csrf 危害:使受害用户在不经意间执行了不是用户意愿 ...

  9. [WebGL入门]四,渲染准备

    注意:文章翻译http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:].另外.鄙人webgl研究还不够深入,一些专业词语,假设翻译有误,欢迎大家 ...

  10. IOS 数据库管理系统(SQLite)

    嵌入式数据库 SQLite嵌入式数据库的优势 1.支持事件,你并不需要配置,无需安装,不需要管理员 2.支持部分脂肪SQL92 3.完整的数据库被存储在磁盘上的文件的顶部,相同的数据库文件可以在不同机 ...