zoukankan      html  css  js  c++  java
  • GCD(欧拉函数)

    GCD

    http://acm.hdu.edu.cn/showproblem.php?pid=2588

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3782    Accepted Submission(s): 2053


    Problem Description
    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
    (a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
    Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.
     
    Input
    The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.
     
    Output
    For each test case,output the answer on a single line.
     
    Sample Input
    3
    1 1
    10 2
    10000 72
     
    Sample Output
    1
    6
    260
     
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define lson l,mid,rt<<1
     4 #define rson mid+1,r,rt<<1|1
     5 #define IT set<ll>::iterator
     6 #define pb push_back
     7 #define eb emplace_back
     8 #define maxn 200005
     9 #define eps 1e-6
    10 #define PI acos(-1.0)
    11 #define rep(k,i,j) for(int k=i;k<j;k++)
    12 typedef long long ll;
    13 typedef pair<int,int> pii;
    14 typedef pair<ll,ll>pll;
    15 typedef pair<ll,int> pli;
    16 typedef pair<pair<int,string>,pii> ppp;
    17 typedef unsigned long long ull;
    18 const long long MOD=1000000007;
    19 const double oula=0.57721566490153286060651209;
    20 const int INF=0x3f3f3f3f;
    21 using namespace std;
    22 
    23 
    24 ll Euler(ll n){
    25     ll ans=n;
    26     for(ll i=2;i*i<=n;i++){
    27         if(n%i==0) ans=ans/i*(i-1);
    28         while(n%i==0) n/=i;
    29     }
    30     if(n>1) ans=ans/n*(n-1);
    31     return ans;
    32 }
    33 
    34 int main(){
    35     std::ios::sync_with_stdio(false);
    36     int t;
    37     cin>>t;
    38     while(t--){
    39         ll n,m;
    40         cin>>n>>m;
    41         ll ans=0;
    42         for(int i=1;i*i<=n;i++){
    43             if(n%i==0){
    44                 if(i>=m) ans+=Euler(n/i);
    45                 if(i*i!=n&&n/i>=m) ans+=Euler(i);
    46             }
    47         }
    48         cout<<ans<<endl;
    49     }
    50 }
    View Code
  • 相关阅读:
    HDU4366 Successor 线段树+预处理
    POJ2823 Sliding Window 单调队列
    HDU寻找最大值 递推求连续区间
    UVA846 Steps 二分查找
    HDU3415 Max Sum of MaxKsubsequence 单调队列
    HDU时间挑战 树状数组
    UVA10168 Summation of Four Primes 哥德巴赫猜想
    UESTC我要长高 DP优化
    HDUChess 递推
    HDU4362 Dragon Ball DP+优化
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/10726914.html
Copyright © 2011-2022 走看看