Showing posts with label Math. Show all posts
Showing posts with label Math. Show all posts

Sunday, June 24, 2012

[UVa] 11296 - Counting Solutions


Method:
First design a bruteforce solution with small input in mind. It should be able to compute answers for 100 101 etc. Now sequentially examine all the answers you'll find a pattern. The solution to every i-th even number is the sum of all numbers till i+1. It also applies to all odd numbers. Such as 2, 1st even number, so result is 1+2, till 2nd number And for 3, 1st odd number, it's the same. Just find the i using i = (n/2) and use i*(i+1)/2 formula for the result.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <vector>
#include <list>
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

typedef long long lint;

lint res[1000010];

int main ( void ) {
 
 lint i, j, k, n, cnt, ser;
 res[0] = 1LL;
 res[1] = 1LL;
  
 for (i=2, ser=2LL ; i<=1000001LL ; i+=2, ser++) {
  res[i] = (res[i-1]+ser);
  res[i+1] = res[i];
 }
 
 
 while ( cin >> n ) {
  cout << res[n] << endl;
 }

 return 0;
}

Tuesday, March 06, 2012

[UVa] 10940 - Throwing Cards Away II

This is a problem of case study and math. Create a program that simulates the process and find the pattern.

Inputs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Outputs:
1
2
2
4
2
4
6
8
2
4
6
8
10
12
14
16
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32

Once you find the pattern its straightforward.
1. Find the nearest 2^X such that 2^X <= N
2. If 2^X == N, output N
    Else output (N-2^X)*2

Tuesday, November 22, 2011

[UVa] 11614 - Etruscan Warriors Never Play Chess

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    long long n, lim, x1, x2;

    int tst;
    cin >> tst;

    while (tst--)
    {

        cin >> n;
        lim = (int)(ceil(sqrt(double(n*2))));

        x1 = lim * (lim+1) / 2;
        x2 = (lim-1)*lim/2;



        if (x1 <= n)
        {
            cout << lim << endl;
        } else if (x2<=n)
        {
            cout << lim-1 << endl;
        } else
        {
            cout << lim-2 << endl;
        }

    }
    return 0;
}

Thursday, November 10, 2011

[UVa] 12114 - Bachelor Arithmetic

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define ERROR 1e-11
using namespace std;
typedef double dt;

dt min(dt a, dt b)
{
    return (a<b?a:b);
}

int main()
{
    dt s, b, nx_pro, pr_pro;
    int kase=1;

    while (cin >> b >> s)
    {
        if (fabs(b-0.00)<ERROR && fabs(s-0.00)<ERROR) break;

        cout << "Case " << kase++ << ": ";
        pr_pro = min(s/b,(dt)1);
        if (b==1.00)
        {
            cout << ":-\\" << endl;
            continue;
        }
        nx_pro = min((s-(dt)1)/(b-(dt)1),(dt)1);

        if (nx_pro>pr_pro) cout << ":-)" << endl;
        else if (nx_pro<pr_pro) cout << ":-(" << endl;
        else cout << ":-|" << endl;
    }

    return 0;
}

[UVa] 568 - Just the Facts

#include <cstdio>
#include <iostream>
using namespace std;

int fact[20000];

int main()
{
    int i, f, idx;
    fact[0] = fact[1] = 1;
    f = 1;

    for (i=2 ; i<=10000 ; i++)
    {
        f*=i;
        while (f%10 == 0)
            f/=10;

        f = f%100000;
        fact[i] = f%10;
    }

    while (scanf("%d",&idx)==1)
    {
        printf("%5d -> %d\n",idx,fact[idx]);
    }

    return 0;
}

[UVa] 10161 - Ant on a Chess Board

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;

ll sum(ll n)
{
    return n*n;
}

int main()
{
    ll i, lev, lim, mid;

    while (cin >> i && i)
    {
        lev = (ll)ceil(sqrt(i));
        if (lev&1)
        {
            lim = lev*lev;
            mid = lim - (lev-1);
            if (i<mid)
            {
                cout << lev << " " << i-(lev-1)*(lev-1) << endl;
            } else
            {
                cout << lim-(i)+1 << " " << lev << endl;
            }
        } else
        {
            lim = lev*lev;
            mid = lim - (lev-1);
            if (i>=mid)
            {
                cout << lev << " " << lim-i+1 << endl;
            } else
            {
                cout << i-(lev-1)*(lev-1) << " " << lev << endl;
            }
        }
    }
    return 0;
}

Wednesday, October 26, 2011

[UVa] 160 - Factors & Factorials

