POJ 3478 POJ 3090 (Application of Euler functions)

Source: Internet
Author: User

3478 The title meaning is that the Farey sequence consists of a series of fractional A/b (0<a<b<=n and gcd (b) =1) that cannot be numerator in ascending order, and then gives the first few items of FN, allowing you to calculate how many fractions are in FN.

It is obvious that the number of FN is the number of coprime and (2,3,4.....n-1,n) respectively. That is, the first n of the Euler function is obtained.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <stack>5#include <queue>6#include <map>7#include <algorithm>8#include <vector>9 Ten using namespacestd; One  A Const intMAXN =1000000; -  -typedefLong LongLL; the  -LL phi[maxn+1]; -  - voidInit () + { - LL i,j; +      for(i=1; i<=maxn;i++) Phi[i] =i; A      for(i=2; i<=maxn;i+=2) phi[i]/=2; at      for(i=3; i<=maxn;i+=2){ -         if(Phi[i] = =i) { -              for(j=i;j<=maxn;j+=i) { -PHI[J] = phi[j]/i* (i-1); -             } -         } in     } -      for(i=3; i<maxn+1; i++){ tophi[i]+=phi[i-1]; +     } - } the  * intMain () $ {Panax Notoginseng init (); -      intN; the       while(cin>>n&&N) { +printf"%lld\n", Phi[n]); A      } the  +     return 0; -}
View Code

3090 of the situation is similar to 3478, but to note that the point of view is the x=y of this straight line symmetry, so the final answer is 2*phi[n]+1,x=y also counted.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <stack>5#include <queue>6#include <map>7#include <algorithm>8#include <vector>9 Ten using namespacestd; One  A Const intMAXN = +; -  -typedefLong LongLL; the  -LL phi[maxn+1]; -  - voidInit () + { - LL i,j; +      for(i=1; i<=maxn;i++) Phi[i] =i; A      for(i=2; i<=maxn;i+=2) phi[i]/=2; at      for(i=3; i<=maxn;i+=2){ -         if(Phi[i] = =i) { -              for(j=i;j<=maxn;j+=i) { -PHI[J] = phi[j]/i* (i-1); -             } -         } in     } -      for(i=2; i<maxn+1; i++){ tophi[i]+=phi[i-1]; +     } - } the  * intMain () $ {Panax Notoginseng init (); -      intN; the      intCAs; +Cin>>CAs; A       for(intI=1; i<=cas;i++){ theCin>>N; +printf"%d%d%d\n", I,n,2*phi[n]+1); -      } $  $     return 0; -}
View Code

POJ 3478 POJ 3090 (Application of Euler functions)

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.