Tuesday, October 25, 2016

UVA 136 - Ugly Numbers

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX INT_MAX
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    puts("The 1500'th ugly number is 859963392.");

 return 0;
}

UVA 424 - Integer Inquiry

import java.util.*;
import java.math.BigInteger;

public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger n , sum = BigInteger.valueOf(0);
while(sc.hasNext()){
n = sc.nextBigInteger();
sum = sum.add(n);
}
System.out.println(sum);
}

UVA 10140 - Prime Distance

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX INT_MAX
#define maxL (1000000>>5)+1
#define GET(x) (mark[x>>5]>>(x&31)&1)
#define SET(x) (mark[x>>5]|=1<<(x&31))
using namespace std;
int mark[maxL];
int p[700000], pt = 0;
void sieve() {
    register int i, j, k;
    SET(1);
    int n = 1000000;
    for(i = 2; i <= n; i++) {
        if(!GET(i)) {
            p[pt++] = i;
            for(k = n/i, j = i*k; k >= i; k--, j -= i)
                SET(j);
        }
    }
}
int isprime(int n) {
    if(n < 1000000)
        return !GET(n);
    static int sq, i;
    sq = (int)sqrt(n);
    for(i = 0; i < pt && p[i] <= sq; i++)
        if(n%p[i] == 0)
            return 0;
    return 1;
}
int main()
{
   ios_base::sync_with_stdio(false);
   sieve();
   int L , U;
   while(cin >> L >> U){
    int last = -1 ,x1,x2,x3,x4;
    int close = INT_MAX, dis = -close;
    bool used = false;
    for (LL i = L; i <= U; ++i)
    {
        if(isprime(i)){
            if(last == -1)last = i;
            else{
                if(i-last > dis){
                    dis = i-last;
                    x1 = last , x2 = i;
                }
                if(i-last < close){
                    close = i-last;
                    x3 = last, x4 = i;
                }
                last=i;
                used=true;
            }
        }
    }
    if(!used)cout <<"There are no adjacent primes.\n";
    else cout <<x3<<','<<x4<<" are closest, "<<x1<<','<<x2<<" are most distant.\n";
   }

 return 0;
}

Monday, October 24, 2016

UVA 10394 - Twin Primes

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX 20000010
using namespace std;
bool prime[MAX];
vector<int> P;
vector<int>TwinPrime;
void init_prime() {
  prime[2] = true;
  for (int i = 3; i < MAX; i += 2) prime[i] = true;

  for (int i = 3; i*i < MAX; i += 2)
    if (prime[i])
      for (int j = i*i; j < MAX; j += i+i)
        prime[j] = false;

  REP(i, MAX)if (prime[i]) P.push_back(i);
  for (int i = 0; i <P.size(); i++)
  {
      if(P[i+1]-P[i] == 2)TwinPrime.push_back(P[i]);
  }

}

int main()
{
   ios_base::sync_with_stdio(false);
   init_prime();
   int n;
  while(cin >> n){
    cout <<'('<<TwinPrime[n-1]<<", "<<TwinPrime[n-1]+2<<")\n";
   }
 return 0;
}

UVA 686 - Goldbach's Conjecture (II)

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX INT_MAX
using namespace std;
bool isPrime(int n)
{
    if(n == 1) return false;
    if(n == 2) return true;
    if(n%2==0) return false;
    int sqr = sqrt(n);

    for (int i = 3; i <= sqr; i+=2)
    {
        if(n%i == 0){

            return false;
        }
    }
    return true;
}

int main()
{
   ios_base::sync_with_stdio(false);
   int n;
   while(cin >> n && n){
    int cnt = 0;
    for(int i = n-1; i>=n/2; i--){
        if(isPrime(i)){
            int diff = n-i;
            if(isPrime(diff)){
                cnt++;
            }
        }
    }
    cout << cnt << '\n';
   }
 return 0;
}

Sunday, October 23, 2016

UVA 543 - Goldbach's Conjecture

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX INT_MAX
using namespace std;
bool isPrime(int n)
{
    if(n == 1) return false;
    if(n == 2) return true;
    if(n%2==0) return false;
    int sqr = sqrt(n);

    for (int i = 3; i <= sqr; i+=2)
    {
        if(n%i == 0){

            return false;
        }
    }
    return true;
}

