首页 技术 正文
技术 2022年11月14日
0 收藏 433 点赞 4,642 浏览 1967 个字

题意:

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

知识点:

容斥原理:(容许) 先不考虑重叠的情况,把包含于某条件中的所有对象的数目先计算出来,(排斥)然后再把计数时重复计算的数目排斥出去,使得计算的结果既无遗漏又无重复。

公式:          奇加偶减

一般求互质个数若用欧拉函数不好解决,则从反面考虑,用容斥。

模板:

void dfs(int c,int cur,int i,int j,LL ans1)      //dfs(c,1,i,0,1);
{
if(cur==c+1)
{
if(c&1)
ans-=ma/ans1;
else
ans+=ma/ans1;
return;
}
for(;j<cnt[i];j++)
{
dfs(c,cur+1,i,j+1,ans1*syz[i][j]);
}
}

题解:在同一条直线上的点的形式都是(ka,kb),这些点只能看到第一个。若gcd(a,b)==k’,(k’!=1),则其还能写成    (k’a’,k’b’)的形式。直到gcd(a,b)==1,(a,b)为第一个点。That is say,只要求出1~n与1~m中互质的数的个数,这题就OK了。一般求互质个数若用欧拉函数不好解决,则从反面考虑,用容斥。x为1~n中的一个数,x与1~m中互质的数的个数怎么求呢?用容斥,先找到有多少个数和x有1个公共的质因子,然后加上;再找到有多少个数与x有2个公共的质因子,然后减去;再找到有多少个数有多少个数与x有3个公共的质因子,然后加上……最后得到的个数,就是有多少个数与x不互质。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=1e5+10;
int ma,mi;
LL ans;int syz[N][7]; //素因子分解结果
int cnt[N];
void init() //筛选法求syz
{
memset(cnt,0,sizeof(cnt));
for(int i=2;i<N;i++)
{
if(cnt[i]==0)
{
for(int j=i;j<N;j+=i)
{
syz[j][cnt[j]++]=i;
}
}
}
}void dfs(int c,int cur,int i,int j,LL ans1) //dfs(c,1,i,0,1);
{
if(cur==c+1)
{
if(c&1)
ans-=ma/ans1;
else
ans+=ma/ans1;
return;
}
for(;j<cnt[i];j++)
{
dfs(c,cur+1,i,j+1,ans1*syz[i][j]);
}
}int main()
{
int t;
cin>>t;
init();
LL m,n;
while(t--)
{
scanf("%lld%lld",&m,&n);
ans=m*n;
ma=max(m,n),mi=min(m,n);
for(int i=2;i<=mi;i++)
{
for(int c=1;c<=cnt[i];c++)
{
dfs(c,1,i,0,1);
}
}
printf("%I64d\n",ans);
}
return 0;
}/*
100
23 54
56 54
44 54
10 10
100000 100000
*/

Visible Trees

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,905
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727