hdu 2654(欧拉函数)

Become A Hero

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 210    Accepted Submission(s): 57


Problem Description
Lemon wants to be a hero since he was a child. Recently he is reading a book called “Where Is Hero From” written by ZTY. After reading the book, Lemon sends a letter to ZTY. Soon he recieves a reply.

Dear Lemon,
It is my way of success. Please caculate the algorithm, and secret is behind the answer. The algorithm follows:
Int Answer(Int n)
{
.......Count = 0;
.......For (I = 1; I <= n; I++)
.......{
..............If (LCM(I, n) < n * I)
....................Count++;
.......}
.......Return Count;
}
The LCM(m, n) is the lowest common multiple of m and n.
It is easy for you, isn’t it.
Please hurry up!
ZTY

What a good chance to be a hero. Lemon can not wait any longer. Please help Lemon get the answer as soon as possible.
 

 

Input
First line contains an integer T(1 <= T <= 1000000) indicates the number of test case. Then T line follows, each line contains an integer n (1 <= n <= 2000000).
 

 

Output
For each data print one line, the Answer(n).
 

 

Sample Input
1 1
 

 

Sample Output
0
 
LCM(a,b)=a*b/gcd(a,b) 如果 要LCM(a,b) < a*b 那么gcd(a,b)不能为1,所以题目就转换成了 1-n 内与n不互质的数有多少个了。利用欧拉函数求出互质的个数,减掉就是答案。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = 2000005;
int euler[N];
void getEuler()
{
    memset(euler,0,sizeof(euler));
    euler[1] = 1;
    for(int i = 2; i <= N; i++){
        if(!euler[i])
            for(int j = i; j <= N; j+= i)
            {
                if(!euler[j])
                    euler[j] = j;
                euler[j] = euler[j]/i*(i-1);
            }
    }
}

int main()
{
    getEuler();
    int tcase;
    scanf("%d",&tcase);
    while(tcase--){
        int n;
        scanf("%d",&n);
        printf("%d\n",n-euler[n]);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5532256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值