题目链接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 方法总结的更多相关文章

  1. Coins HDU - 2844 POJ - 1742

    Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...

  2. 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 ...

  3. hdu 2844 poj 1742 Coins

    hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...

  4. 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 ...

  5. 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 ...

  6. 扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]

    学习链接:http://blog.csdn.net/lwt36/article/details/48908031 学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题. 扫描线求矩形周长并 hd ...

  7. hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】

    Tunnel Warfare                                                             Time Limit: 4000/2000 MS ...

  8. 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 ...

  9. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

随机推荐

  1. Django从无到有的艰苦历程

    1, django项目下的各个文件的介绍 1.1, 项目的根目录: 实Django项目的总目录, 所有的子项目, 和需要进行的操作都在其中进行. 1.2

  2. 自己根据java的LinkedList源码编写的一个简单的LinkedList实现

    自己实现了一个简单的LinkedList /** * Create by andy on 2018-07-03 11:44 * 根据 {@link java.util.LinkedList}源码 写了 ...

  3. [Algorithms] Topological Sort

    Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...

  4. Contos更换python版本

    1.查看版本 #python -VPython 2.6.6 2.安装前准备,安装相关库#yum install gcc gcc-c++ autoconf automake#yum install op ...

  5. 修改jquery.automeplete,使其支持value匹配

    原生只会去匹配label,可在实际使用中,可能需要匹配的值并不需要显示在label中,经过添加一个matchType属性解决 1.加入matchType选项,并默认为原生匹配 $.widget(&qu ...

  6. 3 CActiveXUI的一个Bug

    如果主窗口直接用变量生成,则关闭窗口时会产生崩溃            如果用new的方式生成,则不会崩溃,所以给出一个临时的快速解决方案,即主窗口都用new生成,_tWinMain改为下面这样: i ...

  7. Linux基础命令(四)

    作业一:1) 开启Linux系统前添加一块大小为15G的SCSI硬盘 [root@bogon ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 byte ...

  8. 2015-03-12——简析DOM2级事件

    DOM2级事件 事件的几种类型:对象事件,鼠标事件,键盘事件,表单事件,W3CDOM事件,以及针对浏览器的事件. 对象事件:window对象,也是javascript对象.load  适用于windo ...

  9. Milking Time---poj3616(简单dp)

    题目链接:http://poj.org/problem?id=3616 题意:人从奶牛身上挤奶有m个时间段(1----n),每个时间段包含 s e f 表示从 s 到 e 的这段时间可以获得 f 单位 ...

  10. 《深入理解Linux内核》阅读笔记 --- Chapter 2 Memory Addressing

    1.logical address = segment identifier (16bits) + offset (32bits) segment selector其实就是GDT或者LDT的索引,其中 ...