Hihocoder 1165: Puzzle game

Source: Internet
Author: User
Tags cmath

Time limit: 20000ms single point time limit: 1000ms memory limit: 256MB description

Fragrance today is in a good mood, and the flowers in the field of insects play a puzzle game.
This game is like this, for an array A, fragrance Select a number a from a, insects select a number b from a. A and B can be the same. Their score is the number of a*b factors.
The delicate fragrance and the worm of course want to get as high a score as possible, can you tell them which two number to choose?
Since the fragrance is a very casual person, the elements in array A are randomly chosen by her.

Input

A row of number N, which represents the number of integers in a.
The next line of N is the number of A1,A2,..., an, respectively, as an element of a.

n <= 100000, 1 <= ai <= 100000
Ensure that all AI are randomly generated.

Output

A row represents the maximum score.

How to think there is no algorithm, in fact, the right violent posture can also be a miracle.

At first, we only think of the number of factors b[i], and then skillfully use break;

This form: if (Ans>b[i]*b[j]) break a layer of loops, b arrays are sorted by the number of factors from large to small, so correct.

But each time we calculate the sum of the number of factors (a[i],a[j]), we have to enumerate through the past, TLE.

For example, this code:

#include <cstdio> #include <algorithm> #include <cmath> #include <string.h> #include <string >using namespace std; #define N 100001int p[n];int b[n],t;struct node{int x, y;}    A[n];void Prime () {for (Int. i=2;i<n;i++) if (!b[i]) for (int j=i+i;j<n;j+=i) b[j]=1; for (int i=2;i<n;i++) if (!b[i]) p[++t]=i;}   int work (int x,int y) {int sum=1;   int t=1;       while (1) {if (x==1&&y==1) return sum;       int v=1;               if (x%p[t]==0) {while (x%p[t]==0) {v++;           X/=P[T];               }} if (Y%p[t]==0) {while (y%p[t]==0) {v++;           Y/=P[T];       }} t++;   Sum*=v; }}int CMP (node A,node b) {return a.y>b.y;}    int main () {prime ();    Memset (b,0,sizeof (b));    int n;    scanf ("%d", &n);    for (int i=1;i<=n;i++) scanf ("%d", &a[i].x), A[i].y=work (a[i].x,1); for (int i=1;i<=n;i++) printf ("%d", a[i].y);    Sort (a+1,a+n+1,cmp);    int ans=0;    for (int i=1;i<=n;i++) for (int. j=i+1;j<=n;j++) {if (ans>a[i].y*a[j].y) break;    else Ans=work (a[i].x,a[j].x);    } printf ("%d\n", ans); return 0;}

So I see a template: Linear handsome, and each time we can find a number of the precursor prime factor

1#include <cstdio>2#include <algorithm>3#include <cmath>4#include <string.h>5#include <string>6 7 using namespacestd;8 9 #defineN 100001Ten  One intP[n]; A intb[n],t=0; -  - structnode the { -     intx, y; - }a[n]; - voidPrime () + { -     for(intI=2; i<n;i++) +    { A        if(!b[i]) p[++t]=b[i]=i; at         for(intj=1; j<=t&&p[j]<=b[i]&&p[j]<=n/i;++j) b[i*p[j]]=P[j]; -    } -b[1]=N; - } -  -  intWorkintXinty) in { -    intsum=1; to    intC=0; +     while(x>1|| Y>1) -    { the        if(B[x]<b[y]) c=B[x]; *        ElseC=B[y]; $        intv=1;Panax Notoginseng         for(; b[x]==c;x/=c) v++; -         for(; b[y]==c;y/=c) v++; thesum*=v; +    } A    returnsum; the } +InlineintCMP (node A,node b) - { $     returnA.y>b.y; $ } -  - intMain () the { - Prime ();Wuyi  the     intN; -scanf"%d",&n); Wu      for(intI=1; i<=n;i++) -scanf"%d", &a[i].x), A[i].y=work (a[i].x,1); About  $Sort (A +1, a+n+1, CMP); -     intans=a[1].y; -N=min (N, $); -  A      for(intI=1; i<=n;i++) +      for(intj=i+1; j<=n;j++){ the     if(ANS&GT;A[I].Y*A[J].Y) Break; -     Elseans=Max (Ans,work (a[i].x,a[j].x)); $     } the  theprintf"%d\n", ans); the     return 0; the}
View Code

But you will find still very slow, super slow, 9000ms, and then look at a code, found!!

We enumerate the

for (int i=1;i<=n;i++) for    (int j=i+1;j<=n;j++)
Because n can be very large, about 100,000, but the back is mostly useless, so we just n<=1000, and so on is enough.

Violence is horrible.

Hihocoder 1165: Puzzle game

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.