首页 技术 正文
技术 2022年11月15日
0 收藏 477 点赞 4,407 浏览 2785 个字

Visible Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2462    Accepted Submission(s): 1032

Problem DescriptionThere
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. InputThe
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)  OutputFor each test case output one line represents the number of trees Farmer Sherlock can see.  Sample Input2
1 1
2 3 Sample Output1
5 Source 2009 Multi-University Training Contest 3 – Host by WHU 题意:从(0,0)往一个矩阵看,看矩阵中的那些点,问能看到多少个,如果有多个点与(0,0)在一条直线上那么只能看到最前端的点,在这个点后面是看不到的。思路:欧拉函数+容斥原理;可以把矩阵分成三块,一个上三角一个下三角,还有一个对角线,先单独讨论对角线,就只有一种(对角线指的是斜率为负一的那一条)上三角(x,y)—(x<y);关于每一个横坐标,我们的目的是找所有的不同种的斜率,那么假如取(x,y)(x与y不互质,那么(x/(gcd(x,y),y/gcd(x,y)))));得到新坐标(a,b)(a,b互质),所以我们要求的斜率的不同种类就是求oula[x];下三角=上三角(矩阵为正方形)直接欧拉函数;由于给的不一定是正方形;n,m;所以在min(n,m)中的所有点我们可以直接oula函数,ans=sum(oula[i])*2( 1<=i<=min(n,m) );所以在另一个比较长的边,还剩下(min(n,m)+1,max(n,m))没有处理这里就转换成在(1,min(n,m))中求(min(n,m)+1,max(n,m))中每个数在(1,min(n,m))区间有多少个数与之互素的个数的和;这里用容斥原理;求(min(n,m)+1,max(n,m))每个数的素因子,然后容斥求在(1,min(n,m))与之不互质的数的个数,再区间长度减去这个就是与之互素的个数;因为每个数的不同素因子不超过9个,所以复杂度为n*512

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<queue>
7 int oula[100005];
8 bool prime[100005];
9 int ans[100005];
10 long long bns[100005];
11 int id[100005];
12 using namespace std;
13 typedef long long LL;
14 queue<int>que;
15 int main(void)
16 {
17 int i,j,k;
18 for(i=0; i<100005; i++)
19 oula[i]=i;
20 oula[1]=0;
21 memset(prime,0,sizeof(prime));
22 for(i=2; i<1000; i++)
23 {
24 if(!prime[i])
25 {
26 for(j=i; i*j<=100000; j++)
27 {
28 prime[i*j]=true;
29 }
30 }
31 }
32 int cnt=0;
33 for(i=2; i<=100000; i++)
34 if(!prime[i])
35 ans[cnt++]=i;
36 for(i=0; i<cnt; i++)
37 {
38 for(j=1; j*ans[i]<=100000; j++)
39 {
40 oula[j*ans[i]]/=ans[i];
41 oula[j*ans[i]]*=(ans[i]-1);
42 }
43 }
44 scanf("%d",&k);
45 int n,m;
46 while(k--)
47 {
48 scanf("%d %d",&n,&m);
49 memset(id,0,sizeof(id));
50 int xx=max(n,m);
51 int cc=min(n,m);
52 if(cc==1)
53 printf("%d\n",xx);
54 else
55 {
56 long long sum=0;
57 for(i=2; i<=cc; i++)
58 {
59 sum+=oula[i];
60 }
61 sum*=2;
62 for(i=cc+1; i<=xx; i++)
63 {
64 int vv=i;
65 int t=0;
66 int flag=0;
67 while(vv>1)
68 {
69 if(vv%ans[t])
70 {
71 t++;
72 flag=0;
73 }
74 else if(vv%ans[t]==0&&flag==0)
75 {
76 que.push(ans[t]);
77 flag=1;
78 vv/=ans[t];
79 }
80 else if(vv%ans[t]==0&&flag)
81 {
82 vv/=ans[t];
83 }
84 if(vv==1)
85 break;
86 }
87 int ak=0;
88 while(!que.empty())
89 {
90 id[ak++]=que.front();
91 que.pop();
92 }
93 LL ff=0;
94 int ii;
95 for(ii=1; ii<=(1<<ak)-1; ii++)
96 {
97 int tt=0;
98 LL pp=1;
99 for(j=0; j<ak; j++)
100 {
101 if(ii&(1<<j))
102 {
103 tt++;
104 pp*=id[j];
105 }
106 }
107 if(tt%2==0)
108 {
109 ff-=cc/pp;
110 }
111 else ff+=cc/pp;
112 }
113 sum+=cc-ff;
114
115 }
116 sum+=1;
117 printf("%lld\n",sum);
118 }
119 }
120 return 0;
121 }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,990
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,504
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,133
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,843