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 ;
}

最新文章

  1. 把office文档转换为html过程中的一些坑
  2. SQL 事务回滚
  3. angularJS: shop chart
  4. 九个衡量 Rails 应用性能的小方法
  5. android复制数据库到SD卡(网上搜集,未经验证)
  6. redis缓存的安装和使用
  7. iOS开发——真机调试证书—发布证书
  8. mono环境变量
  9. js 鸭式辨型法
  10. ios 基础学习二
  11. 第一次接触Axure
  12. CentOS 7 安装Subversion, 并用Nginx代理
  13. 从 MVC 到前后端分离
  14. 2T以上的盘怎么分区, 利用parted创建 linuxTB硬盘GPT分区
  15. QMQTT简单介绍(2)
  16. vue keep-alive内置缓存组件
  17. php获取数据库中数据
  18. 使用generator生成dao、mapping和model
  19. SVN标准开发布局目录,trunk,branches,tags用法详解
  20. java-基础-【三】try/catch/finally

热门文章

  1. HDU3183 RMQ/贪心
  2. bzoj 1059 [ ZJOI 2007 ] 矩阵游戏 —— 二分图匹配
  3. logistic regression二分类算法推导
  4. 排序系列 之 归并排序算法 —— Java实现
  5. King(差分约束)
  6. Django day08 多表操作 (三) 基于对象的跨表查询 基于双下划线的多表查询
  7. css3 选择器 权重问题 (第二部分)
  8. android平台 cocos2d-x 读取相册数据
  9. [转]SQLServe 存储表结构的几个系统表
  10. Windows phone开发 网络编程之HttpWebRequest