tc 2014 college tour 250 500
题意:
You are given a long long n. Return the largest divisor of n that is a perfect square. That is, the correct return value is x if and only if:
- x divides n
- There is an integer y such that x = y*y.
- x is the largest integer that satisfies conditions 1 and 2.
求最大的y*y 使N MOD (y*y)=0;
x = y*y 返回x的值。
分析:
假设 x*t = n;
当t<=10^6时,枚举t,同时查看是否符合y*y == x的条件。
当t>10^6时,即x<=10^12, y<=10^6, 此时枚举y,
所以两个10^6的循环就可以了。
500: 错误代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
#define LL long long
using namespace std; class SquareDivisor
{
public:
long long biggest(long long n)
{
long long i, x, tmp;
for(i = ; i <= ; i++)
{
if(n%i == )
{
x = n/i;
tmp = sqrt(x);
if(tmp*tmp == x)
return x;
}
}
for(i = ; i <= ; i++)
{
x = i*i;
if(n%x == )
return x;
}
}
}; int main()
{
long long n, ans;
SquareDivisor xx;
while(cin>>n)
{
ans = xx.biggest(n);
cout<<ans<<endl;
}
}
先保存一下,不知道为什么错了
知道错哪了:首先没有判断 x>n的时候停止, 而且第二个循环的时候从前向后 return 的不是最大值。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
#define LL long long
using namespace std; class SquareDivisor
{
public:
long long biggest(long long n)
{
long long i, x, tmp;
for(i = ; i <= ; i++)
{
if(i > n) break; //
if(n%i == )
{
x = n/i;
tmp = sqrt(x);
if(tmp*tmp == x)
return x;
}
}
for(i = ; i >= ; i--) //
{
x = i*i;
if(x > n) continue; //
if(n%x == )
return x;
}
}
}; int main()
{
long long n, ans;
SquareDivisor xx;
while(cin>>n)
{
ans = xx.biggest(n);
cout<<ans<<endl;
}
}
tc 2014 college tour 250 500的更多相关文章
- topcoder srm 628 div2 250 500
做了一道题,对了,但是还是掉分了. 第二道题也做了,但是没有交上,不知道对错. 后来交上以后发现少判断了一个条件,改过之后就对了. 第一道题爆搜的,有点麻烦了,其实几行代码就行. 250贴代码: #i ...
- SRM 719 Div 1 250 500
250: 题目大意: 在一个N行无限大的网格图里,每经过一个格子都要付出一定的代价.同一行的每个格子代价相同. 给出起点和终点,求从起点到终点的付出的最少代价. 思路: 最优方案肯定是从起点沿竖直方向 ...
- TC SRM 593 DIV1 250
我只能说的亏没做,要不就挂0了.. 本来想四色定理,肯定4就可以的...然后准备爆,发现3的时候不好爆,又想了老一会,嗯,数据范围不小,应该不是暴力,直接找规律,貌似最大就是3,有一个3连块,输出3, ...
- TC SRM 593 DIV1 250(dfs)
这图最多3色就可以 搜2就行了 #include <iostream> #include<cstdio> #include<cstring> #include< ...
- TopCoder入门
TopCoder入门 http://acmicpc.info/archives/164?tdsourcetag=s_pctim_aiomsg 本文根据经典的TC教程完善和改编.TopCoder:htt ...
- Java-坦克大战
利用Java语言中的集合.Swing.线程等知识点编写一个坦克大战游戏.(1) 画出敌我坦克的原理:在坦克类里面有一个布尔类型变量good.用于判断坦克的阵营,在创建坦克对象时在Tank类的构造方法中 ...
- 越狱Season 1- Episode 18: Bluff
Season 1, Episode 18: Bluff -Michael: Scofield Scofield Michael Scofield Michael Scofield -Patoshik: ...
- [译]SQL数据库迁移:从低版到高版本
我见过太多的数据库管理员花大量的时间在数据库迁移上,即便在客户的实际环境亦是如此.由于微软频繁的发布新版,基于业务和客户的要求,应用服务不得不同时升级.当然,还有许多用户仍在使用SQL Server ...
- 013-多线程-基础-Fork/Join框架、parallelStream讲解
一.概述 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 它同ThreadPoolExecut ...
随机推荐
- 常用汉字的Unicode码表
\u96d5\u864e\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u77 ...
- oracle中操作数据
使用特定格式插入日期值 insert into emp values (,', to_date('1988-11-11','yyyy-mm-dd'), ); ,); 使用子查询插入数据 create ...
- [resource-]Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
reference: http://www.52nlp.cn/python-%e7%bd%91%e9%a1%b5%e7%88%ac%e8%99%ab-%e6%96%87%e6%9c%ac%e5%a4% ...
- c++ 时间格式化
struct tm tm_time; strptime(time.c_str(), "%Y%m%d %H:%M:%S", &tm_time); time_t tt = mk ...
- python 发邮件 utf-8
import smtplib from operator import itemgetter, attrgetter from email.mime.text import MIMEText from ...
- Jqgrid使用
$('#mygrid').jqGrid('GridUnload'); //保留table元素 $('#mygrid').jqGrid('GridDestroy '); //相当于remove,移除 ...
- 【DP】BZOJ 1260: [CQOI2007]涂色paint
1260: [CQOI2007]涂色paint Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 893 Solved: 540[Submit][Stat ...
- $('li','div') $('div li') $('div li')
$('div','li')是$(子,父),是从父节点里找子,而不是找li外面的div $('div , li')才是找所有的div和li,之间不存在父子关系 $('div li') 是找div里面所有 ...
- Consumer closed input channel or an error occurred. events=0x8 channel is unrecoverably broken and will be disposed(待解决)
跟文件读取有关?关闭文件读取试试. 有可能是读取文件 出现报错
- POJ 2948 Martian Mining(DP)
题目链接 题意 : n×m的矩阵,每个格子中有两种矿石,第一种矿石的的收集站在最北,第二种矿石的收集站在最西,需要在格子上安装南向北的或东向西的传送带,但是每个格子中只能装一种传送带,求最多能采多少矿 ...