time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
On a random day, Neko found nn treasure chests and mm keys. The ii-th chest has an integer aiai written on it and the jj-th key has an integer bjbj on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.
The jj-th key can be used to unlock the ii-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, ai+bj≡1(mod2)ai+bj≡1(mod2). One key can be used to open at most one chest, and one chest can be opened at most once.
Find the maximum number of chests Neko can open.
Input
The first line contains integers nn and mm (1≤n,m≤1051≤n,m≤105) — the number of chests and the number of keys.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the numbers written on the treasure chests.
The third line contains mm integers b1,b2,…,bmb1,b2,…,bm (1≤bi≤1091≤bi≤109) — the numbers written on the keys.
Output
Print the maximum number of chests you can open.
Examples
input
Copy
5 4
9 14 6 2 11
8 4 7 20
output
Copy
3
input
Copy
5 1
2 4 6 8 10
5
output
Copy
1
input
Copy
1 4
10
20 30 40 50
output
Copy
0
Note
In the first example, one possible way to unlock 33 chests is as follows:
In the second example, you can use the only key to unlock any single chest (note that one key can't be used twice).
In the third example, no key can unlock the given chest.
题意:
从第一行数组里选一个数a[i],从第二行里选一个数b[j],使a[i]+b[j]为奇数。
是的这样的数最多,那就是尽量一奇一偶的组合。
#include
using namespace std;
#define ll long long
const int maxn=1e5+5;
int a[maxn],b[maxn];
int main()
{
int n,m;
cin>>n>>m;
int Count1=0,Count2=0;
for(int i=0;i>a[i];
if(a[i]&1)
Count1++;//a[i]奇数个数
}
for(int i=0;i>b[i];
if(b[i]&1)
Count2++;//b[i]奇数个数
}
int sum=0;
sum+=(Count1>=m-Count2)?m-Count2:Count1;//奇数和偶数中少的那个决定
sum+=(Count2>=n-Count1)?n-Count1:Count2;
cout<
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number xx. A perfect longcat is a cat with a number equal 2m−12m−1 for some non-negative integer mm. For example, the numbers 00, 11, 33, 77, 1515 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on xx:
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 4040 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 4040 operations.
Input
The only line contains a single integer xx (1≤x≤1061≤x≤106).
Output
The first line should contain a single integer tt (0≤t≤400≤t≤40) — the number of operations to apply.
Then for each odd-numbered operation print the corresponding number nini in it. That is, print ⌈t2⌉⌈t2⌉ integers nini (0≤ni≤300≤ni≤30), denoting the replacement xx with x⊕(2ni−1)x⊕(2ni−1) in the corresponding step.
If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.
Examples
input
Copy
39
output
Copy
4
5 3
input
Copy
1
output
Copy
0
input
Copy
7
output
Copy
0
Note
In the first test, one of the transforms might be as follows: 39→56→57→62→6339→56→57→62→63. Or more precisely:
In the second and third test, the number already satisfies the goal requirement.
题意:把x经过最多40次操作,使x变成2^i-1
操作A:当操作回合为奇数时,x异或2^j-1
操作B:当操作回合为偶数使,x+1
分析
对于答案没有限制,那我们就可以往把所有的数最后异或为0去想。
在异或时,我们可以把一步步把数的最高位异或为0,直到这个数为0
比如
x=39 即二进制100111
然后我们找111111去异或它,最高位就变为0,
那么我们怎么找111111这样的数的?
因为这个是转化为二进制的,也就是它是2的k次方-1,也就是pow(2,k)-1
这就转化为怎么找k,k是和x有关系的,若x=2^i,那k就是2^(i+1)
那怎么求这个i呢?log2(x)就可以解决!
#include
using namespace std;
int main()
{
int x;
cin>>x;
cout<<39<n 2的k-1次方<=n
x^=(int)(pow(2,k)-1);//A操作
cout<
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.
Neko has two integers aa and bb. His goal is to find a non-negative integer kk such that the least common multiple of a+ka+k and b+kb+k is the smallest possible. If there are multiple optimal integers kk, he needs to choose the smallest one.
Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it?
Input
The only line contains two integers aa and bb (1≤a,b≤1091≤a,b≤109).
Output
Print the smallest non-negative integer kk (k≥0k≥0) such that the lowest common multiple of a+ka+k and b+kb+k is the smallest possible.
If there are many possible integers kk giving the same value of the least common multiple, print the smallest one.
Examples
input
Copy
6 10
output
Copy
2
input
Copy
21 31
output
Copy
9
input
Copy
5 10
output
Copy
0
Note
In the first test, one should choose k=2k=2, as the least common multiple of 6+26+2 and 10+210+2 is 2424, which is the smallest least common multiple possible.
题意:
a,b同时加上一个非负整数k,使得,a+k,b+k的最小公倍数最小
因为最小公公倍数=x*y / gcd(x,y),所以肯定离不开最大公约数了;
首先有个结论 gcd(x,y)=gcd(x,y-x) (y>x)
令c=gcd(x,y),那么x%c=0,y%c=0,(y-x)%c=0,所以gcd(x,y)=gcd(x,y-x)
因为题目中d=x-y的值不会变,所以我们就可以通过枚举d的因子,来凑a+k (d的因子也是(a+k)的因子)
#include
using namespace std;
#define ll long long
ll a,b;
ll ans,lcm=0x3f3f3f3f3f3f3f3f;
int main()
{
cin>>a>>b;
ll d=abs(a-b);
for(ll i=1;i*i<=d;i++)
{
if(d%i==0)//枚举b-a的因数i
{
ll k=(i-a%i)%i;//把a凑成i的倍数需要+k
ll t=(a+k)*(b+k)/i;// a*b/gcd
if(t