xtu 1242 Yada Number 容斥原理
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 容斥原理的更多相关文章
- xtu 1242 Yada Number 打表
Yada Number Time Limit : 2000 MS Memory Limit : 65536 KB Yada Number Problem Description: ...
- XTU 1242 Yada Number 容斥
Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime ...
- ZOJ 3233 Lucky Number --容斥原理
这题被出题人给活活坑了,题目居然理解错了..哎,不想多说. 题意:给两组数,A组为幸运基数,B组为不幸运的基数,问在[low,high]区间内有多少个数:至少被A组中一个数整除,并且不被B中任意一个数 ...
- HDU 4390 Number Sequence 容斥原理
Number Sequence Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 4390 Number Sequence (容斥原理+组合计数)
HDU 4390 题意: 大概就是这样.不翻译了: Given a number sequence b1,b2-bn. Please count how many number sequences a ...
- [CF245H] Queries for Number of Palindromes (容斥原理dp计数)
题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串 ...
- XTU OJ 1210 Happy Number (暴力+打表)
Problem Description Recently, Mr. Xie learn the concept of happy number. A happy number is a number ...
- xtu summer individual 6 B - Number Busters
Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- hdu4059 The Boss on Mars(差分+容斥原理)
题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设 则 为一阶差分. 二阶差分: n阶差分: 且可推出 性质: 1. ...
随机推荐
- 查看Oracle的redo日志切换频率
1.Oracle log 每次切换会记录到告警日志中 设想写个方案来查看log切换频率来判断Oracle log是否应该更改大小. 2.sql a.查看redo日志信息 select * from v ...
- 微信小程序 --- 获取设备信息
获取设备信息: wx.getSystemInfo model:手机型号 pixelRatio:设备像素比 windowWidth:窗口宽度 windowHeight:窗口高度 language:语言 ...
- 170531、FormData 对象的使用
通过FormData对象可以组装一组用 XMLHttpRequest发送请求的键/值对.它可以更灵活方便的发送表单数据,因为可以独立于表单使用.如果你把表单的编码类型设置为multipart/form ...
- Oracle体系结构之Oracle10gR2体系结构-内存、进程
oracle体系结构图1 oracle体系结构图2 用户进程(访问oracle的客户端的总称) 工具的使用:sqlplus.pl/sql developer 如何访问数据库: 本机直接通过sock方式 ...
- Django - 路由层(URLconf)
一.django 静态文件配置 /mysite1/settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_ ...
- java 并查集
并查集代码 并查集优化⼀ 并查集优化⼆ 实战题⽬目1. https://leetcode.com/problems/number-of-islands/2. https://leetcode.com/ ...
- Python开发【Django】:模板语言
排序 1.forloop.counter 表示循环的次数,它从1开始计数,第一次循环设为1 {% for item in todo_list %} <p>{{ forloop.counte ...
- 【python】BeautifulSoup的应用
from bs4 import BeautifulSoup#下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档): html_doc = ...
- SQL Server 不同网段IP通过名称访问
1, 设置订阅服务器C:\Windows\System32\drivers\etc目录的host文件,添加分发服务器(我的环境是发布服务器与分发服务器 是一起的,所以这里指定的是发布服务器的地址)信息 ...
- 常用linux shell脚本记录
遍历目录下所有的文件是目录还是文件 for file in ./* do if test -f $file then echo $file 是文件 fi if test -d $file then e ...