-3

Question is this

Nancy hates any and every string that contains the number "13". Clouseau wants to gift her a string and is looking over his options, ofcourse he would never pick a string that has "13" as a substring.

Tell Clouseau the total number of such strings s that are made of exactly N characters. The strings may contain any integer from 0-9, repeated any number of times.

Input : The first line of input file contains a number T indicating number of test cases. The following T lines, each contain an integer N.

Output : The output file should contain answer to each query in a new line modulo 1000000009.

Constraints : 1 T 100000 , 0 N 1000000009

I am not getting the logic correct.

# include <stdio.h>
# define MOD 1000000009

unsigned long long mod_pow(unsigned long long num, unsigned long long pow, unsigned long long mod)
{
    unsigned long long test;
    unsigned long long n = num;
    for(test = 1; pow; pow >>= 1) {

        if (pow & 1)
            test = ((test % mod) * (n % mod)) % mod;
        n = ((n % mod) * (n % mod)) % mod;
    }

    return test;
}

int main(int argc, char* argv[])
{

    long t;

    unsigned long long total_no, bad_no, n;

    scanf ("%ld", &t);

    while (t--) {
    
        scanf ("%lld", &n);

        if (n != 1) {
      
            total_no = (10 * (mod_pow (10, n-1, MOD))) % MOD;
    
            bad_no = ((n - 1) * (mod_pow(10, n-2, MOD))) % MOD;

            printf ("%lld\n", (((total_no - bad_no) % MOD)));
        }
        else
            printf ("10\n");

    }
    return 0;
}
Community
  • 1
  • 1
  • Use a debugger to step through the code, line by line, while watching the values of the involved variables. That should hopefully help you understand what's happening. – Some programmer dude Oct 20 '15 at 06:12
  • Can you tell me whether my logic is correct or not ? – David Lowe Oct 20 '15 at 06:24
  • I just gave you a tip about how you could figure it out yourself. Do that first. Even if you still can't figure it out, then it should hopefully give enough hints about the problem that you could include in your question. – Some programmer dude Oct 20 '15 at 06:26
  • 2
    Gift her a string? Seriously, who writes these tasks? "Nancy wanted to write fiction, but they told her she was terrible at it, so she ended up writing programming books instead." – Lundin Oct 20 '15 at 06:30
  • Programmers who don't want to learn programming.... – toesslab Oct 20 '15 at 06:40

1 Answers1

0

You can use this function:

long long int  bigmod ( long long a, long long p, long long m )
{
    if ( p == 0 )return 1; // If power is 0, then a ^ 0 = 1 for any value of a, And 1 Mod m=1 for any value of m, So return 1

    if ( p % 2 ) // If power is odd, Split it : a ^ 5 =( a )* (a ^ 4) --> left and right child respectively.
    {
        return ( ( a % m ) * ( bigmod ( a, p - 1, m ) ) ) % m; 
    }
    else //If power is even then split it equally and return the result...
    {
        long long c = bigmod ( a, p / 2, m ); 
        return ( (c%m) * (c%m) ) % m;
    }
}

and call this function like this:

int main(){

     // take input....
    //Calling...
    printf("result is %lld\n",bigmod(a,p,MOD)); // 
   return 0;   
}
Newaz Hossain
  • 157
  • 1
  • 4