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. ...
随机推荐
- linux下安装F-prot杀毒软件
一. f-prot的安装 1.首先我们要创建一个带有超级权限的用户 sudo passwa root 2.su 切换用户 3.下载F-prot http://www.f-prot.com/downlo ...
- Nexus介绍
转自:https://www.cnblogs.com/wincai/p/5599282.html 开始在使用Maven时,总是会听到nexus这个词,一会儿maven,一会儿nexus,当时很是困惑, ...
- javascript飞机大战-----007爆炸效果
要检验什么时候碰撞,我们必须了解什么时候不相撞.以上四种情况是不相撞的时候 首先在引擎里面写好什么时候碰撞什么时候不碰撞 /* 游戏引擎 */ var Engine = { //刚开始的游戏状态 ga ...
- 使用_Capistrano_进行自动化部署(2)
之前的一篇文章是为了解决问题而写的,很多东西都没有介绍清楚,这一篇文章就是完整介绍一下 Capistrano,主要的参考来源是 Modern PHP 这本书. Capistrano 是用于自动部署应用 ...
- js数组转成对象
$scope.addalerts = []; $scope.addalertsString = JSON.stringify($scope.addalerts); 全部教程http://each.si ...
- ZOJ 3469Food Delivery(区间DP)
Food Delivery Time Limit: 2 Seconds Memory Limit: 65536 KB When we are focusing on solving prob ...
- Excel 26机制转换
[问题描述] 在Excel中,列的名称是这样一个递增序列:A.B.C.….Z.AA.AB.AC.….AZ.BA.BB.BC.….BZ.CA.….ZZ.AAA.AAB….我们需要将上述列名序列和以下自然 ...
- JavaScript原型链的理解
JavaScript中的每一个对象都有prototype属性,我们称之为原型,而原型的值也是一个对象,因此它有自己的原型,这样就串联起来形成了一条原型链.原型链的链头是object,它的prototy ...
- 比特币 BTC本地测试节点
BTC本地测试节点 #环境 ubuntu 16.4 #客户端安装 #下载页面 #https://bitcoin.org/zh_CN/download cd /opt/ wget https://bit ...
- Navicat连接服务器上的Mysql数据库