int main()
{
   ios_base::sync_with_stdio(false);
   int n;
   while(cin >> n && n){
    int a=0 , b=0;
    for(int i = n-1; i>=n/2; i--){
        if(isPrime(i)){
            int diff = n-i;
            if(isPrime(diff)){
                a=min(i,diff);
                b=max(i,diff);
                break;
            }
        }
    }
    if(a && b)cout<<n << " = "<< a << " + " << b <<'\n';
    else cout << "Goldbach's conjecture is wrong.\n";
   }
 return 0;
}

UVA 11287 - Pseudoprime Numbers

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(int i = 0; i<MAX; i++)
#define MAX 10001
using namespace std;
bool isPrime(LL n)
{
    if(n == 1) return false;
    if(n == 2) return true;
    LL sqr = sqrt(n);

    for (LL i = 2; i <= sqr; i++)
    {
        if(n%i == 0){

            return false;
        }
    }
    return true;
}
LL big_mod(LL base, LL power, LL mod)
{
    if(power==0)    return 1;
    else if(power%2==1)
    {
        LL p1 = base % mod;
        LL p2 = (big_mod(base,power-1,mod))%mod;
        return (p1*p2)%mod;
    }
    else if(power%2==0)
    {
        LL p1 = (big_mod(base,power/2,mod))%mod;
        return (p1*p1)%mod;
    }
}
int main()
{
   ios_base::sync_with_stdio(false);
  LL p   , a;
  while(cin >> p >> a &&p &&a){
  if(isPrime(p))cout << "no\n";
  else {
    LL mod = big_mod(a,p,p);
    if(a == mod)cout << "yes\n";
    else cout << "no\n";
  }
}

 return 0;
}

UVA 1210 - Sum of Consecutive Prime Numbers

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
using namespace std;
vector<int>prime;
bool isPrime(int n)
{
    if(n == 1) return false;
    if(n == 2) return true;
    int sqr = sqrt(n);

    for (int i = 2; i <= sqr; i++)
    {
        if(n%i == 0){

            return false;
        }
    }
    return true;
}
void primeGenerate(int n)
{
    if(isPrime(n))prime.push_back(n);
    if(n > 10000)return;
    n+=1;
    primeGenerate(n);
}
int main()
{
   ios_base::sync_with_stdio(false);
   primeGenerate(1);
   int cnt=0, val;
   while(cin >> val && val){
   bool equal = false;
   for (int i = 0; i < prime.size(); ++i)
   {
        int total = 0;
       for (int j = i; j < prime.size(); ++j)
       {
           total+=prime[j];
           if(total == val){
            cnt++;
           }
           if(total > val)break;
       }
       if(prime[i] > val)break;
   }
   cout << cnt << '\n';
   cnt = 0;
}

return 0;
}

UVA 11879 - Multiple of 17

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
using namespace std;
int main()
{
   ios_base::sync_with_stdio(false);
   string num="";
   while(cin >> num && num!="0"){
    int temp = 0;
    for (int i = 0; i < num.length(); ++i)
    {
        temp = temp*10 + num[i]-'0';
        temp%=17;
        //cout << temp << ' ';
    }
    temp == 0 ? cout<<1<<'\n':cout<<0<<'\n';
   }

return 0;
}

UVA 846 - Steps

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
using namespace std;
int main()
{
   ios_base::sync_with_stdio(false);
   LL T;
   cin >> T;
   while(T--){
    LL x , y;
    cin >> x >> y;
    LL diff = abs(x-y) , start = 1 , cnt = 0;
    bool flag = false;
    while(diff > 0){
        diff-=start;
        cnt++;
        if(flag)start++;
        flag=!flag;
    }
    cout << cnt << '\n';
   }

return 0;
}

Saturday, October 22, 2016

UVA 10469 - To Carry or not to Carry

#include<bits/stdc++.h>
#define printCase cout << "Case "<< ++kase<<":"
#define sf scanf
#define pf printf
#define FAST ios_base::sync_with_stdio(false)
using namespace std;

