zoukankan      html  css  js  c++  java
  • Happy 2006(poj2773)

    Happy 2006
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 10856   Accepted: 3776

    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
    思路:容斥原理+二分;
    先打表素数,然后求要求的数的不同质因数;然后二分找数,用容斥求在[1,mid]与N不互质的数,然后求得与N互质的数,
    最后最小那个[1,mid]有m个与N互质的数的mid即所求;
      1 #include<stdio.h>
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<string.h>
      5 #include<queue>
      6 #include<stack>
      7 #include<map>
      8 using namespace std;
      9 typedef  long long LL;
     10 bool prime[1000005];
     11 LL ans[100005];
     12 LL fen[100];
     13 LL ak[12];
     14 LL cc[3000];
     15 queue<LL>que;
     16 int main(void)
     17 {
     18         LL n,m;
     19         int i,j;
     20         for(i=2; i<2000; i++)
     21         {
     22                 if(!prime[i])
     23                 {
     24                         for(j=i; i*j<=1000000; j++)
     25                         {
     26                                 prime[i*j]=true;
     27                         }
     28                 }
     29         }
     30         int cnt=0;
     31         for(i=2; i<=1000000; i++)
     32         {
     33                 if(!prime[i])
     34                 {
     35                         ans[cnt++]=(LL)i;
     36                 }
     37         }
     38         while(scanf("%lld %lld",&n,&m)!=EOF)
     39         {
     40 
     41                 {
     42                         LL nn=n;
     43                         int flag=0;
     44                         int cn=0;
     45                         int t=0;
     46                         while(nn>1)
     47                         {
     48                                 if(!flag&&nn%ans[t]==0)
     49                                 {
     50                                         flag=1;
     51                                         nn/=ans[t];
     52                                         que.push(ans[t]);
     53                                 }
     54                                 else if(nn%ans[t]==0&&flag)
     55                                 {
     56                                         nn/=ans[t];
     57                                 }
     58                                 else
     59                                 {
     60                                         flag=0;
     61                                         t++;
     62                                 }
     63                         }
     64                         while(!que.empty())
     65                         {
     66                                 fen[cn]=que.front();
     67                                 que.pop();
     68                                 cn++;
     69                         }
     70                         for(i=1; i<=(1<<cn)-1; i++)
     71                         {
     72                                 int  t=0;
     73                                 LL cp=1;
     74                                 for(j=0; j<cn; j++)
     75                                 {
     76                                         if(i&(1<<j))
     77                                         {
     78                                                 t++;
     79                                                 cp*=fen[j];
     80                                         }
     81                                 }
     82                                 if(t%2)
     83                                 {
     84                                         cc[i]=cp;
     85                                 }
     86                                 else cc[i]=-cp;
     87                         }
     88                         LL l;
     89                         LL r;
     90                         l=1;
     91                         r=1e18;
     92                         LL id=0;
     93                         while(l<=r)
     94                         {
     95                                 LL mid=(l+r)/2;
     96                                 LL sum=0;
     97                                 for(i=1; i<=(1<<cn)-1; i++)
     98                                 {
     99                                         sum=sum+(mid/cc[i]);
    100                                 }
    101                                 sum=mid-sum;
    102                                 if(sum>=m)
    103                                 {
    104                                         id=mid;
    105                                         r=mid-1;
    106                                 }
    107                                 else
    108                                 {
    109                                         l=mid+1;
    110                                 }
    111                         }
    112                         printf("%lld
    ",id);
    113                 }
    114         }
    115         return 0;
    116 }

    油!油!you@
  • 相关阅读:
    鲲鹏服务器测试
    缓存区溢出实验
    读书笔记
    《信息安全系统设计与实现》学习笔记9
    改进ls的实现
    团队作业(四):描述设计
    《需求规格书》修订版
    反汇编测试
    《信息安全系统设计与实现》学习笔记8
    stat命令的实现-mystat
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5465698.html
Copyright © 2011-2022 走看看