Fifth annual Blue Bridge Cup final c\c++b Group-biochip

Source: Internet
Author: User
Tags square root

Part I: Topics

Bio-chip

Dr X is working on a biochip that has a much higher degree of logic density and capacity than ordinary semiconductor chips.

The Doctor has designed n miniature light sources in the chip, and each light source will change its state once it is switched on, that is, the light turns off, or turns the switch off to light.

These light sources are numbered from 1 to N, and all light sources are turned off at the beginning.

The doctor plans to perform the following actions on the chip:

All lights that are numbered in multiples of 2 are operated once, i.e. 2 4 6 8 ... Wait for the number light to open

All lights that are numbered in multiples of 3 are operated once, that is, 3 6 9 ... And so on the operation of the light source, note that at this point 6th light source is closed again.

All lights with a multiple of number 4 are operated once.

.....

The light is manipulated one time until a multiple of the number is N.


Dr X wants to know which light sources in an interval are lit after these operations.


"Input Format"
3 integers separated by spaces: N L R (l<r<n<10^15) n represents the number of lights, L represents the left boundary of the interval, and R represents the right boundary of the interval.

"Output Format"
Outputs 1 integers, indicating how many lights in the [l,r] interval are lit after all operations.

For example:
Input:
5 2 3
The program should output:
2

Again for example:
Input:
10 3 6
The program should output:
3

Resource contract:
Peak memory Consumption < 256M
CPU Consumption < 1000ms


Please strictly according to the requirements of the output, do not use the superfluous printing similar: "Please enter ..." Redundant content.

All the code is placed in the same source file, after debugging passed, the copy is submitted to the source.

Note: The main function needs to return 0
Note: Use only ANSI c/ansi C + + standards, and do not invoke special functions that depend on the compilation environment or operating system.
Note: All dependent functions must explicitly #include <xxx> in the source file, and the common header files cannot be omitted from the project settings.

When committing, be careful to choose the type of compiler you expect.

Part II: Ideas

Originally I wrote it in the original way. 2~ n is calculated from L~r, and if the number of factors is odd then the light source is closed because it is calculated from 2, and 1 is a factor of all numbers, so the number of factors is turned off. This method results in a clear, timed out. Then read the discussion on the Internet. It is known that the number of factors is odd is called the total square number, the total square number is can be written in the form of the number of m*m, one of its characteristics is that the number of factors is an odd number. But the data is 10 of the 15-time side, if a judge or timeout, how to do? Declares a variable I enumerates from the arithmetic square root of L until I*i is greater than R. As long as the i*i is within the [L,r] range, the light source that is numbered i*i is turned off. Defines the sum=r-l+1, which represents the total number of lights in the interval, minus one whenever the light source is closed.

Part III: Code

#include <stdio.h>#include<stdlib.h>#include<math.h>intMain () {intN; Long Longa,b,len=0;//because the data is large, it is defined as a long long typescanf"%lld%lld%lld",&n,&a,&b); Long Longsum; Sum=b-a+1;//total number of lights in interval     for(Long LongI=sqrt (a); i*i<=b;i++)    {            if(i*i>=a&&i*i<=b) {sum--;//Turn off the light source minus}} printf ("%lld\n", sum); return 0;}

Fifth annual Blue Bridge Cup final c\c++b Group-biochip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.