HDU 5407 CRB and candies (combined number + least common multiple + prime table + inverse) and 2015 Summer Multi-School games 10th

Source: Internet
Author: User

Test instructions

Enter N to find the least common multiple of all the combined numbers of C (n,0) to C (N,n).


The first line inputs an integer t, which indicates a common T Group test Sample.

Each set of test sample contains a positive integer n (1<=n<=1e6).


Output results (mod 1e9+7).

Feeling pretty sick, from the beginning of the game I was writing this problem, the game is over or not written out ...

During the period found the inverse, least common multiple, combined number of various formulas, but blew an afternoon tle.

The game is over, the puzzle tells me, formula seconds kill law ...

But the formula can not understand, fortunately there is a group of giant commentary, so some understand, but still need to continue to think to understand.


Set Ans[i] Represents the least common multiple of all combinations of I.

Set F[i] Represents the least common multiple of positive integers from 1 to I.

Then get the formula--ans[i] = f[i+1]/i from f[i] to ans[i]. Anss[i-1] = (f[i]*inv[i])%mod;

Then get the formula for F[i]--

if (i = = p^k) F[i] = f[i-1]*p;

else f[i] = f[i-1];

The next thing to do is to find those i = = p^k.

Problem Solving steps:

1. Number of prime tables;

2. Looking for i = = P^k by the prime number table;

3. f[1] = 1, hit f[] array table and ans[] array table;

4. Input data;

5. Output the answer based on the input data and ans[] table.

I think there are other ways, since the operating time of the AC program is from 15ms to 900+ms. More ways to keep looking.

My (according to the puzzle) code--

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 7 #defineLL Long Long8 Const intN =1000010;9 Const intMod =1000000007;Ten  One intN, t; A LL ans, mid; - LL Inv[n]; - LL Anss[n]; the BOOLSu2[n]; - intSu[n], su1[n]; -LL F[n];//F[i] Represents the least common multiple of 1~i -  +  - voidtable () + { Ainv[1] =1; at      for(inti =2; i < N; i++)//to find the inverse element of I -     { -Inv[i] = inv[mod%i]* (mod-mod/i)%Mod; -     } -  -memset (SU2,1,sizeof(SU2)); inmemset (SU1,0,sizeof(SU1)); -su2[0] = su2[1] =0;//the number of primes within n is calculated tosu2[2] =1; +      for(inti =3; i < N; i++) Su2[i] = i%2==0?0:1; -      for(inti =3; I <= sqrt (n1.0); i++) the     { *         if(Su2[i]) $         {Panax Notoginseng              for(intj = i*i; J <= N; J + =2*i) -             { theSU2[J] =0; +             } A         } the     } +     intK =0; -      for(inti =0; i < N; i++)//Number of prime tables within n $     { $         if(Su2[i] = =1) su[k++] =i; -     } -      for(inti =0; I < K; i++)//find the number that satisfies P^k, where p is prime and K is a positive integer the     { -LL mid =Su[i];Wuyi          while(Mid <N) the         { -Su1[mid] =Su[i]; WuMid *=Su[i]; -         } About     } $f[1] =1;//least common multiple table of 1~i within n -      for(inti =2; i < N; i++) -     { -         if(Su1[i]) f[i] = f[i-1]*Su1[i]; A         ElseF[i] = f[i-1]; +F[i]%=Mod; theanss[i-1] = (f[i]*inv[i])%mod;//Answer table -     } $ } the  the intMain () the { the     //freopen ("test.in", "R", stdin); -     //freopen ("Test.out", "R", stdout); in table (); thescanf"%d", &t); the      while(t--) About     { thescanf"%d", &n); theprintf"%i64d\n", Anss[n]); the     } +     return 0; -}
View Code

HDU 5407 CRB and candies (combined number + least common multiple + prime table + inverse) and 2015 Summer Multi-School games 10th

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.