Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:02:26+00:00 2026-06-01T15:02:26+00:00

I have a code which reads around (10^5) int(s) from stdin and then after

  • 0

I have a code which reads around (10^5) int(s) from stdin and then after performing ## i output them on stdout. I have taken care of the INPUT part by using “setvbuf” & reading lines using “fgets_unlocked()” and then parsing them to get the required int(s).
I have 2 issues which i am not able to come over with:

1.) As i am printing int(s) 5 million on stdout its taking lot of time : IS THERE ANY WAY TO REDUCE THIS( i tried using fwrite() but the o/p prints unprintable characters due to the reason using fread to read into int buffer)

2.) After parsing the input for the int(s) say ‘x’ i actually find the no of divisors by doing %(mod) for the no in a loop.(See in the code below): Maybe this is also a reason for my code being times out:
Any suggestions on this to improved.
Many thanks
This is actually a problem from http://www.codechef.com/problems/PD13

# include <stdio.h>
# define SIZE 32*1024
char buf[SIZE];

main(void)
{
int i=0,chk =0;
unsigned int j =0 ,div =0;
int a =0,num =0;
char ch;

setvbuf(stdin,(char*)NULL,_IOFBF,0);

scanf("%d",&chk);
while(getchar_unlocked() != '\n');
while((a = fread_unlocked(buf,1,SIZE,stdin)) >0)
{
    for(i=0;i<a;i++)
    {
        if(buf[i] != '\n')
        {
            num = (buf[i] - '0')+(10*num);
        }
        else
        if(buf[i] == '\n')
        {   
            div = 1;
            for(j=2;j<=(num/2);j++)
            {
                if((num%j) == 0)    // Prob 2
                {
                    div +=j;
                }
            }
            num = 0;
            printf("%d\n",div); // problem 1
        }       
    }
}
return 0;
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:02:28+00:00Added an answer on June 1, 2026 at 3:02 pm

    //Prob 2 Is your biggesr issue right now…. You just want to find the number of divisors?

    My first suggestion will be to cache your result to some degree… but this requires potentially twice the amount of storage you have at the beginning :/.

    What you can do is generate a list of prime numbers before hand (using the sieve algorithm). It will be ideal to know the biggest number N in your list and generate all primes till his square root. Now for each number in your list, you want to find his representation as product of factors, ie

    n = a1^p1 * a1^p2 *... *an^pn
    

    Then the sum of divisors will be.

    ((a1^(p1+1) - 1)/(a1 - 1))*((a2^(p2+1) - 1)/(a2-1))*...*((an^(pn+1) - 1)/(an-1))
    

    To understand you have (for n = 8) 1+ 2 + 4 + 8 = 15 = (16 - 1)/(2 - 1)

    It will drastically improve the speed but integer factorization (what you are really doing) is really costly…

    Edit:

    In your link the maximum is 5000000 so you have at most 700 primes

    Simple decomposition algorithm

    void primedecomp(int number, const int* primetable, int* primecount,
          int pos,int tablelen){
        while(pos < tablelen && number % primetable[pos] !=0 )
           pos++;
    
        if(pos == tablelen)
          return
    
         while(number % primetable[pos] ==0 ){
            number = number / primetable[pos];
            primecount[pos]++;
         }
         //number has been modified
         //too lazy to write a loop, so recursive call
         primedecomp(number,primetable,primecount, pos+1,tablelen);
        
    }
    

    EDIT : rather than counting, compute a^(n+1) using primepow = a; primepow = a*primepow;

    It will be much cleaner in C++ or java where you have hashmap. At the end
    primecount contains the pi values I was talking about above.

    Even if it looks scary, you will create the primetable only once. Now this algorithm
    run in worst case in O(tablelen) which is O(square root(Nmax)). your initial
    loop ran in O(Nmax).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this code which reads the first field in a database record
I have the following code which reads in the follow file, append a \r\n
we have a C++ class which basically reads and writes vectors from a binary
I have a php script which accesses a MSSQL2005 database, reads some data from
I have code which needs to do something like this There is a list
I have code which looks like: private static DirectiveNode CreateInstance(Type nodeType, DirectiveInfo info) {
I have code which as been working against an older Active Directory server and
I have code which performs query on Jboss server. It has JNDI based datasource
I have code which works in all sorts of different situations, including when copying
I already have code which lazy loads scripts on request. My issue now is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.