Yada Number

Problem Description:

Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,13 in its prime factors is even.

For instance, 18=2 * 3 * 3 is not a yada number since the sum of amount of 2, 3 is 3, an odd number; while 170 = 2 * 5 * 17 is a yada number since the sum of amount of 2, 5 is 2, a even number that satifies the definition of yada number.

Now, Duoxida wonders how many yada number are among all integers in [1,n].

Input

The first line contains a integer T(no more than 50) which indicating the number of test cases. In the following T lines containing a integer n. ()

Output

For each case, output the answer in one single line.

Sample Input

2
18
21

Sample Output

9
11

题意:问1[,n]区间中,有多少个数,它的2,3,5,7,11,13的这几个因子数目之和为偶数

思路:预处理出所有的x,满足x只含有2,3,5,7,11,3这几个质因子,且数目为偶数。x的数目13000+;

对于一个数n,枚举所有的x,对于一个x,f(n/x)即求出[1,n/x]中不含有2,3,5,7,11,13作为因子的数有多少个,这个是经典的容斥问题。对所有的f(n/x)求和即可

    我用优先队列和map处理x;全用ll超时;有个地方会爆int,处理了下

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
#define pi 4*atan(1)
//#pragma comment(linker, "/STACK:102400000,102400000")
int p[]={,,,,,};
int num[],ji,ans;
struct is
{
int x;
int step;
bool operator <(const is a)const
{
return x>a.x;
}
};
priority_queue<is>q;
map<int,int>m;
int gcd(int x,int y)
{
return y==?x:gcd(y,x%y);
}
void init()
{
ji=;
is a;
a.x=;
m[]=;
a.step=;
q.push(a);
while(!q.empty())
{
is b=q.top();
if(b.x>1e9)
break;
q.pop();
if(b.step%==)
num[ji++]=b.x;
for(int i=;i<;i++)
{
is c;
ll gg=(ll)b.x*p[i];
if(gg>1e9)break;
c.step=b.step+;
c.x=(int)gg;
if(c.x<=1e9&&m[c.x]==)
q.push(c),m[c.x]=;
}
}
}
void dfs(int lcm,int pos,int step,int x)
{
if(lcm>x)
return;
if(pos==)
{
if(step==)
return;
if(step&)
ans+=x/lcm;
else
ans-=x/lcm;
return;
}
dfs(lcm,pos+,step,x);
dfs(lcm/gcd(p[pos],lcm)*p[pos],pos+,step+,x);
}
int main()
{
int x,y,z,i,t;
init();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&x);
int Ans=;
for(i=;i<ji&&num[i]<=x;i++)
{
ans=;
dfs(,,,x/num[i]);
Ans+=(x/num[i]-ans);
}
printf("%d\n",Ans);
}
return ;
}

xtu 1242 Yada Number 容斥原理的更多相关文章

  1. xtu 1242 Yada Number 打表

    Yada Number       Time Limit : 2000 MS   Memory Limit : 65536 KB   Yada Number Problem Description: ...

  2. XTU 1242 Yada Number 容斥

    Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime ...

  3. ZOJ 3233 Lucky Number --容斥原理

    这题被出题人给活活坑了,题目居然理解错了..哎,不想多说. 题意:给两组数,A组为幸运基数,B组为不幸运的基数,问在[low,high]区间内有多少个数:至少被A组中一个数整除,并且不被B中任意一个数 ...

  4. HDU 4390 Number Sequence 容斥原理

    Number Sequence Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. HDU 4390 Number Sequence (容斥原理+组合计数)

    HDU 4390 题意: 大概就是这样.不翻译了: Given a number sequence b1,b2-bn. Please count how many number sequences a ...

  6. [CF245H] Queries for Number of Palindromes (容斥原理dp计数)

    题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串 ...

  7. XTU OJ 1210 Happy Number (暴力+打表)

    Problem Description Recently, Mr. Xie learn the concept of happy number. A happy number is a number ...

  8. xtu summer individual 6 B - Number Busters

    Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  9. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

随机推荐

  1. 【BZOJ4428】[Nwerc2015]Debugging调试 记忆化搜索+分块

    [BZOJ4428][Nwerc2015]Debugging调试 Description 你看中的调试器将不会在这件事上帮助你.有代码可以通过多种方式在调试与正式发布的间隙发生不同的行为,当出现这种情 ...

  2. 批量远程执行linux服务器程序--基于pxpect(多进程、记日志版)

    #!/usr/bin/python '''Created on 2015-06-09@author: Administrator''' import pexpect import os,sys fro ...

  3. WCF(五) 深入理解绑定

    适用于本机WCF-WCF交互性能最佳的绑定: 允许跨主机,但只能用于部署同一台主机上,不能访问命名管道 netNamePipeBinding总结 一 WCF与SOA SOA是一种通过为所有软件提供服务 ...

  4. 如何通过命令在Ubuntu中安装PyCharm

    对于Ubuntu 16.10和Ubuntu 17.04,通过Ctrl + Alt + T打开终端,或通过从应用启动器搜索“terminal”,打开后,执行以下步骤: 安装: 1.通过命令添加PPA存储 ...

  5. 南京网络赛J-Sum【数论】

    A square-free integer is an integer which is indivisible by any square number except 11. For example ...

  6. 从零开始写JavaWeb框架(第二章节)

    这一章太多了...好累,不想写那么细了,就做一点总结吧. package org.smart4j.chapter2.controller; import java.io.IOException; im ...

  7. C++循环的内存释放问题?

    针对http://wenku.baidu.com/view/56d732ee856a561252d36ff2.html的内容测试一下. #include "A.h" #includ ...

  8. Selenium之Chrome浏览器的启动问题及解决

    System.setProperty("webdriver.chrome.driver","chromedriver.exe路径"); 配置好Chrome的驱动 ...

  9. Linux查看系统版本信息

    1.查看操作系统相关信息 uname -a 2.查看正在运行的内核版本信息 cat /proc/version //uname -r 3.查看发行版本号  (适用于所有的linux,包括Redhat. ...

  10. stringbuffer 和 stringbuilder区别

    stringbuffer  和  stringbuilder速度                 小于         线程安全           线程非安全 单线程操作大量数据用stringbui ...