Visible Trees(hdu2841)
Visible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2462 Accepted Submission(s): 1032
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.
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)
1 1
2 3
5
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 }
Visible Trees(hdu2841)的更多相关文章
- 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 ...
- HDU-2841 Visible Trees(莫比乌斯反演)
Visible Trees 传送门 解题思路: 实际上的答案就是1~n与1~m之间互质的数的对数,写出式子就是 \(ans=\sum^{n}_{i=1}\sum^{m}_{j=1}[gcd(i,j)= ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- HDU 2841 Visible Trees 数论+容斥原理
H - Visible Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 2841 Visible Trees(数论)
标题效果:给你个m*n方格,广场格从(1,1)开始. 在树中的每个点,然后让你(0,0)点往下看,问:你能看到几棵树. 解题思路:假设你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊.挡 ...
- Visible Trees HDU - 2841
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...
- hdu 2841 Visible Trees 容斥原理
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...
- Visible Trees
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...
随机推荐
- 关于SQL中Union和Join的用法
转自帘卷西风的专栏(http://blog.csdn.net/ljxfblog) https://blog.csdn.net/ljxfblog/article/details/52066006 Uni ...
- Python查找最长回文暴力方法
查找最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 例如1: 输入: "babad" 输出: "bab" ...
- day08 Nginx模块
day08 Nginx模块 lnmp架构 l :Linux n :Nginx m :MySQL p :Python/PHP lnmp架构:是最简单的架构 Nginx中的模块(Python模块):前提是 ...
- day32 HTML
day32 HTML 什么是前端 只要是跟用户打交道的界面都可以称之为前端 # eg:电脑界面, 手机界面,平板界面, 什么是后端? eg:python, java,php,go, 不跟用户直接打交道 ...
- JS去除对象或数组中的空值('',null,undefined,[],{})
javascript去掉对象或数组中的'',null,undefined,[],{}.思路就是创建一个新的空对象,然后对传入的对象进行遍历,只把符合条件的属性返回,保留有效值,然后就相当于把空值去掉了 ...
- VIM多标签页
:tabnew 增加一个标签 :tabc 关闭当前的tab :tabo 关闭所有其他的tab :tabp 或gT 前一个 :tabn 或gt 后一个 :tabs 显示 ...
- Linux学习 - 流程控制
一.if语句 1 单分支if条件语句 (1) if [ 条件判断式 ];then 程序 fi (2) if [ 条件判断式 ] then 程序 fi 例:检测根分区的使用量 2 双分支if条件语 ...
- GO 定时器NewTimer、NewTicker使用
package main import ( "fmt" "sync" "time" ) /** *ticker只要定义完成,从此刻开始计时, ...
- C++ default constructor | Built-in types
Predict the output of following program? 1 #include <iostream> 2 using namespace std; 3 4 int ...
- 【编程思想】【设计模式】【结构模式Structural】桥梁模式/桥接模式bridge
Python版 https://github.com/faif/python-patterns/blob/master/structural/bridge.py #!/usr/bin/env pyth ...