A. Neko Finds Grapes
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:
- Use first key to unlock the fifth chest,
- Use third key to unlock the second chest,
- Use fourth key to unlock the first chest.
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<bits/stdc++.h>
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<n;i++)
{
cin>>a[i];
if(a[i]&1)
Count1++;//a[i]奇數個數
}
for(int i=0;i<m;i++)
{
cin>>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<<sum<<endl;
}
B. Neko Performs Cat Furrier Transform
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:
- (Operation A): you select any non-negative integer nn and replace xx with x⊕(2n−1)x⊕(2n−1), with ⊕⊕ being a bitwise XOR operator.
- (Operation B): replace xx with x+1x+1.
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:
- Pick n=5n=5. xx is transformed into 39⊕3139⊕31, or 5656.
- Increase xx by 11, changing its value to 5757.
- Pick n=3n=3. xx is transformed into 57⊕757⊕7, or 6262.
- Increase xx by 11, changing its value to 63=26−163=26−1.
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<bits/stdc++.h>
using namespace std;
int main()
{
int x;
cin>>x;
cout<<39<<endl;
int num=19;
int k;
while(num--)
{
k=log2(x)+1; //2的k次方>n 2的k-1次方<=n
x^=(int)(pow(2,k)-1);//A操作
cout<<k<<' ';
x++; //B操作
}
cout<<log2(x)+1;
}
C. Neko does Maths
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<bits/stdc++.h>
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<lcm)
{
lcm=t;
ans=k;
}
ll ii=d/i;
k=(ii-a%ii)%ii;
t=(a+k)*(b+k)/ii;
if(t<lcm)
{
lcm=t;
ans=k;
}
}
}
cout<<ans<<endl;
}