HDU2841 (队列容斥)
2024-08-27 23:59:42
Visible TreesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2213 Accepted Submission(s): 908
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.
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
1 1
2 3
Sample Output
1
5
5
Source
其实感觉这个代码写搓了 但是确实是利用队列的一种方式 重点理解dfs容斥方式
传送门 http://www.cnblogs.com/hsd-/p/5008912.html
#include<bits/stdc++.h>
using namespace std;
__int64 n;
__int64 a,b;
__int64 slove( __int64 m,__int64 gg)
{
__int64 que[1000];
__int64 a[1000];
memset(que,0,sizeof(que));
memset(a,0,sizeof(a));
__int64 sum=0;
__int64 t=0;
__int64 ss=0;
for(__int64 i=2;i*i<=m;i++)
{
if(m%i==0)
{
que[ss++]=i;
while(m%i==0)
m=m/i;
}
}
if(m>1)
que[ss++]=m;
a[t++]=-1;
for(__int64 i=0;i<ss;i++)
{
int k=t;
for(__int64 j=0;j<k;j++)
{
a[t++]=que[i]*a[j]*(-1);
}
}
for(__int64 i=1;i<t;i++)
sum=sum+gg/a[i];
return sum;
}
int main()
{
while(scanf("%I64d",&n)!=EOF)
{
for(__int64 i=1;i<=n;i++)
{
__int64 re=0;
scanf("%I64d %I64d",&a,&b);
for(__int64 j=1;j<=a;j++)
re+=slove(j,b);
printf("%I64d\n",a*b-re);
}
}
return 0;
}
最新文章
- hibernate学习(7)——HQL查询
- Linux命令小结:crontab/netstat/iostat/sar
- POJ - 1107 W&#39;s Cipher
- Fragment 切换问题
- Rolling Cursor Invalidations with DBMS_STATS.AUTO_INVALIDATE (文档 ID 557661.1)
- Linux中的svn客户端RabbitVCS-2
- MySQL架构优化实战系列2:主从复制同步与查询性能调优
- 性能测试分享:jmeter性能监控(一)
- 606. Construct String from Binary Tree
- 数据结构与算法 —— 链表linked list(02)
- 一句python代码搭建FTP服务
- java持有对象-集合类
- java框架之SpringBoot(14)-任务
- 微信出现BUG,发送“ 两位数字+15个句号 ”,双方系统会卡崩……
- Spark 分布式调试工具
- 在hadoop运行tensor flow
- [漏洞复现]CVE-2018-4887 Flash 0day
- UVALive-3972 March of the Penguins (最大流:节点容量)
- CUBE,ROLLUP 和 GROUPING
- oralce 查看执行计划