Visible Trees

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

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
分析:由题可知只有gcd(x,y)=1才能被看见;
   所以枚举其中一维坐标,容斥求出与之互质的另一维坐标即可;
   预处理每个数的素因子;
   或使用莫比乌斯函数,也是容斥求解;
代码1:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t;
bool vis[maxn];
vi fac[maxn];
void init()
{
for(int i=;i<=maxn-;i++)
{
if(vis[i])continue;
for(int j=i;j<=maxn-;j+=i)
{
vis[j]=true;
fac[j].pb(i);
}
}
}
int gao(int x,int y)
{
int ret=,num=fac[x].size();
for(int i=;i<(<<num);i++)
{
int now=,cnt=;
for(int j=;j<num;j++)
{
if(i&(<<j))
{
cnt++;
now*=fac[x][j];
}
}
if(cnt&)ret+=y/now;
else ret-=y/now;
}
return y-ret;
}
int main()
{
int i,j;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
ll ret=;
rep(i,,m)
{
ret+=gao(i,n);
}
printf("%lld\n",ret);
}
return ;
}
代码2:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,cnt,fac[maxn],ok[maxn];
int x,y;
bool vis[maxn];
void init()
{
for(int i=;i<=maxn-;i++)
{
if(vis[i])continue;
for(int j=i;j<=maxn-;j+=i)
{
vis[j]=true;
++fac[j];
if(j%((ll)i*i)==)ok[j]=;
}
}
}
int main()
{
int i,j;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&x,&y);
ll ret=(ll)x*y;
rep(i,,maxn-)
{
if(i>x||i>y)break;
if(ok[i])continue;
ret+=(ll)(fac[i]&?-:)*(x/i)*(y/i);
}
printf("%lld\n",ret);
}
return ;
}

Visible Trees的更多相关文章

  1. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

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

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

  3. HDU 2841 Visible Trees(数论)

    标题效果:给你个m*n方格,广场格从(1,1)开始. 在树中的每个点,然后让你(0,0)点往下看,问:你能看到几棵树. 解题思路:假设你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊.挡 ...

  4. Visible Trees HDU - 2841

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

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

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

  6. Hdu2841 Visible Trees 2017-06-27 22:13 24人阅读 评论(0) 收藏

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

  7. hdu 2841 Visible Trees 容斥原理

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

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

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

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

    Visible Trees 传送门 解题思路: 实际上的答案就是1~n与1~m之间互质的数的对数,写出式子就是 \(ans=\sum^{n}_{i=1}\sum^{m}_{j=1}[gcd(i,j)= ...

随机推荐

  1. 加密学教程(Cryptography Tuturials)文件夹

    加密学教程(Cryptography Tuturials) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&quo ...

  2. ASP.NET MVC脚本及样式压缩

    现在我用ASP.NET MVC4.0,发现它自带有脚本和样式压缩功能.不知道以前的版本有木有,没有深究.太棒了!以前我们还辛辛苦苦自己搞了一个压缩的东西.这再次说明,平庸程序员如我辈,应该把时间和精力 ...

  3. Single document interface和Multiple document interface

    https://en.wikipedia.org/wiki/Single_document_interface https://msdn.microsoft.com/en-us/library/b2k ...

  4. awk查找特定字段

    在一行中,查找字段包含exe的: ###########awk.awk######## { for(i=1;i<NF;i++) { if($i ~ /exe/) { print $i } } } ...

  5. jquery的ajax同步异步执行

    大家先看一段简单的jquery ajax 返回值的js 代码 function getReturnAjax{  $.ajax({    type:"POST",    http:/ ...

  6. 基于Angular4+ server render(服务端渲染)开发教程

    目标: 1.更好的 SEO,方便搜索爬虫抓取页面内容 2.更快的内容到达时间(time-to-content) 影响: 1.用户:比原来更快的看到渲染的页面,提升用户体验 2.开发人员:某些代码可能需 ...

  7. Elasticserach 同步索引报错:ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  8. Oracle_备份整库

    @echo off color 0b & cls echo echo 设置备份文件存放文件夹... echo set "tbuf=C:\OracleBackup" if n ...

  9. BZOJ 2084 二分+hash OR Manacher

    思路: 二分+哈希 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> ...

  10. Spring Boot (19) servlet、filter、listener

    servlet.filter.listener,在spring boot中配置方式有两种:一种是以servlet3开始提供的注解方式,另一种是spring的注入方式. servlet注解方式 serv ...