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 - ...
随机推荐
- 3g自己主动更新网卡驱动web完架构文档
几年前写. 看它是否是用得上 1 简单介绍 本文档具体描写叙述了基于ASP.NET平台和IIS服务的T-Mobile自己主动更新系统的实现框架. 本文档主要从技术架构和业务架构两个方面来着手来描写叙 ...
- 玩转Web之easyui(三)-----easy ui dataGird 重新指定url以获取不同数据源信息
如果已经写了一个dataGird并且已经通过url绑定数据源,能不能在其他地方改变url使其从不同数据源获取信息,从而实现查询等操作?答案当然是肯定的,而且仅需要几行代码 $('#btnq').bin ...
- HDU 4915 Parenthese sequence _(:зゝ∠)_ 哈哈
哦,我没做 #include <cstdio> #include <cstring> #include <algorithm> const int N = 1000 ...
- 离PACKET_INp获取信息acket data
于Floodlight模块假设要packet in消息,就对对应的消息类型进行监听就可以.然后在receive方法中就能够操纵这个上传上来的packet_in. 关键代码: E ...
- Javascript作用域问题的构造函数的变量
构造函数new对于使用.代表创建对象.此外,它可以被用作普通的函数调用,因为它也是一个功能. function Person(name) { this.name=name; } Person(12); ...
- ng-repeat出现环路输出Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique
采用ng-repeat循环发生错误时,如下面的输出对象: Duplicates in a repeater are not allowed. Use 'track by' expression to ...
- TextView于getCompoundDrawables()使用演示样本的方法
MainActivity例如下列: package cc.testcompounddrawables; import android.app.Activity; import android.grap ...
- Android 中字体的处理
//得到TextView控件对象 TextView textView = (TextView)findViewById(R.id.custom); //将字体文件保存在assets/fonts/文件夹 ...
- 获取鼠标点击相对于Canva位置的2种方法
如果给Canvas添加 onmousedown事件,获取到的鼠标位置都是相对于当前文档的位置(x,y):
- SQL Server中tempdb的管理
原文:SQL Server中tempdb的管理 资料来自: http://blogs.msdn.com/b/sqlserverstorageengine/archive/tags/tempdb/ ht ...