Thursday, 30 April 2015

UVA 10006 Carmichael Numbers

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

bool status[65010];
void primeGenerator(int n)
{
    int sq=sqrt(n);
    status[0]=status[1]=true;
    for(int i=3;i<=sq;i+=2)
        if(!status[i])
            for(int j=i*i;j<=n;j+=i)
                status[j]=true;
return;
}

long long bigMod(long long base, long long power, long long mod)
{
    long long ret=1;
    while(power)
    {
        if(power & 1)
            ret=(ret*base)%mod;
        base = (base*base)%mod;
        power=power>>1;
    }
    return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    long long n, tc, kk=1;
    string s;
    primeGenerator(65005);
    while(cin>>n && n)
    {
        bool chk=true;
        if(!status[n])
        {
            cout<<n<<" is normal.\n";
            continue;
        }
        for(long long i=2;i<n;i++)
                if(bigMod(i, n, n)!=i)
                {
                    chk=false;
                    break;
                }
        if(chk) cout<<"The number "<<n<<" is a Carmichael number.\n";
        else cout<<n<<" is normal.\n";
    }
return 0;
}

Wednesday, 29 April 2015

UVA 11195 Another n-Queen Problem

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

string board[16];
int n, cnt;

void nq(int row, int col, int lft, int rgt)
{
    if(row==n)
    {
        cnt++;
        return;
    }

    int all = col | lft | rgt;

    for(int i=0;i<n;i++)
    {
        if(!((1<<i) & all) && board[row][i]=='.')
            nq(row+1, col | (1<<i), (lft | (1<<i))<<1, (rgt | (1<<i))>>1 );
    }
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1;
    string s;
    while(cin>>n && n)
    {
        for(int i=0;i<n;i++)
            cin>>board[i];
        cnt=0;
        nq(0, 0, 0, 0);
        cout<<"Case "<<kk++<<": "<<cnt<<"\n";
    }
return 0;
}

Tuesday, 28 April 2015

UVA 10309 Turn the Lights Off

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

string tmp[12];

int lightSwitch(int n)
{
    int cnt=0;
    for(int i=0;i<10;i++) //bit check for first row
        if(n & (1<<i))
        {
            cnt++;
            //top-left
            for(int j=max(i-1, 0);j<=min(i+1, 9);j++)
            {
                if(tmp[0][j]=='#') tmp[0][j]='O';
                else tmp[0][j]='#';
            }
            //2nd row
            if(tmp[1][i]=='#') tmp[1][i]='O';
            else tmp[1][i]='#';
        }
    //cout<<n<<" "<<cnt<<endl;
    for(int i=1;i<10;i++)
    {
        for(int j=0;j<10;j++)
            if(tmp[i-1][j]=='O')
            {
                cnt++;
                tmp[i-1][j]=='#'; //above

                for(int k=max(j-1, 0);k<=min(j+1, 9);k++)
                {
                    if(tmp[i][k]=='#') tmp[i][k]='O';
                    else tmp[i][k]='#';
                }

                if(i<9)
                {
                    if(tmp[i+1][j]=='#') tmp[i+1][j]='O';
                    else tmp[i+1][j]='#';
                }
            }
    }
    bool chk=true;
    for(int i=0;i<10;i++)
        if(tmp[9][i]=='O')
            chk=false;
    if(!chk) cnt=1e9;
return cnt;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n, tc, kk=1;
    string s, ss[12];
    while(cin>>s && s!="end")
    {
        for(int i=0;i<10;i++)
            cin>>ss[i];
        int mn=1e9;
        for(int i=0;i<1024;i++)
        {
            for(int j=0;j<10;j++)
                tmp[j]=ss[j];
            int mnn=lightSwitch(i);
            //if(mnn==0) cout<<i<<endl;
            mn=min(mn, mnn);

        }

        if(mn>100) mn=-1;
        cout<<s<<" "<<mn<<"\n";
    }
return 0;
}

Tuesday, 21 April 2015

UVA - 623 500!

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  int n;
  while(sc.hasNext())
  {
   n=sc.nextInt();
   BigInteger B = BigInteger.valueOf(1);
   for(int i=n;i>1;i--)
    B = B.multiply(BigInteger.valueOf(i));
   System.out.println(n+"!");
   System.out.println(B);
  }
 }
}