zoukankan      html  css  js  c++  java
  • poj2773 —— 二分 + 容斥原理 + 唯一分解定理

    题目链接:http://poj.org/problem?id=2773


    Happy 2006
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 12942   Accepted: 4557

    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


    题意:

    求第k个与m互质的数。

    题解:

    方法一:

     如果 gcd(a,b) = 1, 那么成a与b互质。 注:1和任何数都互质

    根据唯一分解定理: 对一个数进行因式分解, 最终必定能分解为多个素数相乘的式子, 且这个式子是唯一的(1除外)。

    1.那么我们可以m进行素数分解, 并记录它由哪些素数构成。 如果一个数的分解式中不含有构成m的素数, 那么这个数就与m互素。

    2.然后二分答案ans, 如果在ans范围之内, 有>=k个与m互素的数, 那么就缩小范围, 否则扩大范围。

    3.那么怎么知道在ans范围内, 有多少个数有m互素呢? 用容斥原理。


    方法二:

    可知 gcd(a+b*k, b) = gcd(b, a%b), gcd(a, b) = gcd(b, a%b), 所以 gcd(a+b*k, b) = gcd(a, b), k为常数。

    这表明了:对于与b互素的数,他们对b取模的余数会周期性出现。 那么我们就只需要计算出在b的范围内, 与b互素的数有哪些就可以了。

    然后看第k个与b互素的数是在第几个周期的第几个就可以了。(注意:刚好在周期末时, 需要特判)。



    方法一:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <cmath>
     8 #include <map>
     9 #include <set>
    10 #include <queue>
    11 #include <sstream>
    12 #include <algorithm>
    13 using namespace std;
    14 #define pb push_back
    15 #define mp make_pair
    16 #define ms(a, b)  memset((a), (b), sizeof(a))
    17 typedef long long LL;
    18 const double eps = 1e-6;
    19 const int INF = 2e9;
    20 const LL LNF = 9e18;
    21 const int mod = 1e9+7;
    22 const int maxn = 1e5+10;
    23 
    24 LL m, k;
    25 LL fac[maxn], sum;
    26 
    27 void getFactor()
    28 {
    29     sum = 0;
    30     LL tmp = m;
    31     for(LL i = 2; i*i<=tmp; i++)
    32     if(tmp%i==0)
    33     {
    34         fac[sum++] = i;
    35         while(tmp%i==0) tmp /=  i;
    36     }
    37     if(tmp>1) fac[sum++] = tmp;
    38 }
    39 
    40 LL test(LL tmp)
    41 {
    42     if(m==1) return tmp;
    43     if(tmp==1) return 1;
    44 
    45     LL ret = 0;
    46     for(LL s = 1; s < (1<<sum); s++)
    47     {
    48         LL p = 1,  cnt = 0;
    49         for(LL j = 0; j<sum;  j++)
    50         if(s&(1<<j))
    51         {
    52             p *= fac[j];
    53             cnt++;
    54         }
    55         ret += (cnt&1)?(tmp/p):(-tmp/p);
    56     }
    57     return tmp - ret;
    58 }
    59 
    60 int main()
    61 {
    62     while(scanf("%lld%lld",&m,&k)!=EOF)
    63     {
    64         getFactor();
    65         LL l = k, r = LNF;
    66         while(l<=r)
    67         {
    68             LL mid = (l+r)>>1;
    69             if(test(mid)>=k)
    70                 r = mid - 1;
    71             else
    72                 l = mid + 1;
    73         }
    74         printf("%lld
    ", l);
    75     }
    76     return 0;
    77 }
    View Code

    方法二:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <cmath>
     8 #include <map>
     9 #include <set>
    10 #include <queue>
    11 #include <sstream>
    12 #include <algorithm>
    13 using namespace std;
    14 #define pb push_back
    15 #define mp make_pair
    16 #define ms(a, b)  memset((a), (b), sizeof(a))
    17 typedef long long LL;
    18 const double eps = 1e-6;
    19 const int INF = 2e9;
    20 const LL LNF = 9e18;
    21 const int mod = 1e9+7;
    22 const int maxn = 1e6+10;
    23 
    24 int pri[maxn];
    25 
    26 int gcd(int a, int b)
    27 {
    28     return b==0?a:gcd(b, a%b);
    29 }
    30 
    31 int main()
    32 {
    33     int m, k, sum;
    34     while(cin>>m>>k)
    35     {
    36         sum = 0;
    37         for(int i = 1; i<=m; i++)
    38             if(gcd(i,m)==1)
    39                 pri[sum++] = i;
    40 
    41         if(k%sum)
    42             cout<< (k/sum)*m+pri[(k%sum-1)] <<endl;
    43         else
    44             cout<< (k/sum-1)*m+pri[sum-1] <<endl;
    45     }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    Jenkins可用环境变量列表以及环境变量的使用(Shell/Command/Maven/Ant)
    CreateJS结合Falsh工具生成Canvas动画(加密字符串的由来)
    Linux下使用mv重命名文件或者移动文件(增强版的工具为rename)
    Windows7/8/10中无法识别USB设备的问题解决
    Eclipse工程中Java Build Path中的JDK版本和Java Compiler Compiler compliance level的区别(转)
    使用Docker部署Spring boot项目
    豆瓣API
    scrapy
    elasticsearch x-pack
    Document
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538689.html
Copyright © 2011-2022 走看看