HDU 1337 && POJ 1218&& zju 1350 方法总结
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1337 杭电
http://poj.org/problem?id=1218清华
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1350浙江
原本以为小小的一道水题不值得去总结,但是发现却有好多方法,好神奇啊
方法一:数据挺小的模拟就好
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std; int a[110];
int main()
{
int t, n;
scanf("%d", &t);
while(t--)
{
memset(a, 0, sizeof(a));
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
for(int j=i; j<=n; j+=i)
{
a[j]=a[j]^1;
}
}
int ans=0;
for(int i=1; i<=n; i++)
{
if(a[i]==1)
ans++;
} printf("%d\n", ans);
}
return 0;
}
方法二:其实犯人是否能够逃跑是有规律可循的,门有开/关两种形式,只要犯人最终开和关的总次数是奇数就好;例如3 1开3关 ,开关总和为偶数; 例如4 1开2关4开,开关总和为奇数 其实就是看一个数是否有奇数个因子;
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std; int solve(int x)
{
int cnt=;
for(int i=; i<=x; i++)
{
if(x%i==)
cnt++;
}
if(cnt%==)
return ;
else
return ;
}
int main()
{
int t, n;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
int ans=;
for(int i=; i<=n; i++)
{
if(solve(i))
ans++;
}
printf("%d\n", ans);
}
return ;
}
第三种:是看别人才知道的,真的是世界之大无奇不有;
原理是:只有完全平方数才有奇数个因子,例如3, 9,所以只需要求出n以内的完全平方数的个数即可;
n以内的 完全平方数的个数=sqrt(n);
#include<cstdio>
#include<cmath>
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",(int)sqrt(n));
}
return 0;
}
HDU 1337 && POJ 1218&& zju 1350 方法总结的更多相关文章
- Coins HDU - 2844 POJ - 1742
Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...
- hdu 2844 poj 1742 Coins
hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
- HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)
Description Too worrying about the house price bubble, poor Mike sold his house and rent an apartmen ...
- 扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]
学习链接:http://blog.csdn.net/lwt36/article/details/48908031 学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题. 扫描线求矩形周长并 hd ...
- hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】
Tunnel Warfare Time Limit: 4000/2000 MS ...
- Eight hdu 1043 poj 1077
Description The 15-puzzle has been around for over 100 years; even if you don't know it by that name ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
随机推荐
- 网络虚拟化之FlowVisor:网络虚拟层(下)
在前面两篇文章:网络虚拟化之FlowVisor:网络虚拟层(上)和网络虚拟化之FlowVisor:网络虚拟层(中)中分别介绍了FLowVisor的特性和实现,三连载的最后一篇介绍虚拟网络的隔离机制. ...
- String 转 List<Map<String, Object>>
public static List<Map<String, Object>> toListMap(String json){ List<Object> list ...
- PAT 1016 Phone Bills(模拟)
1016. Phone Bills (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A long-di ...
- 简单工厂模式设计(java反射机制改进)
如果做开发的工作,工厂设计模式大概都已经深入人心了,比较常见的例子就是在代码中实现数据库操作类,考虑到后期可能会有数据库类型变换或者迁移,一般都会对一个数据库的操作类抽象出来一个接口,然后用工厂去获取 ...
- Oracle Schema Objects——Tables——TableType
Oracle Schema Objects Object Tables object type An Oracle object type is a user-defined type with a ...
- Powershell数据处理
1.导出csv文档 Export-Csv D:\ps\xxx.csv -Encoding UTF8 -NoTypeInformation 2.发送mail $from="frommailad ...
- DHTML 简介
DHTML, 动态的 html, 不是一门语言, 是多项技术综合体的简称.其中包括了 html, CSS, DOM, javascript. HTML : 负责提供标签, 对数据进行封装,目的是便于对 ...
- scrapy spider
spider 定义:在spiders文件夹中由用户自定义,继承scrapy.Spider类或其子类 Spider并没有提供什么特殊的功能. 其仅仅请求给定的 start_urls/start_requ ...
- LAMP兄弟连 视频教程集
电驴的资源:http://www.verycd.com/topics/2843130/?ref=msg
- Python高级教程-迭代
Python中的迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过for...in ...