Happy 2006
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 11458   Accepted: 4001

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
思路:若a与m互素,那么a+t*m(t>=1)与m 也互素,否则不互素.设小于m且与m互素的数有n个,分别为a(0),a(1),a(2),...,a(n-1).那么第n+1个为a0+m,第n+2个为a(1)+m...第k个为m*(k-1)+a((k-1)%n);
#include <cstdio>
using namespace std;
const int MAXN=;
int m,k;
int relative[MAXN],top;
int gcd(int a,int b)
{
if(b==) return a;
else return gcd(b,a%b);
}
void sieve()
{
for(int i=;i<=m;i++)
{
if(gcd(i,m)==)
{
relative[top++]=i;
}
}
}
int main()
{
while(scanf("%d%d",&m,&k)!=EOF)
{
top=;
sieve();
int n=(k-)/top;
int z=(k-)%top;
int res=n*m+relative[z];
printf("%d\n",res);
}
return ;
}

容斥原理+二分.

容斥原理介绍:http://baike.baidu.com/link?url=H0UEe3zE2jUT7Ree_tycNyXcLYRWH4v25KpCZ3DOcx2HN0jaMYB3rJNF45SFs_EDxWo01C0LCz1rrh-_CG4On_

n/p表示1~n中是p倍数的数的个数。求1~m中与n互素的数的个数。先将n进行质因数分解,然后通过位运算枚举所有质因数的组合。若选了奇数个质因数ans+=m/质因数之积,否则ans-=m/质因数之积。然后二分枚举m的范围,确定k.

#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
LL sieve(LL n,LL m)
{
vector<LL> divisor;
for(LL i=;i*i<=n;i++)
{
if(n%i==)
{
divisor.push_back(i);
while(n%i==) n/=i;
}
}
if(n>) divisor.push_back(n);
LL ans=;
for(LL mark=;mark<(<<divisor.size());mark++)
{
LL mul=;
LL odd=;
for(LL i=;i<divisor.size();i++)
{
if(mark&(<<i))
{
odd++;
mul*=divisor[i];
}
}
LL cnt=m/mul;
if(odd&) ans+=cnt;
else ans-=cnt;
}
return m-ans;
}
LL n,k;
int main()
{
while(scanf("%lld%lld",&n,&k)!=EOF)
{
LL left=;
LL right=1LL<<;
while(right-left>)
{
LL mid=(left+right)>>;
LL cnt=sieve(n,mid);
if(cnt>=k)
{
right=mid;
}
else
{
left=mid;
}
}
printf("%lld\n",right);
}
return ;
}

Java版:

import java.util.Scanner;
import java.util.ArrayList;
public class Main{
Scanner in = new Scanner(System.in);
long m, k;
long sieve(long n, long m)
{
ArrayList<Long> divisor = new ArrayList();
for(long i = ; i * i <= n; i++)
{
if(n % i == )
{
divisor.add(i);
while(n % i == ) n /= i;
}
}
if(n > ) divisor.add(n);
long ret = ;
for(long mark = , size = divisor.size(); mark < ( << size); mark++)
{
long odd = ;
long mul = ;
for(int i = ; i < size; i++)
{
if((mark & (1L << i)) != )
{
odd++;
mul *= divisor.get(i);
}
}
if(odd % == )
{
ret += m / mul;
}
else
{
ret -= m / mul;
}
}
return m - ret;
}
Main()
{
while(in.hasNext())
{
m = in.nextLong();
k = in.nextLong();
long left = , right = 1L << ;
while(right > left)
{
long mid = (right + left) >> ;
long s = sieve(m, mid);
if(s >= k)
{
right = mid;
}
else
{
left = mid + ;
}
}
System.out.println(right);
}
}
public static void main(String[] args){ new Main();
}
}

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

  1. poj2773 —— 二分 + 容斥原理 + 唯一分解定理

    题目链接:http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

  2. POJ2773 Happy 2006【容斥原理】

    题目链接: http://poj.org/problem?id=2773 题目大意: 给你两个整数N和K.找到第k个与N互素的数(互素的数从小到大排列).当中 (1 <= m <= 100 ...

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

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

  4. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  5. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  6. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

  7. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

  8. HDU5838 Mountain(状压DP + 容斥原理)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...

  9. 【BZOJ-2669】局部极小值 状压DP + 容斥原理

    2669: [cqoi2012]局部极小值 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 561  Solved: 293[Submit][Status ...

随机推荐

  1. INSPIRED启示录 读书笔记 - 第38章 打造企业级产品的经验

    十大要点 1.可用性:很少有企业开发这类软件时会进行交互设计.视觉设计.可用性测试,因此产品才会表现得如此糟糕 2.产品正常工作:多数企业级产品根本没法使用,或者还需花大量的时间和资金开发临时补丁,产 ...

  2. FreeMarker缓存处理

    FreeMarker 的缓存处理主要用于模版文件的缓存,一般来讲,模版文件改动不会很频繁,在一个流量非常大的网站中,如果频繁的读取模版文件对系统的负担还是很重的,因此 FreeMarker 通过将模版 ...

  3. Windows 配置Apache以便在浏览器中运行Python script的CGI模式

    打开httpd.conf,找到”#ScriptInterpreterSource Registry “,移除前面的注释# (如果找不到这行,就自己添加进去) 找到“Options Indexes Fo ...

  4. HTML中table边框的显示总结

    一.1.显示表格的4个边框:<table border frame=box>2.只显示上边框: <table border frame=above>3.只显示下边框: < ...

  5. (转)Openstack Cascading和Nova Cell

    Openstack是看着AWS成长起来的,这点毋庸置疑:所以AWS大规模商用已成事实的情况下,Openstack也需要从架构和产品实现上进行优化,以满足大规模商用的要求.从已有的实现来看其中两种方式值 ...

  6. 去除sql中不可见字符的n种方法

    CREATE TABLE [ASCII0127] ( Bin          INT, Dec          INT, Hex          VARCHAR(128), Abbr       ...

  7. jsp:tld标签

    z注意每个uri地址要保持统一 1.创建MytagPrinta.java文件 package cn.tag; import java.io.IOException; import javax.serv ...

  8. spring boot: 条件注解@Condition

    @Conditional根据满足某一个特定的条件创建一个特定的Bean(基于条件的Bean的创建,即使用@Conditional注解). 比方说,当一个jar包在一个类的路径下的时候,自动配置一个或多 ...

  9. 19条ANDROID平台设计规范平台设计规范

    1.尺寸以及分辨率: Android的界面尺寸比较流行的有:480*800.720*1280.1080*1920,我们在做设计图的 时候建议是以 480*800的尺寸为标准: 2.界面基本组成元素: ...

  10. 我总结的call()与apply()方法的区别

    [call()与apply()的区别]在ECMAScript中每一个函数都是function类型(是javascript的基本引用类型)的实例,具有一定的属性和方法.call()和apply()则是这 ...