#include <cstdio>
#include <iostream>
#define MP 100
using namespace std;
bool ver[300]={false};
int list[1000];
int siever() {
    int i, j, k=0;
    list[k++]=2;
    for (i=3 ; i<=200 ; i+=2)
    {
        if (ver[i]==false)
        {
            list[k++]=i;
            for (j=3 ; i*j<=200 ; j+=2)
            {
                ver[i*j]=true;
            }
        }
    }
    list[0]=2;
    return k;
}

int test(int x, int p)
{
    int i, counter=0, temp, rem=1, n=x;
    for (i=x ; i>1 ; i--)
    {
        n=i;
        while (n%p == 0)
        {

            counter++;
            n/=p;
        }
    }
    return counter;
}

int main() {
    int pl=siever();
    int i, n;
    while (cin >> n && n)
    {
        printf("%3d! =",n);
        for (i=0 ; i<pl && list[i]<=n ; i++)
        {
            printf("%3d",test(n,list[i]));
            if (i%14 == 0 && i>0 && list[i+1]<=n) printf("\n%6c",' ');
        }
        printf("\n");
    }
    return 0;
}

Sunday, October 23, 2011

[UVa] 10190 - Divide but not quite conquer

Critical point:
If n<2 or m<2, it's "Boring!"
If at some point (n%m != 0), it's "Boring!" to the limit :P
Method:
I store the result in a string (char arrray) when the division is taking place. If the sequence is found to be not boring, print the string.

#include #include <stdio.h>
#include <string.h>

char output[1000000], temp[1000000];
int addStr(char *des, int val, int len)
{

    if (!len)
        sprintf(des,"%d",val);
    else
    {
        int i, j;
        sprintf(temp," %d",val);
        int ln = strlen(temp);
        for (i=len, j=0 ; j<ln ; i++, j++)
        {
            des[i]=temp[j];
        }
        des[i]='\0';
        return (len+j);
    }
    return strlen(des);
}

int main()
{
    int n, m;
    while (scanf("%d %d",&n,&m)==2)
    {
        int lnt = 0;
        int b=0;
        if (n<2 || m<2)
        {
            printf("Boring!\n");
            continue;
        }
        while (n>1)
        {
            lnt=addStr(output,n,lnt);
            if (n%m != 0)
                b=1;
            n/=m;
        }
        lnt=addStr(output,n,lnt);


        if (b)
            printf("Boring!\n");
        else
            printf("%s\n",output);
    }
    return 0;
}

[UVa] 343 - What base is this?

I was too lazy when I saw this problem for the very first time 2 years ago. Solving problems that required parsing a string were too tedious for me back then. Well, guess what it generated a great code for future problems from this domain.

#include <stdio.h>
#include <math.h>
#include <string.h>
#define isChar(i) ((i>='A'&&i<='Z')||(i>='0'&&i<='9'))
typedef unsigned long long ULL;
int vals[1000];
char input[1000], val1[1000], val2[1000];
ULL getVal(char *a, int base, int ln)
{
    int len = ln, i;
    ULL val=0;
    for (i=len-1 ; i>=0 ; i--)
    {
        val = val + (vals[a[i]]) * (int)pow((double)base,(len-1-i));
    }
    return val;
}

int setVals()
{
    int i;
    for (i=0 ; i<=9 ; i++)
    {
        vals[i+'0'] = i;
    }
    for (i='A' ; i<='Z' ; i++)
    {
        vals[i] = (i-55);
    }
}

int parser(char *a, int *l1, int *l2, int *mb1, int *mb2)
{
    int i, j, min;
    for (i=0 ; !isChar(a[i]) ; i++);
    min=-1;
    for (j=0 ; isChar(a[i]) ; i++, j++)
    {
        val1[j]=a[i];
        if (vals[a[i]]>min)
            min = vals[a[i]];
    }
    val1[j]='\0';
    *l1=j;
    *mb1=min+1;

    for ( ; !isChar(a[i]) ; i++);
    min=-1;
    for (j=0 ; isChar(a[i]) ; i++, j++)
    {
        val2[j]=a[i];
        if (vals[a[i]]>min)
            min = vals[a[i]];
    }
    val2[j]='\0';
    *l2=j;
    *mb2=min+1;
}

