容斥原理入门题吧。

Happy 2006
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 9798   Accepted: 3341

Description

Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are all relatively prime to 2006.

Now your job is easy: for the given integer m, find the K-th element which is relatively prime to m when these elements are sorted in ascending order.

Input

The input contains multiple test cases. For each test case, it contains two integers m (1 <= m <= 1000000), K (1 <= K <= 100000000).

Output

Output the K-th element in a single line.

Sample Input

2006 1
2006 2
2006 3

Sample Output

1
3
5

Source

 
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <stdlib.h>
using namespace std; int m,k;
int mark[];
int save[];
int pcnt;
int g[];
long long int sum;
int cnt; void getprime()
{
//1不是素数
for(int i=;i<=;i++)
{
if(mark[i]==) continue;
save[pcnt++]=i;
for(int j=i;j<=;j+=i)
mark[j]=;
}
} void dfs(int n,long long num,int s,long long int key)
{
if(n==)
{
sum += key/num;
return ;
}
if(s>=cnt) return ; for(int i=s;i<cnt;i++)
{
if(num*g[i]>key) continue;
else dfs(n-,num*g[i],i+,key);
}
} long long int fuc(long long int x)
{
if(x==) return ; long long ans=; int sign=;
for(int i=;i<=cnt;i++)
{
sum=;
if(sign==)
{
dfs(i,,,x);
ans+=sum;
}
else
{
dfs(i,,,x);
ans-=sum;
}
sign=sign^;
} return x-ans;//这里面应该不会出现负数吧
} int main()
{
getprime();
while(scanf("%d%d",&m,&k)!=EOF)
{
//然后就是分解一个数了
cnt=;
for(int i=;i<=m;i++)
{
int flag=;
while(m%i==)
{
if(flag==)
{
g[cnt++]=i;
}
flag=;
m/=i;
}
}
if(m!=) g[cnt++]=m; //然后就是容斥原理 int b=,d=;
while(b<d)
{
int mid=(b+d)/;
int key=fuc(mid);
if(key>=k) d=mid;
else b=mid+;
}
printf("%d\n",b);
}
return ;
}

poj 2773(容斥原理)的更多相关文章

  1. Happy 2006 POJ - 2773 容斥原理+二分

    题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include ...

  2. POJ 2773 Happy 2006#素数筛选+容斥原理+二分

    http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...

  3. poj 2773 Happy 2006 - 二分答案 - 容斥原理

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11161   Accepted: 3893 Description Two ...

  4. [poj 2773] Happy 2006 解题报告 (二分答案+容斥原理)

    题目链接:http://poj.org/problem?id=2773 题目大意: 给出两个数m,k,要求求出从1开始与m互质的第k个数 题解: #include<algorithm> # ...

  5. poj 2773 Happy 2006 容斥原理+二分

    题目链接 容斥原理求第k个与n互质的数. #include <iostream> #include <vector> #include <cstdio> #incl ...

  6. POJ 2773 Happy 2006(容斥原理+二分)

    Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10827   Accepted: 3764 Descr ...

  7. POJ 2773 Happy 2006 数学题

    题目地址:http://poj.org/problem?id=2773 因为k可能大于m,利用gcd(m+k,m)=gcd(k,m)=gcd(m,k)的性质,最后可以转化为计算在[1,m]范围内的个数 ...

  8. poj 2773 Happy 2006

    // 题意 :给你两个数 m(10^6),k(10^8) 求第k个和m互质的数是什么这题主要需要知道这样的结论gcd(x,n)=1 <==> gcd(x+n,n)=1证明 假设 gcd(x ...

  9. POJ 2773 Happy 2006(欧几里德算法)

    题意:给出一个数m,让我们找到第k个与m互质的数. 方法:这题有两种方法,一种是欧拉函数+容斥原理,但代码量较大,另一种办法是欧几里德算法,比较容易理解,但是效率很低. 我这里使用欧几里德算法,欧几里 ...

随机推荐

  1. 由浅入深了解Thrift之微服务化应用架构

    为什么选择微服务 一般情况下,业务应用我们都会采用模块化的分层式架构,所有的业务逻辑代码最终会在一个代码库中并统一部署,我们称这种应用架构为单体应用. 单体应用的问题是,全部开发人员会共享一个代码库, ...

  2. HDU4776 Ants(Trie && xor)

    之前mark下来的一道题,今天填一下坑. 题意是这样子的.给你一棵边上有权的树.然后有树上两点(u,v)的路径有n*(n-1)条,路径(u,v)的权值是边权的xor. 然后下面有m个询问,询问你n*( ...

  3. C# 中Newtonsoft.Json的安装和使用

    官网参考:http://json.codeplex.com/ 在程序包管理控制台,键入NuGet命令  install-package Newtonsoft.Json  安装Newtonsoft.Js ...

  4. 关于SQL查询效率,100w数据,查询只要1秒

    1.关于SQL查询效率,100w数据,查询只要1秒,与您分享:机器情况p4: 2.4内存: 1 Gos: windows 2003数据库: ms sql server 2000目的: 查询性能测试,比 ...

  5. 二叉查找树的查找、插入和删除 - Java实现

    http://www.cnblogs.com/yangecnu/p/Introduce-Binary-Search-Tree.html 作者: yangecnu(yangecnu's Blog on ...

  6. iOS tintColor解析

    在UIView中一个相对来说比较小的属性,tintColor属性是相当的强大.今天我们就来看看如何使用他,包含使用tint color进行着色标准控件.我们自定义控件甚至重新着色图像. 本章的实例程序 ...

  7. Redis的String操作

    set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] 如: set a 1 ex 10 , 10秒有效 Set a 1 px 9000 , 9秒有效 注: 如果ex, ...

  8. UML系列01之 UML用例图

    UML,全称是Unified Modeling Language,中文是"统一建模语言".通俗点说,UML是一种创建模型的语言. UML是在开发阶段,说明,可视化,构建和书写一个面 ...

  9. Android 时间轴

    最近开发的app中要用到时间轴这东西,需要实现的效果如下: 想想这个东西应该可以用listview实现吧.然后最近就模拟着去写了: 首先写  listview的item的布局: listview_it ...

  10. JVM生产环境参数实例及分析

    java application项目(非web项目) 改进前: -Xms128m-Xmx128m-XX:NewSize=64m-XX:PermSize=64m-XX:+UseConcMarkSweep ...