Project Euler #172: Investigating numbers with few repeated digits

Sort by

recency

|

32 Discussions

|

  • + 0 comments

    This problem can be solved by finding recurrence relation using exponential generating functions. Becuase we calculate mod 10^9 + 7 we must find modulo inverses for factorials. The recurrence relation can also be found using pure combinatorics.

  • + 1 comment

    my code work perfectly if k below 6... if the k is 7 and above, my C# code will not showing any result...i think is the max size of the data type problem...

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    class Solution {
        static void Main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
            
            int[] m_t = Array.ConvertAll(System.Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp));
            int numDigitRepeated = m_t[0];
            // numDigitRepeated--;
            int numInput = m_t[1];
            
            // System.Console.WriteLine(numDigitRepeated+" "+numInput);
            
            
            int numDigit = -1;
            for(int i=0;i<numInput;i++){
                numDigit = Convert.ToInt32(Console.ReadLine());
                // System.Console.WriteLine(numDigit);
                //numDigit++;
                int finalNumOfDigit = getCountNumOfDigit(numDigit);
                
                int temp = getCountNumOfDigit(numDigit-1);
                
                finalNumOfDigit = finalNumOfDigit - temp;//eg if 3 digit, range(100-999) = range(1-999) - range(1-10)
                //System.Console.WriteLine("finalNumOfDigit:"+finalNumOfDigit);
                
                if(numDigit > numDigitRepeated){
                    //3 > 1
                    updateRepeatedCountNumOfDigit(ref finalNumOfDigit, numDigitRepeated, numDigit);
                }
                System.Console.WriteLine("finalNumOfDigit:"+finalNumOfDigit);
            }
            
            
            
        }
        public static int getCountNumOfDigit(int n){
            String initDigit="";
            for(int w=0;w<n;w++) initDigit += "0";
    
            initDigit="1"+initDigit;
            int finalNumOfDigit = Int32.Parse(initDigit) - 1;
            
            return finalNumOfDigit;
        }
        
        public static void updateRepeatedCountNumOfDigit(ref int finalNumOfDigit, int digitRepeat, int totalNumDigit){
            digitRepeat++;
            
            
           // System.Console.WriteLine("digitRepeat:"+digitRepeat+",totalNumDigit:"+totalNumDigit+",aaaaaaaaa");
            int allNumOfDigit = getCountNumOfDigit(totalNumDigit);
    
    
            String initDigit="";
            for(int w=1;w<digitRepeat;w++) initDigit += "0";
            initDigit="1"+initDigit;
            int startDigit = Int32.Parse(initDigit);
    
            //System.Console.WriteLine("startDigit:"+startDigit+",allNumOfDigit:"+allNumOfDigit);
            for(int q=startDigit;q<=allNumOfDigit;q++){
    
                //if( Math.Floor(Math.Log10(q) + 1) >= digitRepeat ){
    
                    if(checkRepeatedNumber(q, digitRepeat)){
    
                        finalNumOfDigit--;
                    }
                //}
    
            }
                
            
            
        }
        public static bool checkRepeatedNumber(int q, int digitRepeat){
            
            String sss = q.ToString();
            // if(q==1000 ||q==2000|| q==3000 || q==4000|| q==5000|| q==6000|| q==7000|| q==8000 || q==9000)
            //     System.Console.WriteLine("sss:"+sss);
            bool isRepeated = false;
            
            //int[] arr1 = Enumerable.Range(1, 9).ToArray();
            
            
            char[] qqq={'1','2','3','4','5','6','7','8','9'};
            foreach(char qq in qqq){
                int countR = countFrom(sss,qq,0);//Convert.ToChar(1)
                //if(q==1111 ||q==2000|| q==2111) System.Console.WriteLine("countR:"+countR+",digitRepeat:"+digitRepeat);
                if(countR >= digitRepeat){
                    //System.Console.WriteLine("adddddddddd"+sss);
                    isRepeated = true;
                }
            }
            
            return isRepeated;
        }
        
        public static int countFrom(String str, char ch, int start) {
            int count = 0;
            for (int i = start; i < str.Length; i++) {
                int tt = str.IndexOf(ch,i,1);
                if(tt!=-1){
                    count++;
                }
            }
            return count;
        }
    }
    
    • + 0 comments

      Based on the code you provided, it seems that the issue you're facing is related to performance rather than a limitation of data types. As the value of k increases, the code becomes slower and may not produce any result for larger values of k.

      The main performance bottleneck in your code is the updateRepeatedCountNumOfDigit method. This method has a nested loop that iterates over a large range of numbers. As k increases, the range of numbers and the number of iterations also increase exponentially, causing the code to slow down significantly.

      To optimize your code and improve its performance, you can try the following suggestions:

      Analyze the problem: Understand the problem requirements and constraints thoroughly. Look for any patterns or mathematical formulas that can help you solve the problem more efficiently.

      Avoid unnecessary calculations: The getCountNumOfDigit method seems to be computing the count of numbers with a certain number of digits. Instead of recomputing this value for each number, you can precompute and store these values in an array or a dictionary for efficient lookup.

      Optimize the nested loops: The nested loop in the updateRepeatedCountNumOfDigit method is the main performance bottleneck. Try to find a way to optimize or eliminate this loop if possible. Consider using a mathematical approach or a more efficient algorithm to solve the problem.

      Use efficient data structures: Depending on the problem requirements, you may be able to use more efficient data structures such as sets, arrays, or bit manipulation techniques to solve the problem more efficiently.

      Test with smaller inputs: Before running your code with larger values of k, test it with smaller inputs to verify its correctness and ensure it produces the expected results. This will help you identify any issues or bugs in your code early on.

      Remember that performance optimization is a complex topic, and the specific optimizations required for your code may depend on the problem you're trying to solve. It's important to analyze the problem, understand the requirements, and apply appropriate techniques to improve the efficiency of your code.

  • + 1 comment
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int main()
    {
    	int m,tot;
    	int k;
    	cin>>m;
    	cin>>tot;
    
     
     long low;
     long high;
     for(long i=0;i<tot;i++){
    cin>>k; 
     low = pow(10,(k-1));
    high = pow(10,k) - 1;
    long temp;
    temp = (k-m) * 9;
    long tot = high-low+1;
    tot = tot - temp;
    cout<<tot%1000000007<<endl;	
    }
    	
    	return 0;
    }
    

    Can I know the mistake?

    • + 0 comments

      In the code you provided, the mistake lies in reusing the variable name tot for two different purposes within the same scope. This leads to incorrect results in the output.

      The mistake was in using tot as both the total number of inputs and the count of digits to be subtracted (totalCount). By reusing the same variable, the value of tot was being overwritten, leading to incorrect calculations. I've renamed the variable that holds the total count of digits to totalCount to avoid this issue.

  • + 1 comment

    My code is not submitting. Its show Terminated due to timeout..... only Testcase0 is submitted .

    • + 0 comments

      If your code is timing out and only successfully processing the first test case, it suggests that the solution is not efficient enough to handle larger inputs within the given time limit. To address this issue, you need to optimize your code to improve its performance. Here are a few suggestions:

      Avoid unnecessary calculations: Review your code to ensure that you're not performing any redundant or unnecessary calculations. Simplify the logic and eliminate any unnecessary computations.

      Precompute values: If there are any repetitive calculations or values that can be precomputed, do so outside the loop to avoid recomputation in each iteration.

      Analyze the problem constraints: Understand the constraints of the problem and look for any patterns or formulas that can help optimize the solution. There might be a mathematical or algorithmic approach to solve the problem more efficiently.

      Optimize loops: If there are nested loops or loops with a large number of iterations, consider ways to optimize or eliminate them. Look for alternative algorithms or mathematical techniques that can solve the problem more efficiently.

      Use efficient data structures: Evaluate the data structures used in your code. Consider if there are more efficient alternatives that can improve performance. For example, using arrays instead of vectors or optimizing memory usage.

      Test with larger inputs: Ensure that your code works correctly with larger inputs by testing it against such cases. This will help identify any potential issues or bugs that might not be apparent with smaller inputs.

      Remember, optimizing code for efficiency can be a complex task, and the specific optimizations required depend on the problem at hand. Take a careful look at your code, analyze the problem constraints, and apply appropriate techniques to improve its performance.

  • + 4 comments

    when will contest end :p