Hdu1299&&spoj-kpequ (Find the number of factors to find the solution)

Source: Internet
Author: User

Topic Link: HDU 1299 portal

Test instructions

The number of solutions to the 1/x+1/y=1/n of equations 1/3+1/2 and 1/2+1/3 are regarded as a set of solutions.

Analysis:

1/x+1/y = 1/n Set y = n + k;

==>1/x + 1/(n+k) =1/n;

==>x = n^2/k + N;

Because X is an integer, K is the approximate number of n^2.

The code is as follows:

 #include <iostream> #include <cstring> #include <cstdio> #include < algorithm>using namespace Std;const int maxn = 1e7+10;typedef long long ll;int p[maxn/10],cnt;bool vis[maxn];int fac[10    00],tot;void init () {cnt=0;    memset (vis,0,sizeof (VIS));            for (int i=2;i<maxn;i++) {if (!vis[i]) {p[cnt++]=i;        for (int j=i+i;j<maxn;j+=i) vis[j]=1;    }}}ll get (LL x) {tot=0;    memset (fac,0,sizeof (FAC));            for (int i=0;i<cnt&&p[i]*p[i]<=x;i++) {if (x%p[i]==0) {while (x%p[i]==0) fac[tot]++,x/=p[i];        tot++;    }} if (x>1) fac[tot++]=1;    LL ans = 1;    for (int i=0;i<tot;i++) ans=ans* (2*fac[i]+1); return ans;}    int main () {init ();    int T,cas=1;    scanf ("%d", &t);        while (t--) {LL n;        scanf ("%i64d", &n);    printf ("Scenario #%d:\n%i64d\n\n", cas++, (Get (n) +1)/2); } return 0;} 


Topic Link: spoj-kpequ Portal

Test instructions

The same question is almost the same as n replaced by n!, so high precision and n! factor decomposition is required.

Import Java.math.biginteger;import Java.util.scanner;public class Main {static int cnt; static int[] pri =new int[10010];s Tatic boolean[] Vis = new boolean[10010];p ublic static void init () {cnt=0;for (int i=0;i<10010;i++) vis[i] = False;for (in T i=2;i<10010;i++) {if (!vis[i]) {pri[cnt++]=i;for (int j=i+i;j<10010;j+=i) Vis[j]=true;}}} public static int calc (int n,int p) {if (n<p) return 0;else return calc (n/p,p) +n/p;} public static void Main (string[] args) {//TODO auto-generated method Stubinit (); Scanner cin = new Scanner (system.in), while (Cin.hasnext ()) {int N=cin.nextint (), if (n==0) break; BigInteger ans = biginteger.one;for (int i=0;i<cnt&&pri[i]<=n;i++) {int tmp = CALC (n,pri[i]); tmp = tmp*2+1; Ans=ans.multiply (biginteger.valueof (TMP));} System.out.println (ANS);}}


Hdu1299&&spoj-kpequ (Find the number of factors to find the solution)

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.