vijos-p1447 Switch Bulb (large number of templates + Find rule + full number + Python)

Source: Internet
Author: User

P1447Switch Bulb accepted Label: CSC WorkGroup iii[display label]<textarea id="code" class="textbox" style=""></textarea>Describe

There are n bulbs in a room, at first it is extinguished, there are 1 to n moments, each time I, we will be a multiple of the bulb change the state (that is, the original open will it extinguished, the original extinguished it is now lit), asked the last number of light bulbs is on.

Format input Format

A number n

Output format

M, indicating that the last m is lit.

Example 1 sample input 1[copy]
5
Sample output 1[Copy]
2
Limit

1s

Tips

Range: 40% data guarantee, N<=maxlongint
100% Data Guarantee, n<=10^200

Source

[Email protected] WorkGroup

The pattern can be obtained by x ^ x <= n +1 (where x represents the number of the last remaining lights)


#!/usr/bin/env python3#-*-coding:utf-8-*-import mathn = Long (raw_input ()) lb = 0ub = Nwhile ub-lb > 1:    mid = ( UB + lb) >> 1    if mid * mid > n + 1:        UB = Mid    else:        lb = midprint lb



#include <iostream> #include <string> #include <iomanip> #include <algorithm> #include < Cstring>using namespace std;/*************** large number template ***************/#define MAXN 9999#define MAXSIZE 400#define DLEN    4class Bignum {private:int a[500 + 5];       You can control the number of digits of the large number int len;    Large number of Lengths Public:bignum () {len = 1;    Constructor memset (A,0,sizeof (a));       } bignum (const int);     Converts a variable of type int to a large number bignum (const char*);  Converts a variable of a string type to a large number bignum (const bignum &);   Copy constructor Bignum &operator= (const bignum &);   Overloaded assignment operators, assigning values between large numbers friend istream& operator>> (istream&, bignum&);   Overloaded input operator friend ostream& operator<< (ostream&, bignum&);   Overloaded output operator Bignum operator+ (const bignum &) const;   Overloaded addition operator, the addition of two large numbers bignum operator-(const bignum &) const;   The overloaded subtraction operator, the subtraction operation between two large numbers bignum operator* (const bignum &) const; Overloaded multiplication operator, the multiplication operation between two large numbers bignum operator/(const int &) const;    Overloaded division operator, large number divides an integer by bignum operator^ (const int &) const;    Large number of n-th-square operations int operator% (const int &) const;   Large number modulo a variable of an int type bool operator> (const BIGNUM & T) const;      Large number and another large number of sizes compare bool Operator> (const INT & T) const;       The size of a variable of large number and an int type is compared with void print (); Output large number};    Bignum::bignum (const int b) {//Converts a variable of type int to a large number int c,d = b;    len = 0;    memset (A,0,sizeof (a));        while (d > maxn) {c = d-(d/(MAXN + 1)) * (MAXN + 1);        D = d/(MAXN + 1);    a[len++] = c; } a[len++] = D;}    Bignum::bignum (const char*s) {//Converts a variable of a string type to a large number int t,k,index,l,i;    memset (A,0,sizeof (a));    L=strlen (s);    Len=l/dlen;    if (L%dlen) len++;    index=0;        for (I=L-1; i>=0; i-=dlen) {t=0;        k=i-dlen+1;        if (k<0) k=0;        for (int j=k; j<=i; j + +) t=t*10+s[j]-' 0 ';    a[index++]=t; }}bignum::bignum (const Bignum & T): Len (T.len) {//copy constructor int i;    memset (A,0,sizeof (a)); for (i = 0; i < len; i++) a[i] = T.a[i];}    Bignum & bignum::operator= (const Bignum & N) {//overloaded assignment operator, large number of assignment operations int i;    len = N.len;    memset (A,0,sizeof (a));    for (i = 0; i < len; i++) a[i] = N.a[i]; return *this;} istream& operator>> (IStream & In, Bignum & B) {//overloaded input operator char ch[maxsize*4];//used to go out leading 0 int i =-    1;    in>>ch;    int L=strlen (CH);    int count=0,sum=0; for (I=L-1; i>=0;)        {sum = 0;        int t=1;        for (int j=0; j<4&&i>=0; j++,i--, t*=10) {sum+= (ch[i]-' 0 ') *t;        } b.a[count]=sum;    count++;    } B.len =count++; return in;}    ostream& operator<< (ostream& out, bignum& b) {//overloaded output operator int i;    cout << b.a[b.len-1];        for (i = b.len-2; I >= 0; i--) {cout.width (Dlen);        Cout.fill (' 0 ');    cout << B.a[i]; } return out;} Bignum Bignum::operator+ (const BIGNUM & T) const {//Two a large number of addition operations Bignum T (*this);      int i,big; Number of digits big = t.len > len?    T.len:len;        for (i = 0; i < big; i++) {t.a[i] +=t.a[i];            if (T.a[i] > Maxn) {t.a[i + 1]++;        T.a[i]-=maxn+1;    }} if (T.a[big]! = 0) T.len = big + 1;    else T.len = big; return t;}    Bignum bignum::operator-(const BIGNUM & T) const {//Two a large number of subtraction operations int i,j,big;    BOOL Flag;    Bignum t1,t2;        if (*this>t) {t1=*this;        t2=t;    flag=0;        } else {t1=t;        T2=*this;    flag=1;    } Big=t1.len;            for (i = 0; i < big; i++) {if (T1.a[i] < T2.a[i]) {j = i + 1;            while (t1.a[j] = = 0) j + +;            t1.a[j--]--;            while (J > i) t1.a[j--] + = MAXN;        T1.a[i] + = MAXN + 1-t2.a[i];    } else t1.a[i]-= t2.a[i];    } T1.len = big; while (t1.a[len-1] = = 0 &&Amp        T1.len > 1) {t1.len--;    big--;    } if (flag) t1.a[big-1]=0-t1.a[big-1]; return t1;}    Bignum bignum::operator* (const BIGNUM & T) const {//Two a large number of multiplication operations bignum ret;    int i,j,up;    int temp,temp1;        for (i = 0; i < len; i++) {up = 0;            for (j = 0, J < T.len; J + +) {temp = a[i] * T.a[j] + ret.a[i + j] + up;                if (Temp > maxn) {temp1 = temp-temp/(MAXN + 1) * (MAXN + 1);                Up = temp/(MAXN + 1);            Ret.a[i + j] = Temp1;                } else {up = 0;            Ret.a[i + j] = temp;    }} if (up! = 0) Ret.a[i + j] = up;    } Ret.len = i + j;    while (ret.a[ret.len-1] = = 0 && ret.len > 1) ret.len--; return ret;}    Bignum bignum::operator/(const int & B) const {//large number to an integer divide operation bignum ret;    int i,down = 0; for (i = len-1; I >= 0, i--) {ret.a[i] = (A[i] + down * (MAXN + 1))/b;    Down = a[i] + down * (MAXN + 1)-ret.a[i] * b;    } Ret.len = Len;    while (ret.a[ret.len-1] = = 0 && ret.len > 1) ret.len--; return ret;}    int Bignum::operator% (const int & B) const {//large number to a variable of type int is modulo operation int i,d=0;    for (i = len-1; i>=0; i--) {d = ((d * (maxn+1))% B + a[i])% B; } return D;}    Bignum bignum::operator^ (const int & N) const {//large number of n-Th square operation Bignum T,ret (1);    int i;    if (n<0) exit (-1);    if (n==0) return 1;    if (n==1) return *this;    int m=n;        while (m>1) {t=*this;        for (I=1; i<<1<=m; i<<=1) {t=t*t;        } m-=i;        Ret=ret*t;    if (m==1) ret=ret* (*this); } return ret;}    BOOL Bignum::operator> (const BIGNUM & T) const {//large number and another large number compared to the size of int ln;    if (len > T.len) return true;        else if (len = = t.len) {ln = len-1;      while (a[ln] = = T.a[ln] && ln >= 0)      ln--;        if (ln >= 0 && A[ln] > T.a[ln]) return true;    else return false; } else return false;}    BOOL Bignum::operator > (const int & T) const {//large number and an int type of variable size comparison bignum B (t); return *this>b;}    void Bignum::p rint () {//output large number int i;    cout << a[len-1];        for (i = len-2; I >= 0; i--) {cout.width (Dlen);        Cout.fill (' 0 ');    cout << A[i]; } cout << Endl;}    /*************** large number of templates ***************/int main () {int i,n;    Bignum x;    Cin>> x;    x = x + 1;    Bignum lb = 0, ub = x;        while (ub-lb > 1) {bignum mid = (UB + lb)/2;        if (Mid * mid > x + 1) UB = mid;    else lb = mid; } cout<<lb;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

vijos-p1447 Switch Bulb (large number of templates + Find rule + full number + Python)

Related Article

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.