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 ...
随机推荐
- Start my cnBlogs
Compared to CSDN blog, althought it's my first time to use CNBlog,i felt it makes me more comfortabl ...
- Codeforces Round #360 (Div. 2) D. Remainders Game 中国剩余定理
题目链接: 题目 D. Remainders Game time limit per test 1 second memory limit per test 256 megabytes 问题描述 To ...
- 一些css效果积累
悬浮效果: ul li a{text-decoration: none;color: black} ul li a:hover{color: blue} 鼠标变小手 span:hover{cur ...
- [设计模式] 16 迭代器模式 Iterator Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对迭代器模式是这样说的:提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该对象的内部表示. 类图和实例: 迭代器模式由以下角 ...
- UVALive 3977
直接搜索,简单题: #include<cstdio> #include<cstring> #include<cmath> #include<algorithm ...
- soap消息机制 讲解
SOAP(Simple Object Access Protocol,简单对象访问协议)作为一种信息交互协议在分布式应用中非常广泛,如WebService.在使用.Net开发WebService时候, ...
- log4j 总结 精华
去年这个时候,为做软件工程的大作业就详细学过Log4J的用法了,时隔一年想要在新的项目中好好使用一下的时候,发现几乎全忘了,悲催啊…… 再上网查资料,总是不能找到一篇符合我的口味,拿来就能轻松上手,方 ...
- java reflect 例子
public static void main(String[] args) { Student stu1 = new Student(); stu1.setId(1); stu1.setName(& ...
- 灵魂有香气的女子IOS版本APP,近期将考虑开放源代码
实在太忙,灵魂有香气的女子这个App,断断续续开发了1个多月了,前后台自己独立完成, 由于接触swift没多久,还属于新手行列,不熟悉,希望大家给出意见, 根据意见,完善后将于近期将考虑开放swift ...
- Apache Ignite——新一代数据库缓存系统
[编者按]飞速增长的数据需要大量存储,对这些数据的管理也不是一件容易的事.但相比于存储和管理,如何处理数据才是开发人员真正的挑战.对于TB级别数据的存储和处理通常会让开发人员陷入速度.可扩展性和开销的 ...