P1447开关灯泡

描写叙述

一个房间里有n盏灯泡。一開始都是熄着的,有1到n个时刻。每一个时刻i,我们会将i的倍数的灯泡改变状态(即原本开着的现将它熄灭,原本熄灭的现将它点亮),问最后有多少盏灯泡是亮着的。

格式

输入格式

一个数n

输出格式

m,表示最后有m盏是亮着的

例子1

例子输入1[复制]

5

例子输出1[复制]

2

限制

1s

提示

范围:40%的数据保证,n<=maxlongint

100%的数据保证,n<=10^200

来源

dejiyu@CSC WorkGroup

找规律能够得到x ^ x <= n +1(当中x代表最后剩余的灯的个数)

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

#!/usr/bin/env python3
# -*- coding: utf-8 -*- import math
n = long(raw_input())
lb = 0
ub = n
while ub - lb > 1:
mid = (ub + lb) >> 1
if mid * mid > n + 1:
ub = mid
else:
lb = mid
print lb

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
#include <cstring>
using namespace std; /***************大数模板***************/
#define MAXN 9999
#define MAXSIZE 400
#define DLEN 4 class BigNum {
private:
int a[500 + 5]; //能够控制大数的位数
int len; //大数长度
public:
BigNum() {
len = 1; //构造函数
memset(a,0,sizeof(a));
}
BigNum(const int); //将一个int类型的变量转化为大数
BigNum(const char*); //将一个字符串类型的变量转化为大数
BigNum(const BigNum &); //拷贝构造函数
BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算 friend istream& operator>>(istream&, BigNum&); //重载输入运算符
friend ostream& operator<<(ostream&, BigNum&); //重载输出运算符 BigNum operator+(const BigNum &) const; //重载加法运算符,两个大数之间的相加运算
BigNum operator-(const BigNum &) const; //重载减法运算符,两个大数之间的相减运算
BigNum operator*(const BigNum &) const; //重载乘法运算符,两个大数之间的相乘运算
BigNum operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算 BigNum operator^(const int &) const; //大数的n次方运算
int operator%(const int &) const; //大数对一个int类型的变量进行取模运算
bool operator>(const BigNum & T)const; //大数和还有一个大数的大小比較
bool operator>(const int & t)const; //大数和一个int类型的变量的大小比較 void print(); //输出大数
};
BigNum::BigNum(const int b) { //将一个int类型的变量转化为大数
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) { //将一个字符串类型的变量转化为大数
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) { //拷贝构造函数
int i;
memset(a,0,sizeof(a));
for(i = 0 ; i < len ; i++)
a[i] = T.a[i];
}
BigNum & BigNum::operator=(const BigNum & n) { //重载赋值运算符,大数之间进行赋值运算
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) { //重载输入运算符
char ch[MAXSIZE*4];//用来出去前导的零
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) { //重载输出运算符
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 { //两个大数之间的相加运算
BigNum t(*this);
int i,big; //位数
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 { //两个大数之间的相减运算
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 && 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 { //两个大数之间的相乘运算
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 { //大数对一个整数进行相除运算
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 { //大数对一个int类型的变量进行取模运算
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 { //大数的n次方运算
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 { //大数和还有一个大数的大小比較
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 { //大数和一个int类型的变量的大小比較
BigNum b(t);
return *this>b;
} void BigNum::print() { //输出大数
int i;
cout << a[len - 1];
for(i = len - 2 ; i >= 0 ; i--) {
cout.width(DLEN);
cout.fill('0');
cout << a[i];
}
cout << endl;
} /***************大数模板***************/ 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;
}

vijos - P1447开关灯泡 (大数模板 + 找规律 + 全然数 + python)的更多相关文章

  1. hdu_1041(Computer Transformation) 大数加法模板+找规律

    Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/ ...

  2. vijos - P1077克隆龙 (找规律 + 指数型母函数 + python)

    P1077克隆龙 Accepted 标签:[显示标签] 描写叙述 如今龙的克隆已成为可能,龙基因由ACTG字母组成,而龙的基因有例如以下特点: 1.A在基因中的出现为偶数次(包含0): 2.C的情况也 ...

  3. 【集训笔记】【大数模板】特殊的数 【Catalan数】【HDOJ1133【HDOJ1134【HDOJ1130

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3324 http://blog.csdn.net/xymscau/artic ...

  4. vijos - P1176奇怪的数列 (递归 + 找规律)

    P1176奇怪的数列 Accepted 标签:[显示标签] 背景 一天.学军数学小组的成员遇到了一个奇怪的数列,正巧信息小组的你碰到了他们. 于是他们把这个数列展示给你-- 描写叙述 这个数列是这种: ...

  5. 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】

    度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...

  6. HDU 1134 Game of Connections(卡特兰数+大数模板)

    题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 20 ...

  7. 算法---数组总结篇2——找丢失的数,找最大最小,前k大,第k小的数

    一.如何找出数组中丢失的数 题目描述:给定一个由n-1个整数组成的未排序的数组序列,其原始都是1到n中的不同的整数,请写出一个寻找数组序列中缺失整数的线性时间算法 方法1:累加求和 时间复杂度是O(N ...

  8. hdu 5047 大数找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=5047 找规律 信kuangbin,能AC #include <stdio.h> #include & ...

  9. UVA 10254 - The Priest Mathematician (dp | 汉诺塔 | 找规律 | 大数)

    本文出自   http://blog.csdn.net/shuangde800 题目点击打开链接 题意: 汉诺塔游戏请看 百度百科 正常的汉诺塔游戏是只有3个柱子,并且如果有n个圆盘,至少需要2^n- ...

随机推荐

  1. Codeforces_789C_(dp)

    C. Functions again time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. jQuery 全选、全不选、反选

    <!DOCTYPE html> <html lang="en"> <head> <title></title> < ...

  3. RabbitMQ之六种队列模式

    先学习一下RabbitMQ中的六种队列,只学习前五种,具体的官方文档地址是:http://next.rabbitmq.com/getstarted.html 导入maven依赖: <depend ...

  4. tinyxml

    在TinyXML中,根据XML的各种元素来定义了一些类:        TiXmlBase:整个TinyXML模型的基类.        TiXmlAttribute:对应于XML中的元素的属性.   ...

  5. RN code push自定义弹框

    最近在弄react native的code push热更新问题.开始是用的后台默默更新配置.由于微软服务器速度问题,经常遇到用户一直在下载中问题.而用户也不知道代码需要更新才能使用新功能,影响了正常业 ...

  6. 关于Map的遍历

    想起之前有人问过我这个,那就顺手写一下Map的遍历 Map<Integer, String> map = new HashMap<Integer, String>(); map ...

  7. Python 面向对象 组合-多态与多态性-封装-property

    面向对象-组合 1.什么是组合 组合指的是某一个对象拥有一个属性,该属性的值是另外一个类的对象 class Foo: xxx = 111 class Bar: yyy = 222 obj = Foo( ...

  8. 标量子查询中有ROWNUM=1怎么改?

    碰到标量子查询中有ROWNUM=1怎么改? select to_date(o.postdate,'yyyymmdd'), (select cur.c_code from cur_tbl cur whe ...

  9. assert.ok()详解

    assert.ok(value[, message]) 测试 value 是否为真值.它等同于 assert.equal(!!value, true, message). 如果 value 不是真值, ...

  10. GROUP函数

    GROUP_ID 首先我们看看官方的解释: 大意是GROUP_ID用于区分相同分组标准的分组统计结果. 解释起来比较抽象,下面我们来看看具体的案例. 例1:单一分组 SQL> select gr ...