Problem 1:Odd Divisor
You are given an integer n. Check if n has an odd divisor, greater than one (does there exist such a number x (x>1) that n is divisible by x and x is odd).
For example, if n=6, then there is x=3. If n=4, then such a number does not exist.
Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
Each test case contains one integer n (2≤n≤1014).
Please note, that the input for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language.
Output
For each test case, output on a separate line:
“YES” if n has an odd divisor, greater than one;
“NO” otherwise.
You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
inputCopy
6
2
3
4
5
998244353
1099511627776
outputCopy
NO
YES
NO
YES
YES
NO
方法1:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
long long n,c;
cin>>n;
while(n--){
cin>>c;
while(c%2==0)c>>=1;/此处c>>=1可以类似于c/=2;
if(c==1)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}
方法2:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
long long n,c;
cin>>n;
while(n--){
cin>>c;
if(c%2!=0)/其实不用讨论是奇数的情况,因为如果是奇数的话就不进那个while循环就一定不会是1,所以可以不用写此处。
cout<<"YES"<<endl;
else{
while(c%2==0){
c/=2;
}
if(c==1)
cout<<" NO"<<endl;
else
cout<<"YES"<<endl;
}
}
return 0;
}
本题主要就是判断一个整数是否含有奇数因子,只要判断是否为2的幂即可,即当一个数为偶数时,每次对该数除以2,看最后是否等于1,若等于1则证明是2的幂。
唯一分解定理:
任何大于1的自然数n并且n不为质数的情况下,都可以唯一的分解成有限个质数的乘积
质数里只有2是偶数,所以给一个数所有的2的因子都除去,如果剩的是1 原来的数就没有奇数因子,不然的话一定有.
Problem 2:New Year’s Number
Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.
For example, if:
n=4041, then the number n can be represented as the sum 2020+2021;
n=4042, then the number n can be represented as the sum 2021+2021;
n=8081, then the number n can be represented as the sum 2020+2020+2020+2021;
n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.
Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
Each test case contains one integer n (1≤n≤106) — the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.
Output
For each test case, output on a separate line:
“YES” if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
“NO” otherwise.
You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
inputCopy
5
1
4041
4042
8081
8079
outputCopy
NO
YES
YES
YES
NO
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
long long t,n;
cin>>t;
while(t--)
{
cin>>n;
if(n%2020<=n/2020)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
本题主要判断一个数是否可以由若干个2020和若干个2021组成。
please waiting…