BestCoder Round #82 (div.2) A B
2016-05-02 22:20
411 查看
链接:戳这里
ztr loves math
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
问题描述
ztr喜欢研究数学,一天,他在思考直角三角形方程组的Lower版,即n=x^2-y^2 他想知道,对于给出的n,是否会有正整数解。
输入描述
有T组数据,第一行为一个正整数T(T<=1e6),每一行一个正整数n,n<=1e18
输出描述
如果有正整数解,输出True,否则输出False
输入样例
4
6
25
81
105
输出样例
False
True
True
True
Hint
对于第四个样例,有一组解13^2-8^2=105
思路:
n=x^2-y^2=(x+y)*(x-y) n能整除(x+y)并且n能整除(x-y)
并且(x+y)=a 和 (x-y)=b a + b 这两个数相加和要为偶数
1:a,b要能被n整除
2:a,b必须均为奇数或者偶数
=>假设a=1时那么只需要b=n 并且n为奇数就可以 所以当n为奇数且n不为1时 True
=>假设a b均为偶数 所以n必须整除4 且n!=4 答案为True
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll n;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%I64d",&n);
if(n==1) {
printf("False\n");
continue;
}
if(n%2) printf("True\n");
else if(n%4==0 && n!=4) printf("True\n");
else printf("False\n");
}
return 0;
}
ztr loves lucky numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
问题描述
ztr喜欢幸运数字,他对于幸运数字有两个要求
1:十进制表示法下只包含4、7
2:十进制表示法下4和7的数量相等
比如47,474477就是
而4,744,467则不是
现在ztr想知道最小的但不小于n的幸运数字是多少
输入描述
有T(1≤T≤1e5)组数据,每组数据一个正整数n,1≤n≤1e18
输出描述
有T行,每行即答案
输入样例
2
4500
47
输出样例
4747
47
Hint
请尽可能地优化算法,考虑全面
思路:
先把1~1e18范围内的满足只为4,7且个数相等的数先DFS出来
然后二分找出不小于n的数lower_bound()...
但是这里有一个hack点 就是当n==0 或者 n>444444444777777777时 分别输出47 和 44444444447777777777
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll n;
ll a[1000100];
int tot;
bool pd(ll t){
int a=0,b=0;
while(t){
if(t%10==4) a++;
else b++;
t/=10;
}
if(a==b) return true;
return false;
}
void DFS(int x,ull t){
if(t>1e18 || x>18) return ;
if(pd(t) && t) a[tot++]=t;
DFS(x+1,t*10+4);
DFS(x+1,t*10+7);
}
void init(){
tot=0;
DFS(0,0);
sort(a,a+tot);
/*cout<<tot<<endl;
for(int i=0;i<tot;i++) cout<<a[i]<<" ";
cout<<endl;*/
}
int main(){
init();
int T;
scanf("%d",&T);
while(T--){
scanf("%I64d",&n);
int x=lower_bound(a,a+tot,n)-a;
if(a[tot-1]<n) printf("44444444447777777777\n");
else printf("%I64d\n",a[x]);
}
return 0;
}
ztr loves math
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
问题描述
ztr喜欢研究数学,一天,他在思考直角三角形方程组的Lower版,即n=x^2-y^2 他想知道,对于给出的n,是否会有正整数解。
输入描述
有T组数据,第一行为一个正整数T(T<=1e6),每一行一个正整数n,n<=1e18
输出描述
如果有正整数解,输出True,否则输出False
输入样例
4
6
25
81
105
输出样例
False
True
True
True
Hint
对于第四个样例,有一组解13^2-8^2=105
思路:
n=x^2-y^2=(x+y)*(x-y) n能整除(x+y)并且n能整除(x-y)
并且(x+y)=a 和 (x-y)=b a + b 这两个数相加和要为偶数
1:a,b要能被n整除
2:a,b必须均为奇数或者偶数
=>假设a=1时那么只需要b=n 并且n为奇数就可以 所以当n为奇数且n不为1时 True
=>假设a b均为偶数 所以n必须整除4 且n!=4 答案为True
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll n;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%I64d",&n);
if(n==1) {
printf("False\n");
continue;
}
if(n%2) printf("True\n");
else if(n%4==0 && n!=4) printf("True\n");
else printf("False\n");
}
return 0;
}
ztr loves lucky numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
问题描述
ztr喜欢幸运数字,他对于幸运数字有两个要求
1:十进制表示法下只包含4、7
2:十进制表示法下4和7的数量相等
比如47,474477就是
而4,744,467则不是
现在ztr想知道最小的但不小于n的幸运数字是多少
输入描述
有T(1≤T≤1e5)组数据,每组数据一个正整数n,1≤n≤1e18
输出描述
有T行,每行即答案
输入样例
2
4500
47
输出样例
4747
47
Hint
请尽可能地优化算法,考虑全面
思路:
先把1~1e18范围内的满足只为4,7且个数相等的数先DFS出来
然后二分找出不小于n的数lower_bound()...
但是这里有一个hack点 就是当n==0 或者 n>444444444777777777时 分别输出47 和 44444444447777777777
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll n;
ll a[1000100];
int tot;
bool pd(ll t){
int a=0,b=0;
while(t){
if(t%10==4) a++;
else b++;
t/=10;
}
if(a==b) return true;
return false;
}
void DFS(int x,ull t){
if(t>1e18 || x>18) return ;
if(pd(t) && t) a[tot++]=t;
DFS(x+1,t*10+4);
DFS(x+1,t*10+7);
}
void init(){
tot=0;
DFS(0,0);
sort(a,a+tot);
/*cout<<tot<<endl;
for(int i=0;i<tot;i++) cout<<a[i]<<" ";
cout<<endl;*/
}
int main(){
init();
int T;
scanf("%d",&T);
while(T--){
scanf("%I64d",&n);
int x=lower_bound(a,a+tot,n)-a;
if(a[tot-1]<n) printf("44444444447777777777\n");
else printf("%I64d\n",a[x]);
}
return 0;
}
相关文章推荐
- 5-33 地下迷宫探索 (30分)
- Hadoop MapReduce概念学习系列之在MapReduce编程时,三大接口抉择(十六)
- arm linux start_kernel之前页表初始化
- Collection和Collections的区别
- Linux之DRBA搭建与部署
- Java笔记---Hadoop 2.7.1下WordCount程序详解
- springMVC(10)------springMVC重定向
- Mongoose(三):数据类型
- Python之实现一个简易计算器
- 统计单链表中的节点数
- 要哭你回去哭,我受够了
- visibility与display
- STL学习_配接器篇
- [NIO日记](2)NIO vs IO
- 第58课:使用Java和Scala在IDE中开发DataFrame实战
- 用小猫统计制作运动员评价管理系统:修改一下主表的中文名称
- 20159315《网络攻防实践》第九周总结
- 母亲的一跪
- Java线程泄露的分析与处理
- SGU-495 Kids and Prizes(概率DP)