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.