Huge graph exercise with binary operations

时间:2015-05-12 22:20:34

标签: algorithm

So I've come across the following problem:

Lets --remote-debugging-port= be a graph with G vertices labeled 2^N to 0. It's a directed acyclic graph and in order for there to be a path from 2^N-1 to a the following two conditions must apply:

  1. b
  2. a < b (in binary) = a XOR b, where 2^x

I need to determine the total number of paths of length x>=0 (all edges have length K). The input consists of 1 and N.

The easy way to count the number of paths of length K would be to look at K as any other graph and use the conditions to determine which vertices are connected. However, the task requires that G and N be of size K.

2<=K<=N<=100000 is a bit too much, so there has to be a better way to calculate this. Also I'm limited to an execution time of 2^100000 ms.

Example: For 200 and N = 4, the result is K = 2.

Any ideas?

1 个答案:

答案 0 :(得分:5)

So basically we need to find how many <!DOCTYPE html> <!--[if IE 8]> <html class="ie ie8" <?php language_attributes(); ?>> <![endif]--> <!--[if IE 9]> <html class="ie ie9" <?php language_attributes(); ?>> <![endif]--> <!--[if gt IE 9]><!--> <html <?php language_attributes(); ?>> <!--<![endif]--> <head> <?php /** * Match wp_head() indent level */ ?> <meta charset="<?php bloginfo('charset'); ?>" /> <title><?php wp_title(''); // stay compatible with SEO plugins ?></title> <?php if (!Bunyad::options()->no_responsive): // don't add if responsiveness disabled ?> <meta name="viewport" content="width=device-width, initial-scale=1" /> <?php endif; ?> <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> <?php if (Bunyad::options()->favicon): ?> <link rel="shortcut icon" href="<?php echo esc_attr(Bunyad::options()->favicon); ?>" /> <?php endif; ?> <?php if (Bunyad::options()->apple_icon): ?> <link rel="apple-touch-icon-precomposed" href="<?php echo esc_attr(Bunyad::options()->apple_icon); ?>" /> <?php endif; ?> <?php wp_head(); ?> <!--[if lt IE 9]> <script src="<?php echo get_template_directory_uri(); ?>/js/html5.js" type="text/javascript"></script> <![endif]--> </head> <body <?php body_class(); ?>> <div class="main-wrap"> <?php /** * Get the partial template for top bar */ get_template_part('partials/header/top-bar'); ?> <div id="main-head" class="main-head"> <div class="wrap"> <?php /** * Get the header based on settings */ $header = Bunyad::options()->header_style ? Bunyad::options()->header_style : 'default'; get_template_part('partials/header/' . $header); /** * Setup data variables to enable or disable sticky nav functionality */ $attribs = array('class' => array('navigation cf', Bunyad::options()->nav_align)); if (Bunyad::options()->sticky_nav) { $attribs['data-sticky-nav'] = 1; // sticky navigation logo? if (Bunyad::options()->sticky_nav_logo) { $attribs['data-sticky-logo'] = 1; } } ?> <nav <?php Bunyad::markup()->attribs('navigation', $attribs); ?>> <div class="mobile" data-type="<?php echo Bunyad::options()->mobile_menu_type; ?>" data-search="<?php echo Bunyad::options()->mobile_nav_search; ?>"> <a href="#" class="selected"> <span class="text"><?php _e('Navigate', 'bunyad'); ?></span><span class="current"></span> <i class="hamburger fa fa-bars"></i> </a> </div> <?php wp_nav_menu(array('theme_location' => 'main', 'fallback_cb' => '', 'walker' => 'Bunyad_Menu_Walker')); ?> </nav> </div> </div> <?php if (!Bunyad::options()->disable_breadcrumbs): ?> <div class="wrap"> <?php Bunyad::core()->breadcrumbs(); ?> </div> <?php endif; ?> <?php do_action('bunyad_pre_main_content'); ?> **Layout page:** <?php /* Template Name: Radio Player */ get_header('radio'); if (Bunyad::posts()->meta('featured_slider')): get_template_part('partial-sliders'); endif; ?> <div class="main wrap cf"> <div class="row"> <div class="col-8 main-content"> <?php if (have_posts()): the_post(); endif; // load the page ?> <div id="post-<?php the_ID(); ?>" <?php post_class('page-content'); ?>> <?php if (Bunyad::posts()->meta('page_title') != 'no'): ?> <header class="post-header"> <?php if (has_post_thumbnail()): ?> <div class="featured"> <a href="<?php $url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full'); echo $url[0]; ?>" title="<?php the_title_attribute(); ?>"> <?php if ((!in_the_loop() && Bunyad::posts()->meta('layout_style') == 'full') OR Bunyad::core()->get_sidebar() == 'none'): // largest images - no sidebar? ?> <?php the_post_thumbnail('main-full', array('title' => strip_tags(get_the_title()))); ?> <?php else: ?> <?php the_post_thumbnail('main-slider', array('title' => strip_tags(get_the_title()))); ?> <?php endif; ?> </a> </div> <?php endif; ?> </header><!-- .post-header --> <?php endif; ?> <div class="refreshMe"> <?php Bunyad::posts()->the_content(); ?> </div> </div> </div> <!-- .row --> </div> <!-- .main --> </div> <?php get_footer('radio'); ?> **i put my refresh div on the layout page with class name "refreshMe Footer.php** <?php do_action('bunyad_post_main_content'); ?> <footer class="main-footer"> <p> Copyright &copy; Babylon Radio 2015 </p> </footer> </div> <!-- .main-wrap --> <?php wp_enqueue_script("jquery"); ?> <script src="<?php echo get_template_directory_uri(); ?>/js/jquery.timers-1.0.0.js"></script> <script type="text/javascript"> jQuery(document).ready(function(){ var j = jQuery.noConflict(); j(document).ready(function() { j(".refreshMe").everyTime(10000,function(i){ j.ajax({ cache: false, success: function(html){ j(".refreshMe").html(html); } }) }) }); j('.refreshMe').css({color:"red"}); }); </script> <?php wp_footer(); ?> </body> </html> **and my ajax script is in the footer page.** Help me out with this there are such that:

