Monday 23 November 2015

A. Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
Input
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.
Next line describe the second permutation in the same format.
Output
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
Sample test(s)
Input
3
3 2 1
1 2 3
Output
2
Input
5
1 2 3 4 5
1 5 2 3 4
Output
1
Input
5
1 5 2 3 4
1 2 3 4 5
Output
3
Note
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.
In the second sample, he removes number 5 and inserts it after 1.
In the third sample, the sequence of changes are like this:
  • 1 5 2 3 4
  • 1 4 5 2 3
  • 1 3 4 5 2
  • 1 2 3 4 5
So he needs three moves.
 
 
Solution 
 
If we replace the array the 1st array with the corresponding position in 2nd array then the question simply reduces to sorting the first array.
Suppose now we postpone the placement of every last element till we find its appropriate place to put. Therefore  let i be the largest index for which 1 to i is sorted so the answer is n-i.
Let us take an example and justify 
suppose we have p1= 1 2 5 4 3
                           p2= 4 2 1 3 5
the new array acccording to position is   
3 2 1 5 4 
so the longest increasing sequence is 3
Now suppose we have 2 1 54 in our hands and then we place each elements in their respective postion to sort the array.
 
code
#include<bits/stdc++.h>
using namespace std;
int a[200010];
int b[200010];
int h[200010];
int arr[200010];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
         cin>>a[i];
    }
    for(int i=0;i<n;i++)
    {
         cin>>b[i];
         h[b[i]]=i;
    }
    for(int i=0;i<n;i++)
    {
         arr[i]=h[a[i]];
    }
    int mx=-1;
    int cnt=1;
    for(int i=1;i<n;i++)
    {
          if(arr[i]>arr[i-1])
          cnt++;
          else
          break;
    }
    cout<<n-cnt<<endl;
    return 0;
}

Monday 16 November 2015

C. Beautiful Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits a and b. Vitaly calls a good number excellent, if the sum of its digits is a good number.
For example, let's say that Vitaly's favourite digits are 1 and 3, then number 12 isn't good and numbers 13 or 311 are. Also, number111 is excellent and number 11 isn't.
Now Vitaly is wondering, how many excellent numbers of length exactly n are there. As this number can be rather large, he asks you to count the remainder after dividing it by 1000000007 (109 + 7).
A number's length is the number of digits in its decimal representation without leading zeroes.
Input
The first line contains three integers: abn (1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).
Output
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
Sample test(s)
input
1 3 3
output
1
input
2 3 10
output
165





First we need to make few observations ,as the number of digits is maximum 10^6 so the maximum value of the sum can go upto 9*10^6.  So looping once through all through all possible sum values can be plausible. So for each sum we first check if that is constructed only with a and bs . If it is then let the number have i's number of a and (n-i)'s number of bs. Hence the sum can be represented as ai+(n-i)b=sum.
THerefore for all values satisfying the equaiton we can add n!/i!(n-i)! to the answer.
The factorials and inverse factorials can be precomputed.

solution


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
 
#define mod 1000000007

typedef long long int ll;
ll f[1000010];
ll inv[1000010];
ll powmod(ll a,ll n,ll MOD)
{
ll p=1;
for(;n;)
{
if(n%2) p=(p)*a%MOD;
if(n/=2) a=(a)*a%MOD;
}
return p;
}
void pre()
{
 f[0]=1;
  for(int i=1;i<=1000001;i++)
  {
    f[i]=(f[i-1]*i)%mod;
  }
  inv[0]=1;
  for(int i=1;i<=1000001;i++)
  {
         inv[i]=powmod(f[i],mod-2,mod);
  }
}
int a,b;
bool check(int val)
{
   while(val!=0)
   {
    int rem=val%10;
    val=val/10;
    if(rem==a || rem== b )
    continue;
    else 
    return 0;
}
return 1;
}
int main()
{
pre();
int n;
//cout<<f[6]<<" "<<endl;
// int a,b;
cin>>a>>b;
if(b>a)
swap(a,b);
cin>>n;
ll ans=0;
int mx=9*n;
for(int i=1;i<=mx;i++)
{
   if(check(i))
   {
//     cout<<"for i "<<i<<endl;
       int checkval=i-b*n;
       if(checkval<0)
       continue;
           int temp=a-b;
           if(checkval%temp==0)
           {
//             cout<<"here "<<endl;
              int numbera=checkval/temp;
              int numberb=n-numbera;
//               cout<<"numer a "<<numbera<<" number b "<<numberb<<endl;
              if(numbera>=0 && numberb>=0)
              {
//               cout<<"inverses "<<inv[numbera]<<" "<<inv[numberb]<<endl;
     ll res=(((f[n]*inv[numbera])%mod)*inv[numberb])%mod;
                 ans=(ans+res)%mod;
             }
}
          
}
}
cout<<ans<<endl;
return 0;
}