Description
Do you know the problem “Divisible” in SCAUCPC 2013? No? It does not matter. Today, I will introduce a similar thing to you.
You are given N numbers to multiply into a single number A, and another M numbers into number B.
However, because WJMKQDM does not like large number, the absolute values of each sub-number of A or B, which means Ai or Bi, will not larger than three.
Your task is extremely simple, that is to decide whether A is equal to B.
To make the problem a little more technical, you should notice that N and M will be a little large.
输入格式
First line of input is the number of test cases, less than 30.
In each case, there will be 2 line. The first line of each case represents number A in such format:
N A1 A2 … AN, where A = A1 * A2 * … * AN.
The second line, similarly, number B will be represent like this:
M B1 B2 … BM, where B = B1 * B2 * … * BM.
(1 <= N, M <= 100000)
输出格式
For each test case, output one line “Yes” or “No”(without quotation), corresponding to equality or inequality.
输入样例
2
2 2 3
3 3 2 1
3 1 2 3
2 2 2
输出样例
Yes
No
提示
Huge input, and scanf strongly recommended.
题意:给两个数组,判断各自累乘是否相等,元素绝对值不超过3 |
思路:
1.天真地累乘肯定不行,n上限1e5,要是有这么多个3,那肯定爆long long了爆上天了。
2.想两组数逐个相除,看结果是否为1,这也不行。因为最坏的情况是一边是全是3一边全是1,还是会爆。
3.其实发现上面方法中,分式里,分子分母中的2和3肯定要和自己才能约分。也就是说2n = 3m无解。所以,只需要统计两个数组中的2和3的个数是否相等即可。1不用管,对乘数大小无贡献
4.还有一个恶心人的地方就是还有负数。。。这个时候把负数转正,同上。最后看看两边的负数个数是否同奇同偶即可。
AC代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll cnt[5][2];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n,m;
n = read();
int flag1 = 0; int flag2 = 0;
mem(cnt,0);
ll cnt1 = 0, cnt2 = 0;
rep(i,1,n)
{
ll x = read();
if(!x) flag1 = 1;
if(x<0) cnt1++;
x = abs(x);
cnt[x][0] ++;
}
m = read();
rep(i,1,m)
{
ll y = read();
if(!y) flag2 = 1;
if(y<0) cnt2++;
y = abs(y);
cnt[y][1] ++;
}
if(flag1&&flag2)
cout<<"Yes"<<'\n';
else if(flag1&&!flag2||!flag1&&flag2) cout<<"No"<<'\n';
else
{
int flag = 1;
rep(i,2,3)
{
if(cnt[i][0]!=cnt[i][1]||(cnt1&1)!=(cnt2&1)) flag = 0;
}
if(flag) cout<<"Yes"<<'\n';
else cout<<"No"<<'\n';
}
}
return 0;
}