HDU 2841 Visible Trees(数论)
标题效果:给你个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
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
2
1 1
2 3
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(数论)的更多相关文章
- HDU 2841 Visible Trees 数论+容斥原理
H - Visible Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 2841 Visible Trees(容斥定理)
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 2841 Visible Trees 容斥原理
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...
- HDU 2841 Visible Trees(莫比乌斯反演)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可 ...
- hdu 2841 Visible Trees(容斥)
原文链接 There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is stand ...
- hdu 2841 Visible Trees
/** 大意: 求[1,m], [1,n] 之间有多少个数互素...做了 1695 ,,这题就so easy 了 **/ #include <iostream> #include < ...
- HDU 2841 Visible Trees(容斥)题解
题意:有一块(1,1)到(m,n)的地,从(0,0)看能看到几块(如果两块地到看的地方三点一线,后面的地都看不到). 思路:一开始是想不到容斥...后来发现被遮住的地都有一个特点,若(a,b)有gcd ...
- HDU - 2814 Visible Trees
题意: m*n(1<=m,n<=100000)的森林里,起始点在(1,1),某人从(0,0)点开始看,问能看到多少棵树. 题解: 求出1~x中的每个数与1~y的数中互质的数的总和.用素数筛 ...
- C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...
随机推荐
- 【DataStructure】The description of Java Collections Framework
The Java Connections FrameWork is a group of class or method and interfacs in the java.util package. ...
- Linux 没有 my.cnf 解决方案文件完全我自己的整个教程很多口才
我看过好多关于Linux下没有my.cnf的博客,都是什么rmp安装没有my.cnf文件啊,然后什么两个方法啊,我就无语了,大家要是知道就不会查资料了,你们敢不敢负责点?说具体点?有的说从 /usr/ ...
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...
- DirectShow基础编程 最简单transform filter 编写步骤
目标编写一个transform filter,功能是对图像进行翻转. 一.选择基类 从CBaseFilter派生出三个用于编写transform filter的类,各自是:CTransformFilt ...
- poj 2408 Anagram Groups(hash)
id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups 题目大意:给定若干 ...
- Chapter 1 Securing Your Server and Network(2):管理服务的SIDs
原文出处:http://blog.csdn.net/dba_huangzj/article/details/37927319 ,专题文件夹:http://blog.csdn.net/dba_huang ...
- [渣译文] SignalR 2.0 系列: SignalR 自托管主机
原文:[渣译文] SignalR 2.0 系列: SignalR 自托管主机 英文渣水平,大伙凑合着看吧…… 这是微软官方SignalR 2.0教程Getting Started with ASP.N ...
- 【C语言探索之旅】 第二课:工欲善其事,必先利其器
内容简介 1.课程大纲 2.第一部分第二课:工欲善其事,必先利其器 3.第一部分第三课预告:你的第一个程序 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C ...
- can't connect to mysql server on localhost <10061>
需要启动MySQL服务.它可以通过两种方式来启动使用MySQL: 1.命令行模式. Win+R,进入cmd然后按Enter键.在命令行形式的输入: net start mysql56 mysql56是 ...
- Post数据到 https异常:基础连接已经关闭: 连接被意外关闭 解决办法
POST数据到HTTPS站点的时候需要设置ServicePointManager类的ServerCertificateValidationCallback属性,并且在POST到https://pass ...