给出一个整数集,其中包含1-n的所有整数,要求挑选出一个元素最多的子集,使得子集中任意两数的乘积不是完全平方数 (n<=10^6)
求这样一个最大子集的元素个数

#include <cstdio>
#include <cstring>
#include <cmath> const int N = 1000006; int b[N];
int cnt[N];
int s[N]; void init()
{
int i, j;
memset(b, 1, sizeof(b));
for (i = 2; i < N; i++) {
if (b[i]) {
cnt[i] = i;
for (j = i + i; j < N; j += i) {
if(b[j]) {
cnt[j] = i;
b[j] = 0;
} else
cnt[j] *= i;
}
}
}
} //法1.将范围内所有因数分解质因数,将所有含有因子不是一次的数删去 int main(int argc, char* argv[])
{
int n, i, t;
init();
s[1] = 1;
for (i = 2; i < N; i++)
{
if (cnt[i] == i)
s[i] = s[i - 1] + 1;
else
s[i] = s[i - 1];
}
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
printf("%d\n", s[n]);
}
return 0;
} //法2.将范围内完全平方数和完全平方数的倍数删掉
int main()
{
int i, j; memset(b, 0, sizeof(b)); for (i = 4; i < N; i++)
{
int sq = sqrt((double)i);
//printf("sq = %d i = %d\n", sq, i);
if (sq * sq == i)
for (j = i; j < N; j += i)
b[j] = 1;
} int t, n;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
int ans = n;
for (int i = 0; i <= n; i++)
ans -= b[i];
printf("%d\n", ans);
} return 0;
}

[思维题]Bored Qishen的更多相关文章

  1. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  2. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  3. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  4. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  5. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  7. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  8. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  9. cf796c 树形,思维题

    一开始以为是个树形dp,特地去学了..结果是个思维题 /* 树结构,设最大点权值为Max,则答案必在在区间[Max,Max+2] 证明ans <= Max+2 任取一个点作为根节点,那么去掉这个 ...

随机推荐

  1. Python天天美味(15) - Python正则表达式操作指南(re使用)(转)

    http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...

  2. SSH架构简单总结

    Struts.spring.Hibernate在各层的作用 1)struts 负责 web层.    ActionFormBean 接收网页中表单提交的数据,然后通过Action 进行处理,再Forw ...

  3. Spring mvc 模式小结

    http://www.taobaotesting.com/blogs/2375 1.spring mvc简介 Spring MVC框架是一个MVC框架,通过实现Model-View-Controlle ...

  4. Tiny4412汇编流水灯代码,Tiny4412裸机LED操作[1]

    从今天开始就正式进入到tiny4412的开发学习中了,今天主要看了一下Tiny4412的启动流程及存储器映射及Exynos4412数据手册,用汇编写了一个跑马灯程序(后续会有C语言版本的出来),先说一 ...

  5. [原]Unity3D深入浅出 - 角色控制器(Character Controller)

    角色控制器主要用于第一人称和第三人称主角的控制,并不使用刚体物理效果. 添加角色控制器的方法:依次打开菜单栏中的Component - Physiscs - Character Controller ...

  6. BZOJ2055: 80人环游世界

    题解: 总算A掉了,各种蛋疼... int main() { freopen("input.txt","r",stdin); freopen("out ...

  7. Excel 内容粘贴到DataGridView, DataGridView 粘贴到 Excel

    void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Ke ...

  8. [转] LCA与Tarjan

    转载:http://m.blog.csdn.net/blog/u013076044/41875009# 在线算法与离线算法的定义 在计算机科学中,一个在线算法是指它可以以序列化的方式一个个的处理输入, ...

  9. Zen Coding support in WebStorm/PhpStorm

    With the last WebStorm/PhpStorm EAP you can edit HTML and CSS code really fast usingZen Coding featu ...

  10. 【 D3.js 高级系列 — 1.1 】 封装文本自动换行

    在[高级 - 第 1.0 章]中讲解了在 SVG 中如何配合使用 text 和 tspan 来实现换行的功能,本文对此功能进行一下封装,以后就可以直接用了. 1. 引用 js 文件 下载地址:mult ...