int main()
{
    int l1, l2, minb1, minb2, f, i, j;
    setVals();

    while (gets(input))
    {
        parser(input,&l1,&l2,&minb1,&minb2);

        f=0;

        for (i=(minb1>1?minb1:2) ; i<=36 && !f ; i++)
        {
            for (j=(minb2>1?minb2:2) ; j<=36 && !f ; j++)
            {
                if (getVal(val1,i,l1) == getVal(val2,j,l2))
                {
                    printf("%s (base %d) = %s (base %d)\n",val1,i,val2,j);
                    f=1;
                }
            }
        }

        if (!f) printf("%s is not equal to %s in any base 2..36\n",val1,val2);

    }
    return 0;
}

Saturday, October 22, 2011

[UVa] 10432 - Polygon inside a circle

#include <stdio.h>
#include <math.h>
#define PI (2*acos(0))
int main()
{
    double area, r, n;
    while (scanf("%lf %lf",&r,&n)==2)
    {
        area = r*r*sin(2*PI/n)/2.0;
        printf("%.3lf\n",area*n);
    }
    return 0;
}

[UVa] 374 - Big Mod

Used a recursive method from Art of Programming Contest
LL bigMod(LL b, LL p, LL m)
{
    if (!p) return 1;
    else if (!(p&0x01)) return power(bigMod(b,p/2,m),2)%m;
    else return (b%m * bigMod(b,p-1,m))%m;
}

#include <stdio.h>
#define LL long
LL power(LL a, LL p)
{
    int ret=1;
    while (p--)
        ret*=a;
    return ret;
}
LL bigMod(LL b, LL p, LL m)
{
    if (!p) return 1;
    else if (!(p&0x01)) return power(bigMod(b,p/2,m),2)%m;
    else return (b%m * bigMod(b,p-1,m))%m;
}

int main()
{
    LL b, p, m;
    while (scanf("%ld %ld %ld",&b,&p,&m)==3)
    {
       printf("%ld\n",bigMod(b,p,m));
    }
    return 0;
}

Friday, October 21, 2011

[UVa] 11650 - Mirror Clock

Nice and fun! The only tricky case probably is a time that generated 0 hours, just make it 12 but keep the minute same. I don't know if handling for 24 hours time is needed but added that too.
#include <stdio.h>

int main()
{
    int tst, h, m, time;
    scanf("%d",&tst);
    while (tst--)
    {
        scanf("%d:%d",&h,&m);
        time = h*60 + m;
        if (time>720) time-=720;
        time = 720-time;

        h = (time/60);
        m = time%60;

        if (!h) h = 12;

        if (h<10)
            printf("0%d:",h);
        else
            printf("%d:",h);
        if (m<10)
            printf("0%d\n",m);
        else
            printf("%d\n",m);
    }
    return 0;
}

Monday, October 17, 2011

[UVa] 278 - Chess

Analyze all 4 cases and soon you'll equations for all of them. The given advantage is the lower limit of 4. Below that the derived theorems fail.

#include <stdio.h>
#include <string.h>
#define min(a,b) (a<b?a:b)
char inp[100];
int main()
{
    int test, x, y;
    char a;
    scanf("%d",&test);
    getchar();
    while (test--)
    {
        gets(inp);


        sscanf(inp,"%c %d %d",&a,&x,&y);
        if (a=='k')
        {
            printf("%d\n",(x*y+1)/2);
        } else if (a=='K')
        {
            printf("%d\n",((x+1)/2) * ((y+1)/2));
        } else if (a=='Q'||a=='r')
        {
            printf("%d\n",min(x,y));
        }
    }
    return 0;
}

Friday, September 09, 2011

Important links

Mathematics:
Series and Sums

Programming help [Algorithms, Codes, Links]:
Mehadi Bhai's Blog
Shafaet's Blog
Fahim Bhai's Site
Fahim Bhai's Page

Online Judge [Contest, Problems, Virtual Contests]
TopCoder
Codeforces
Codechef

[UVa] 10642 - Can you solve it?

