标题效果:给你个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. WF系列——工作流基本知识

    工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期的业务目标 ...

  2. Java模式(适配器型号)

    今天阅读Java该适配器模式,这里有一个小的总结和下谈感受.对于将来使用. 首先.让我们有关适配器先说说. 适应是“来源”至“目标”适应.其中连接这两个的关系是适配器.它负责“源”过度到“目标”. 举 ...

  3. CentOS7.0 安装JAVA周围环境

    CentOS7.0 安装JAVA周围环境  安装JDK 1.配置JDK环境变量 把下载好的JDK(jdk-7u75-linux-x64.gz)文件上传到 Reg: /home/p2pweb/java/ ...

  4. 【Android基础】listview控件的使用(3)------Map与SimpleAdapter组成的多显示条目的Listview

    前面介绍的两种listview的使用都是最基础的,所以有很大的局限性,比如只能在一个item(即每一行的条目)中显示一个文本信息,这一篇我将介绍Map与SimpleAdapter组成的多显示条目的Li ...

  5. HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分法+枚举)

    主题链接: HDU:pid=4430" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4430 ...

  6. HDU 4946 Area of Mushroom 凸包

    链接:pid=4946">http://acm.hdu.edu.cn/showproblem.php?pid=4946 题意:有n个人.在位置(xi,yi),速度是vi,假设对于某个点 ...

  7. HDOJ 4745 Two Rabbits DP

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Tot ...

  8. RecyclerView0基于使用

    (转载请注明出处:http://www.kennethyo.me/post/android/recyclerviewchu-ji-shi-yong) RecyclerView是Android在v7包中 ...

  9. SQL Server 索引列的顺序——真的没关系吗

    原文:SQL Server 索引列的顺序--真的没关系吗 翻译自:http://www.mssqltips.com/sqlservertip/2718/sql-server-index-column- ...

  10. 有意练习--Rails RESTful(一)

    书要反复提及<哪里有天才>在说,大多数所谓的天才是通过反复刻意练习获得. 当你的练习时间达到10000几个小时后,.你将成为该领域的专家. 近期在学习rails怎样实现RESTful We ...