您的位置:首页 > 其它

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<=1e​18

​​ 

输出描述

如果有正整数解,输出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≤1e​5)组数据,每组数据一个正整数n,1≤n≤1e​18

​​ 

输出描述

有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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: