zoukankan      html  css  js  c++  java
  • hdu 4135 -Co-prime

    找区间[a,b]中所有与n互素的数字个数,思想也很简单,首先小小技巧:分别算出平[1,b]中与n互素的总个数,再减去[1,a-1]中与n互素的个数。其次,运用容斥原理,学长学姐说得好,加加减减总没错。由于系统崩盘了,此时的我不知道这代码是否已AC掉了,姑且相信自己一回吧,上天有眼。

    #include <iostream>
    
    using namespace std;
    int prime[50];
    long long ges(long num,int m)
    {
    
     long long ans=0,tmp,i,j,flag;
    
        for(i=1; i<(long long)(1<<m); i++)
        {
            tmp=1,flag=0;
            for(j=0; j<m; j++)
                if(i&((long long)(1<<j)))
                    flag++,tmp*=prime[j];
            if(flag%2==1)//奇数加,偶数减
                ans+=num/tmp;//计算与n有公共因子的数的个数
            else
                ans-=num/tmp;
        }
        return ans;
    }
    
    int main()
    {
        int c;
        cin >> c;
       for(int count = 1;count <= c;count++)
        {
            long long a,b,n;
    
            cin >> a >> b >> n;
            int index = 0;
    
    
            for(int i = 2; i * i<= n; i++)
            {
    
                if(n % i == 0)
                {
                    prime[index++] = i;
    
                    while(n % i == 0)
                    {
                        n /= i;
                    }
    
                }
            }
            if(n>1)
                prime[index++]=n;
    
       long long ans=(b-ges(b,index))-(a-1-ges(a-1,index));
        cout << "#case" << count << ": " << ans << endl;
        }
    
        return 0;
    }
  • 相关阅读:
    iOS的文字自适应
    指向指针的指针
    NSString的创建
    Foundation-常用结构体
    Foundation summary
    成员变量补充
    Block^
    Protocol
    Category-分类
    李明杰要在广州开课啦
  • 原文地址:https://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3863473.html
Copyright © 2011-2022 走看看