Analysis:
Draw the coordinate system but start from 1 not 0, just to escape negative value problems. Draw it like a triangle where the top has 1. Now see that on the right side, every i-th number is the sum of all numbers till i.
1 = 1x(1+1)/2
3 = 2x(2+1)/2
6 = 3x(3+1)/2
10 = 4x(4+1)/2
15 = 5x(5+1)/2
So, given the x coordinate you can find the first number in that row using X*(X+1)/2, BUT make X=X+1 first.
To find the Y-th element (in my case (Y+1)-th, you can use the formula
S = a + (a+d) + (a+2d) + ... + [a + (n-1)d] = n[2a + (n-1)d]/2
But you have to modify it a bit first. This is derived from the analysis that every number in a row has some initial numbers lost except the first row. In X=2, 2 is not in the series, in X=3, 3 and 4 are not in the series. This keeps increasing by 1 at every i. So the series is
Y[0], Y[0]+1x1, Y[0]+1x2, Y[0]+1x3 ......
So you can simply use the formula with a=0 and n=Y+(X-1). Just add X to that.
Find both the numbers this way and print their difference. Done!!!.


A useful link for Summation formulas

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <vector>
#include <climits>
#include <set>
#include <algorithm>
#define MI INT_MAX
#define ULONG unsigned long long
#define LLONG long long
#define swap(a,b) {int t=a ; a=b ; b=t; }
#define sz(a) sizeof(a)
#define FOR(i, a, b) for (i=a ; i<b ; i++)
#define QSORT(a,n,s,f) qsort(a,n,sizeof(s),f)
using namespace std;
int sum(int a, int n)
{
    return n*(2*a+(n-1)*1)/2;
}
int main()
{
    int test, kase=1, x1, x2, y1, y2, num1, num2, temp, n, a;
    scanf("%d",&test);
    while (test--)
    {
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        x1++; x2++; y1++; y2++;


        num1 = x1+(sum(0,y1+(x1-1)));
        num2 = x2+(sum(0,y2+(x2-1)));


        printf("Case %d: %d\n",kase++,num2-num1);
    }
    return 0;
}

Sunday, October 17, 2010

Finding the number of Divisors of a Number

You can say that I've hacked through high school system somehow so I didn't know this before. Today, I was trying to solve a problem that I faced in my last day's DIUPC 2010. While solving the problem I found some interesting properties of number that of course kids play for most. Anyways. The facts that I found are as follows:

1. To find if a number is dividable by other number (of course less that it's half) you just need to check till its' square root. Say if the number is N then check till sqrt(N). That because if the number is dividable by numbers bigger than that then you get the same numbers that you used previously to divide it. Just as this. Say we are testing with 9.
Limit = sqrt(9) = 3.
9/1 = 9
9/3 = 3
9/9 = 1 we used that before. 9 is not a good example so let's use 16.
Limit = sqrt(16) = 4.
16/1 = 1
16/2 = 8
16/4 = 4
16/8 = 2 used.
16/16 = 1 used.
Test with other number to find that I've been a stupid all the way till this day.

2. It's not something that I found today but should be noted here. A number is a combination of several primes. Like this:
1x2 = 2
1x2x3 = 6
1x2x3x4[2x2] = 24
No, 1 is not among primes.

3. If a number N is square then it has equal number of factors below and beyond sqrt(N) + the sqrt(N) itself. Check for 4. sqrt(4) = 2. The divisors of 4 are 1, 2, 4. Check for 16. sqrt(16) = 4. The divisors of 16 are 1, 2, 4, 8, 16. But if the number is not square then It doesn't have the same property. Take 15, the divisors of 15 are 1, 3, 5, 15. Below sqrt(15) are 1, 3 and including and above it are 5, 15. It should've had another number to have the same property.

4. If you keep on dividing a number by it's prime factors (till sqrt(N)) and that number is not a square the at the end there will be a prime left out. Such as 6. Divide by 2. Left 3, But next prime 3 is more squared. So left is 3. Take 20, keep dividing by 2.
20/2 = 10
10/2 = 5
End 'cause 5*5=25>5.
So left is 5.
Take 65.
65/3 = 13
End 'cause it's a prime.

I used this program to find these properties which I did during solving a divisor based problem:

int nod(int n) {

int i, j, c, count=1, temp;

for (j=0, c=0 ; primes[j]*primes[j]<=n ; j++) {
c=1;
temp=primes[j];
while (n%temp==0) {
n/=temp;
c++;
}
count*=c;
}

if (n==1) { /*putchar('x');*/ return count; }
else return count*2;
}


This can be easily modified to show all the primes. Check the sections where I multiply the primes and the sections where I decide what to give as output.

Monday, August 23, 2010

Mod on Sequences

While solving a problem at one of the Programming Practice sites, I came to know this about sum of a sequence. I wonder how much lacking do I have in Maths.

If, for example, 1+2+3+.......+(n-1)+n is the sequence that you want the sum of then, what we do is
add all the numbers from 1 to n. But this has another method. Whenever the sum crosses n during the the summation process you can take it as sum%n.

Which means, (1+2+3+.........+n)%n = (sum+1)%n + (sum+2)%n + ...... + ( sum+n)%n.

I didn't get it from any book so not sure if it's wrong or right. If someone finds it a mistake please comment.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...