[Miscellaneous Questions] URAL2047. Maths

Source: Internet
Author: User
Tags string back

Test instructions: Constructs a string of length n so that, except for the first, the number of prefixes and factors in each position is exactly equal to the number at that position.

N$\le 100000$

For example $a_i$:2 4 6 6 4 8 4 8 4 8

Prefixes and: 6 12 18 22 30 34 42 46 54

Factor of 6:1 2 3 6 is 4 equals a[2]

Factor of 12:1 2 3 4 6 12 is 6 equals a[3]

Factor of 18:1 2 3 6 9 18 is 6 equals a[4]

...

...

Factor of 54:1 2 3 6 9 18 27 54 is 8 equals a[9]

All are satisfied, the column is legal

The first time I've been thinking about where it impossible

And then we're going to get violent and see where.

Then I found that I was writing it, and I couldn't write it.

So I found that the number of the number of factors written out, backwards to reduce back,

There is only one way to go back every number, the longest is the answer.

Like what

27 This number goes back to a length of 9

26 This number goes back to a length of 6

...

Each number goes back with a length

And then I found out just the number minus the number of factors. How long will it take to get back to +1?

Well, just preprocess the number of factors.

Just for once, O (1) can get the length

(For the and, the length is the title of the input n, need to output is the longest length of that string back to the number of factors on the road)

So make a table like this, just 1 seconds.

intphi[20000005];intpri () {memset (phi,0,sizeof(PHI));  for(intI=2; i<=2000000; i++)         for(intj=i;j<=2000000; j+=i) phi[j]++;}intdp[20000005];voidPre () {memset (DP,0,sizeof(DP)); dp[1]=1, dp[2]=1;  for(intI=3; i<=2000000; i++)    {        intnum=phi[i]+1;//if (num>300 | | dp[i-num]==-1)//Dp[i]=-1;//Elsedp[i]=dp[i-num]+1;//printf ("%d%d\n", I, Dp[i]);        if(dp[i]>=100000) {printf ("%d%d\n", I, dp[i]);  Break; }    }}intMain () {//freopen ("OUT.txt", "w", stdout);pri ();    Pre (); return 0;}

The resulting 1568617 is the last number of N for 100000.

To get the n number, it's good to go backwards.

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4typedef pair<int,int>PI;5 Const intn=1e5+5;6 Const Doubleeps=1e-5;7 Const intmod=1e9+7;8 9 intans[100005], D;Ten intphi[1568625]; One voidPre () A { -Memset (PHI,0,sizeof(PHI)); -      for(intI=2; i<=1568617; i++) the          for(intj=i;j<=1568617; j+=i) -phi[j]++; -     intx=1568617; -D=0; +      while(x) -     { +ans[d++]=x; A         intnum=phi[x]+1; atx-=num; -     } - } -  - intMain () - { in pre (); -     intN; to      while(~SCANF ("%d",&N)) +     { -         intsum=0; the          for(inti=d-1, j=1; j<=n;j++, i--) *         { $printf"%d", ans[i]-sum);Panax Notoginsengsum=Ans[i]; -             if(j==N) thePuts""); +             Else APutchar (' '); the         } +     } -     return 0; $}
Ural 2047

[Miscellaneous Questions] URAL2047. Maths

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.