Find new friendsTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total Submission (s): 9387 Accepted Submission (s): 4974
Problem description New Year is coming, "pig head to help the association" ready to engage in a party, already know the existing member N, the member 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 has more than 1 of the convention number, otherwise are new friends , now the president wants to know how many new friends there are? Please make up the program gang length calculation.
The first line of input is the number of groups of test data cn (case number,1<cn<10000), followed by a CN line positive integer n (1<n<32768), representing the number of members.
Output for each n, the number of new friends out of a line, so that a total of CN line output.
Sample Input
22560824027
Sample Output
768016016
Euler's function to make a table:Code:
#include <stdio.h> #include <string.h> #define SIZE 40000int euler[size];void Euler () { int i,j; memset (euler,0,sizeof (Euler)); Euler[1]=1; for (i=2;i<size;i++)//Play table to find Euler function { if (! Euler[i]) for (j=i;j<size;j+=i) { if (! EULER[J]) euler[j]=j; euler[j]=euler[j]/i* (i-1);}}} int main () { int n,m; scanf ("%d", &n); while (n--) { Euler (); scanf ("%d", &m); printf ("%d\n", Euler[m]);} }
direct targeting, do not hit the table:Code:
#include <stdio.h> #include <string.h> #define SIZE 40000int Euler (int n) { int i,j; int ret=n; for (i=2;i*i<=n;i++) { if (n%i==0) { ret=ret/i* (i-1),//except after multiplication, to prevent overflow while (n%i==0) n/=i;} } if (n>1) ret=ret/n* (n-1); return ret;} int main () { int n,m; scanf ("%d", &n); while (n--) { scanf ("%d", &m); printf ("%d\n", Euler (M));} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdoj 1286 Find new friends