hdu_1286 Find new friends (Euler's theorem)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1286

Find new friends

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 10969 Accepted Submission (s): 5818


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 Input22560824027

Sample Output7680 16016

The puzzle: The number of 1~n-1 and N coprime, in fact, is to find the Euler function of n, can be directly into the formula F (n) = n (1-1/p1) * (1-1/P2) ... * (1-1/PK) (pi is an n-factor decomposition of each factorization)

1 //Euler functions2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intN =32770;7 intAns[n];8 BOOLPri[n];9 voidInit ()Ten { One      for(inti =1; i < N; i++) Ans[i] =i; Apri[0] = pri[1] =1; -      for(inti =2; i < N; i++){ -         if(!Pri[i]) { theans[i]=i-1; -              for(intj = i+i; J < N; j+=i) { -PRI[J] =1; -ANS[J] = ans[j]/i* (i-1); +             } -         } +     } A     return; at } - intMain () - { -     intT; -scanf"%d",&T); - init (); in      while(t--) -     { to         intN; +scanf"%d",&n); -printf"%d\n", Ans[n]); the     } *     return 0; $ }Panax Notoginseng  - /* the  + //Find Euler function A #include <cstdio> the #include <cstring> + #include <algorithm> - #include <vector> $ using namespace std; $ const int N = 33000; - int mp[n][50]; - int cnt[n]; the bool Pri[n]; - void init ()Wuyi { the memset (cnt,0,sizeof (CNT)); - memset (Mp,0,sizeof (MP)); Wu pri[0] = pri[1] = 1; - for (int i = 2; i < N; i++) About     { $ if (!pri[i]) { - mp[i][0] = i; - Cnt[i] = 1; - For (int j = i+i; j < N; j+=i) { A Pri[j] = 1; + mp[j][cnt[j]++] = i; the             } -         } $     } the } the int main () the { the int T; - scanf ("%d", &t); in init (); the While (t--) the     { About int n; the scanf ("%d", &n); the int ans = n; the for (int i = 0; i < cnt[n]; i++) { + ans = ans/mp[n][i]* (mp[n][i]-1); -         } the printf ("%d\n", ans);Bayi     } the return 0; the } - */

hdu_1286 Find new friends (Euler's theorem)

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.