标题效果:给你个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. 解决adb server is out of date. killing...问题

    在运行 adb 命令时出现了例如以下提示: adb server is out of date.  killing... 导致 adb 无法正常启动,更无法运行其它命令. 有问题怎么办?百度呗.查了查 ...

  2. ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题

    Information Entropy Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge Informatio ...

  3. CMSIS标准

    CMSIS 标准(Cortex Microcontroller Software Interface Standard) ,翻译过来是"ARM Cortex™ 微控制器软件接口标准" ...

  4. InputMonitor注意事项

    文章只记录自己的点点理解.为了你自己的参考. 1.mInputFocus WMS.addWindow()-->WMS.finishUpdateFocusedWindowAfterAssignLa ...

  5. LINQ之路(3):LINQ扩展

    本篇文章将从三个方面来进行LINQ扩展的阐述:扩展查询操作符.自定义查询操作符和简单模拟LINQ to SQL. 1.扩展查询操作符 在实际的使用过程中,Enumerable或Queryable中的扩 ...

  6. 数据库管理——Powershell——使用Powershell脚本找出消耗最多磁盘空间的文件

    原文:数据库管理--Powershell--使用Powershell脚本找出消耗最多磁盘空间的文件 原文译自: http://www.mssqltips.com/sqlservertip/2774/p ...

  7. Unity2D实现贴图凹凸感并接受实时光照效果

    先看终于效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  8. JAVA学习(二):JDK介绍及其结构、用途

    JDK介绍及其结构.用途 1.JDK的介绍 JDK是Java Development Kit 的缩写.是Sun Microsystems针对Java开发员的产品.它由一个处于操作系统层之上的执行环境还 ...

  9. Extjs GridPanel 几点说明

    1. 在Ext中,表格控件必须包括列定义信息,并指定表格的数据存储器.列信息由columns定义,而数据存储器有store定义. 2. store负责把各种各样的原始数据(JSON对象数组等等)转换成 ...

  10. HDU 3874 离线段树

    在所有数字的统计范围,,对于重复统计只有一次 离线段树算法 排序终点坐标.然后再扫,反复交锋.把之前插入树行被删除 #include "stdio.h" #include &quo ...