int main()
{
FAST;
unsigned long long a , b;
    while(cin >> a >> b){
        string s1 , s2 , total;
        s1 =bitset<32>(a).to_string();
        s2 =bitset<32>(b).to_string();

        for (int i = 0; i < s1.length(); ++i)
        {
            if(s1[i] == '1' && s2[i] !='1' ||s1[i] != '1' && s2[i] =='1' )
                total+='1';
            else
                total+='0';
        }
        unsigned long long num ;
        num = bitset<32>(total).to_ulong();
        cout << num << '\n';
    }
return 0;
}

Another solution


int main()
{
FAST;
unsigned long long a , b;
    while(cin >> a >> b){
     
        unsigned long long num ;
        num = a^b;
        cout << num << '\n';
    }
return 0;
}

UVA 11614 - Etruscan Warriors Never Play Chess

#include<bits/stdc++.h>
#define printCase cout << "Case "<< ++kase<<":"
#define sf scanf
#define pf printf
#define FAST ios_base::sync_with_stdio(false)
using namespace std;

int main()
{
//FAST;
double T , kase = 0;
    sf("%lf",&T);
    while(T--){
        double n;
        sf("%lf",&n);
        pf("%lld\n",(long long)(sqrt(1+8*n)-1)/2);

    }
return 0;
}

UVA 10281 - Average Speed

#include <bits/stdc++.h>
#define dbug(x) cout << '>' << #x << ':' << x << endl
#define dbg(x) cout << '>' << #x << ':' << x << " "

#define pline    cout << "_________________________" << endl
#define mem(x, v) memset(x, v, sizeof(x))

#define eef else if
#define sf scanf
#define pf printf
#define i64 long long
#define ll  long long
#define ull unsigned long long

#define all(x) x.begin(), x.end()
#define lla(x) x.rbegin(), x.rend()
#define SORT(c) sort(all(c))
#define ssort(v) stable_sort(v.begin(), v.end())
#define sz(v) (int)(v).size()

#define IT iterator
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define contains(T, x) (T.find(x) != T.end())

#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(max(a,max(b,c)),d)

#define Reverse(x)  reverse(x.begin(),x.end())

#define loop(i,s,e) for(__typeof(s) i=(s);i<=(e);i++)
#define pool(i,e,s) for(__typeof(e) i=(e);i>=(s);i--)

#define FORIT(i, m) for (__typeof((m).begin()) i=(m).begin(); i!=(m).end(); ++i)

#define newl '\n'

//              12345678901234567890
#define MAX     1e5

#define Fast ios_base::sync_with_stdio(0)
using namespace std;

int main(){
    //Fast;
    char time[20]="";
    double speed = 0 , current = 0 , prev = 0 , dis;
    while(sf("%s",time)!=EOF){
    double h , m , s;
    char c;
    sscanf(time,"%lf:%lf:%lf",&h,&m,&s);
    current = (h*3600)+(m*60)+s;
    dis += (current - prev)*(speed/3600.0);
    c = getchar();
    c == ' ' ? sf("%lf",&speed):printf("%s %.2lf km\n",time,dis);
    prev = current;

    }
    return 0;
}

UVA 11723 - Numbering Roads

#include<bits/stdc++.h>
using namespace std;
int main()
{
   ios_base::sync_with_stdio(false);
   int r , n , kase = 0;
   while(cin >> r >> n &&r &&n){
    bool flg = false;
    for (int i = 1; i <= 27; ++i)
    {
        if(i*n >= r){
            cout << "Case "<<++kase<<": "<<i-1<<'\n';
            flg = true;
            break;
        }
    }
    if(!flg)cout << "Case "<<++kase<<": impossible\n";
   }

return 0;
}

UVA 12149 - Feynman

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

int main()
{
    ios_base::sync_with_stdio(false);
   int n;
   while(cin >>n && n){
    int total = 0;
    for (int i = 2; i <= n; ++i)
    {
        total+=(i*i);
    }
    cout << total+1 << '\n';
   }

return 0;
}