andruhovski icon

TaskF

andruhovski | PRO | 05/26/16 01:13:09 PM UTC | 0 ⭐ | 558 👁️ | Never ⏰ | []
C++ |

1.77 KB

|

None

|

0 👍

/

0 👎

// TaskF.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
 
#include <vector>
#include <bitset>
#include <iostream>
#include <utility>
#include <map>
 
std::map<unsigned long long, unsigned __int64> table;
std::vector<unsigned long long> snum(40);
std::vector<unsigned long long> result;
 
 
int _tmain(int argc, _TCHAR* argv[])
{
    unsigned __int64 st = 1;
    unsigned __int64 nn = 1024;
    unsigned __int64 tmp;
    unsigned __int64 i, k, n = 32;
    unsigned long long res;
    
    k = 1i64 << n;
    
    for (auto index = 0; index < n; index++)
    {
        snum[index] = st;
        st *= 3;
    }
 
    
    for (i = 1; i<k - 1; i++)
    {
        tmp = i;
        res = 0;
        for (auto index = 0; index < n; index++)
        {
            if (tmp % 2) 
                res += snum[index];         
            tmp /=2;
        }       
        table.insert(std::make_pair(res, i));
        if (!(i % 1000000)) std::cout << i << std::endl;
    }
    
    auto r1 = table.lower_bound(nn);
    if (r1->first != nn)
    {
        do
        {
            auto r2 = table.find(r1->first - nn);
            if (r2 != table.end())
            {
                //std::cout << r1->first << " " << r1->second << std::endl;
                //std::cout << r2->first << " " << r2->second << std::endl;
                tmp = r1->second;
                for (unsigned long index = 0; index < n; index++)
                {
                    if (tmp % 2)
                        result.push_back(snum[index]);
                    tmp >>= 1;
                }
 
                tmp = r2->second;
                for (unsigned long index = 0; index < n; index++)
                {
                    if (tmp % 2)
                        result.push_back(snum[index]);
                    tmp >>= 1;
                }
#ifndef _DEBUG
                sort(begin(result), end(result));
#endif
                break;
                }
 
            r1 = ++r1;
            } while (r1 != table.end());
    }
    else
    { 
        result.push_back(nn);
    }
    
    std::copy(begin(result), end(result), std::ostream_iterator<unsigned long>(std::cout," "));
    
    return 0;
}

Comments