x0 < x1 < ... < xk < 2^N

Note that for the xi xor xi+1 = 2^x of two numbers to be a power of 2, the result of the xor must contain exactly 1 bit set. Therefore, the 2 numbers must differ in only one position (otherwise, more positions would be set in the xor, because xor returns 1 only when the operands are different bits).

Let's assume we start at xor. We can move from it to any node labeled with a power of 0.

From that node, we can then move to any node that has set bits in the position of the current node's set bits and a set bit somewhere else. This goes on, taking into account that we must generate the sequence in increasing order.

For example:

2

So, in order to get a path of length 000 -> 001 -> 011 -> 111 -> 101 -> 111 -> 010 -> 011 -> 111 -> 110 -> 111 -> 100 -> 101 -> 111 -> 110 -> 111 => 6 = 3! solutions 0000 -> 0001 -> 0011 -> 0111 -> 1111 -> 1011 -> 1111 -> 0101 -> 1101 -> 1111 -> 0111 -> 1111 -> 1001 -> 1011 -> 1111 -> 1101 -> 1111 => 6 solutions => another 3*6 for the other 3 possibilities from 0000 => 24 solutions , you need to start from an integer with at least k unset (0) bits.

So you can find out how many k integers have at least n-bit unset bits, find out how many possibilities there are to pick k from those, and multiply that answer by k.

To find how many k! integers have exactly n-bit unset bits, you can use the binomial coefficient:

k'

You will also need to multiply this by Binomial(n, k') = n! / [k'!*(n - k')!] in order to decide which Binomial(k', k) bits you'll be using.

You'll need to sum this for k and multiply the sum by k' = k to n.

Those are going to be some huge numbers. I'm guessing you're asked for the result modulo a prime, in which case you'll have to use modular multiplicative inverses.