HDU 1286. Find new friends. "Filter Method" "Euler function" "November 25" __ function

Source: Internet
Author: User
Tags cmath

Find new Friends

Problem Description The New Year is coming, "Pig Head Help Association" ready to engage in a party, already know the existing member N people, the membership from 1 to n number, where the president's number is N, and the president is an old friend, then the member's number affirmation and n have more than 1 of the convention number, otherwise are new friends , now the president wants to know how many new friends there are. Please compile the program gang length to calculate it.
Input the first line is the number of test data of the group CN (case number,1<cn<10000), followed by the CN line positive integer n (1<n<32768), indicating the number of members.
Output for each n, the number of new friends out of a row, so that a total of CN line output.

Sample Input

2 25608 24027
Sample Output
7680 16016 This question I use the screening method, also may use the Euler function the method. Look at the code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int F[35000],cn,n,ans;
int main ()
{
    cin>>cn;
    while (cn--)
    {
        ans=0;
        memset (F,0,sizeof (f));
        scanf ("%d", &n);
        for (int i=2;i<n;i++)
        {
            if (n%i==0&&f[i]==0)
            {
                for (int j=i;j<n;j+=i)//There must be a convention number I, sift off
                    f[j]=1
            }
        }
        for (int i=1;i<n;i++)
        {
            if (!f[i]) ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


the Euler function method is as follows:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
# Include<iostream>
using namespace std;
typedef long long LL;
ll Eular (ll x)
{
    if (x = = 0) return 0;
    LL res = 1, t = x;
    for (ll i = 2; I <= (ll) sqrt (1.*x); i++)
    {
        if (t%i = = 0)
        {
            res *= (i-1);
            T/= i;
            while (t%i ==0)
            {
                res *= i;
                T/= i}
        }
        if (t = = 1) break
    ;
    if (T > 1) {res *= (t-1);}
    return res;
}
int main ()
{
    LL x;
    int N;
    scanf ("%d", &n);
    while (n--)
    {
        scanf ("%lld", &x);
        cout << Eular (x) << Endl;
    }
    return 0;
}

Spents 0MS.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.