首页 > ACM题库 > HDU-杭电 > HDU 4135-Co-prime[解题报告]HOJ
2015
04-16

HDU 4135-Co-prime[解题报告]HOJ

Co-prime

问题描述 :

Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.

输入:

The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).

输出:

The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).

样例输入:

2
1 10 2
3 15 5

样例输出:

Case #1: 5
Case #2: 10

Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.

#include <cstdio>
int f[30],cnt;
typedef long long LL;
LL a,b;
int n;
LL tol,ans;
void dfs(int s,LL mul,int t)
{
    if(s==cnt){
        if(t) ans=ans+tol/mul;
        else ans=ans-tol/mul;
        return;
    }
    dfs(s+1,mul,t);
    dfs(s+1,mul*f[s],1-t);
}
LL cal(LL x)
{
    if(x==0) return 0;
    tol=x;
    ans=0;
    dfs(0,1,1);
    return ans;
}
int main()
{
  // freopen("1.in","r",stdin);
    int T,ca=0;
    scanf("%d",&T);
    while(T--){
        scanf("%I64d%I64d%d",&a,&b,&n);
        if(n==1) { printf("Case #%d: %I64d\n",++ca,b-a+1);continue;}
        cnt=0;
        for(int j=2;j*j<=n;j++){
            if(n%j==0){
                f[cnt++]=j;
                while(n%j==0) n/=j;
            }
        }
        if(n>1) f[cnt++]=n;

       // for(int i=0;i<cnt;i++) printf("%d\n",f[i]);
        printf("Case #%d: %I64d\n",++ca,cal(b)-cal(a-1));
    }
}

  1. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥

  2. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的

  3. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.

  4. [email protected]

  5. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