Saturday, November 5, 2016

UVA 11371 - Number Theory for Newbies

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(LL i = 0; i<MAX; i++)
#define MAX 10000000+10
using namespace std;
int main()
{
    char s[100];
    while(cin >> s){
    sort(s, s+strlen(s));
    for (int i = 0; i < strlen(s); ++i)
    {
    if(s[i] != '0'){
    swap(s[0],s[i]);
    break;
    }
    }
    long long a , b;
    sscanf(s,"%lld",&b);
    sort(s, s+strlen(s));
    for (int i = 0 , j =strlen(s)-1; i < j; ++i,j--)
    {
    swap(s[i],s[j]);

    }
    sscanf(s,"%lld",&a);
    cout<<a<<" - "<<b<<" = "<<a-b<<" = 9 * "<<(a-b)/9<<'\n';
    }

 return 0;
}

UVA 10110 - Light, more light

#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define LL long long
#define REP(i,MAX) for(LL i = 0; i<MAX; i++)
#define MAX 10000000+10
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    long long n;
    while(cin >> n && n){
    floor(sqrt(n)) == ceil(sqrt(n)) ? cout<<"yes\n":cout <<"no\n";
    }

 return 0;
}

Friday, November 4, 2016

UVA 10168 - Summation of Four Primes

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

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

int main()
{
    ios_base::sync_with_stdio(false);
    init_prime();
   LL n;
   while(cin >> n){
    bool found = false;
    if(n < 8){
        cout << "Impossible.\n";
        continue;
    }
    if(n%2 != 0){
        cout <<"2 3 ";
        n-=5;
    }
    else{
        cout <<"2 2 ";
        n-=4;
    }
    if(n == 4){
        cout << "2 2 ";
    }
    else{
        for (int i = 3; i <=(n/2) ; i+=2)
        {
            if(prime[i] && prime[n-i]){
                cout << i <<' '<<n-i<<'\n';
                found = true;
                break;
            }
        }
    }
    if(!found) cout << "Impossible.\n";
   }

 return 0;
}

Thursday, November 3, 2016

UVA 10948 - The primary problem

#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 1000010
bool prime[MAX];
using namespace std;
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;
}

int main()
{
    ios_base::sync_with_stdio(false);
    init_prime();
    int n;
    while(cin >> n && n){
        cout << n << ":\n";
        bool found = false;
        int i;
        for (i = 2; i <= n; ++i)
        {
            if(i > n-i)break;
            if(prime[i] && prime[n-i]){
                found = true;
                break;
            }
        }
        if(found)cout <<i<<'+'<<n-i<<'\n';
        else cout <<"NO WAY!\n";
    }


 return 0;
}