C. Fishingprince Plays With Array(二维数组,思维)

原文链接

Fishingprince is playing with an array [a1,a2,…,an][a1,a2,…,an]. He also has a magic number mm.

He can do the following two operations on it:

  • Select 1≤i≤n1≤i≤n such that aiai is divisible by mm (that is, there exists an integer tt such that m⋅t=aim⋅t=ai). Replace aiai with mm copies of aimaim. The order of the other elements doesn't change. For example, when m=2m=2 and a=[2,3]a=[2,3] and i=1i=1, aa changes into [1,1,3][1,1,3].
  • Select 1≤i≤n−m+11≤i≤n−m+1 such that ai=ai+1=⋯=ai+m−1ai=ai+1=⋯=ai+m−1. Replace these mm elements with a single m⋅aim⋅ai. The order of the other elements doesn't change. For example, when m=2m=2 and a=[3,2,2,3]a=[3,2,2,3] and i=2i=2, aa changes into [3,4,3][3,4,3].

Note that the array length might change during the process. The value of nn above is defined as the current length of the array (might differ from the nn in the input).

Fishingprince has another array [b1,b2,…,bk][b1,b2,…,bk]. Please determine if he can turn aa into bb using any number (possibly zero) of operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n≤5⋅1041≤n≤5⋅104, 2≤m≤1092≤m≤109).

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

The third line of each test case contains one integer kk (1≤k≤5⋅1041≤k≤5⋅104).

The fourth line of each test case contains kk integers b1,b2,…,bkb1,b2,…,bk (1≤bi≤1091≤bi≤109).

It is guaranteed that the sum of n+kn+k over all test cases does not exceed 2⋅1052⋅105.

Output

For each testcase, print Yes if it is possible to turn aa into bb, and No otherwise. You can print each letter in any case (upper or lower).

Example

input

Copy

5
5 2
1 2 2 4 2
4
1 4 4 2
6 2
1 2 2 8 2 2
2
1 16
8 3
3 3 3 3 3 3 3 3
4
6 6 6 6
8 3
3 9 6 3 12 12 36 12
16
9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4
8 3
3 9 6 3 12 12 36 12
7
12 2 4 3 4 12 56

output

Copy

Yes
Yes
No
Yes
No

Note

In the first test case of the sample, we can do the second operation with i=2i=2: [1,2,2,4,2]→[1,4,4,2][1,2,2,4,2]→[1,4,4,2].

In the second testcase of the sample, we can:

  • do the second operation with i=2i=2: [1,2,2,8,2,2]→[1,4,8,2,2][1,2,2,8,2,2]→[1,4,8,2,2].
  • do the second operation with i=4i=4: [1,4,8,2,2]→[1,4,8,4][1,4,8,2,2]→[1,4,8,4].
  • do the first operation with i=3i=3: [1,4,8,4]→[1,4,4,4,4][1,4,8,4]→[1,4,4,4,4].
  • do the second operation with i=2i=2: [1,4,4,4,4]→[1,8,4,4][1,4,4,4,4]→[1,8,4,4].
  • do the second operation with i=3i=3: [1,8,4,4]→[1,8,8][1,8,4,4]→[1,8,8].
  • do the second operation with i=2i=2: [1,8,8]→[1,16][1,8,8]→[1,16].、

思路:

这题都变成最小单元即可

代码;

#include<bits/stdc++.h>
using namespace std;
#define int long long
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")//
const int maxj=2e5+100,mod=998244353;
void solve(){
    //操作一二,太灵活,但是a,b是互逆的,都变成最小的
    int n,m;cin>>n>>m;
    vector<pair<int,int>>a,b;//一维存个数,一维存最小数
    for(int i=1;i<=n;++i){
        int x;cin>>x;
        int cnt=1;
        while(x%m==0){//cnt存多少个,x存最小单元
            cnt*=m,x/=m;
        }
        if(a.empty()||a.back().first!=x){
            a.push_back({x,cnt});
        }else{
            a.back().second+=cnt;
        }
    }int k;
    cin>>k;
    for(int i=1;i<=k;++i){
        int x;cin>>x;
        int cnt=1;
        while(x%m==0){
            cnt*=m,x/=m;
        }
        if(b.empty()||b.back().first!=x){
            b.push_back({x,cnt});
        }else{
            b.back().second+=cnt;
        }
    }if(a==b){
        cout<<"Yes"<<'\n';
    }else{
        cout<<"No"<<'\n';
    }
}
int32_t main(){
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JAVA使用可变长度数组设计一个类型,提供如下方法 提示 要统计每个单词出现的次数,由于一个方法不能返回2种类型,我们需要把单词和它的出现次数封装到一个类中 去,所以,可以定义一个类型如下: 由于我们统计的有多个单词,所以,我们上面的 countSize 方法的返回类型就可以设计成 WordBean[],如下: public class PatternDemo { //此方法用来统计 content 中的英文单词个数, 请使用正则表达式来做,单词的正则表达式请自行编写, public int countWords(CharSequence content) { ... } //此方法返回一串固定的字符串,已写好,可以直接用。 public StringBuilder getContent() { //此方法的内容如下: StringBuilder builder = new StringBuilder(); builder.append("Hooray! It's snowing! It's time to make a snowman.James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons.In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels.A moment later, the snowman takes James's hand and goes out.They go up, up, up into the air! They are flying! What a wonderful night!The next morning, James jumps out of bed. He runs to the door.He wants to thank the snowman. But he's gone."); // return builder; } //此方法统计出每个单词[不考虑大小写]出现的次数,数据结构请自行定义,设计如下: public ? countSize(CharSequence content) { //TODO ... } //注:? 处是你需要去思考,该设计什么结构来存放结果 } public class WordBean { //属性 private String word; //单词 private int count; //出次 //TODO 请自行完成构造、getter/setter、toString、等相关方法 } public WordBean[] countSize(CharSequence content) { //TODO ... } 最后写一个调用者类,来测试你的实现,如下: public class UsePatternDemo() { public static void main(String[] args) { //TODO ... }
06-01
好的,根据你的要求,我来为你解答。这里给出一个参考实现: ```java import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternDemo { //此方法用来统计 content 中的英文单词个数, 请使用正则表达式来做,单词的正则表达式请自行编写, public int countWords(CharSequence content) { String regex = "\\b[a-zA-Z]+\\b"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(content); int count = 0; while (matcher.find()) { count++; } return count; } //此方法返回一串固定的字符串,已写好,可以直接用。 public StringBuilder getContent() { StringBuilder builder = new StringBuilder(); builder.append("Hooray! It's snowing! It's time to make a snowman.James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons.In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels.A moment later, the snowman takes James's hand and goes out.They go up, up, up into the air! They are flying! What a wonderful night!The next morning, James jumps out of bed. He runs to the door.He wants to thank the snowman. But he's gone."); return builder; } //此方法统计出每个单词[不考虑大小写]出现的次数 public WordBean[] countSize(CharSequence content) { String regex = "\\b[a-zA-Z]+\\b"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(content); Map<String, Integer> map = new HashMap<>(); while (matcher.find()) { String word = matcher.group().toLowerCase(); if (map.containsKey(word)) { int count = map.get(word); map.put(word, count + 1); } else { map.put(word, 1); } } WordBean[] result = new WordBean[map.size()]; int i = 0; for (Map.Entry<String, Integer> entry : map.entrySet()) { String word = entry.getKey(); int count = entry.getValue(); WordBean bean = new WordBean(word, count); result[i++] = bean; } Arrays.sort(result); return result; } } class WordBean implements Comparable<WordBean> { private String word; private int count; public WordBean(String word, int count) { this.word = word; this.count = count; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } @Override public String toString() { return word + ": " + count; } @Override public int compareTo(WordBean o) { return Integer.compare(o.count, this.count); } } class UsePatternDemo { public static void main(String[] args) { PatternDemo demo = new PatternDemo(); StringBuilder content = demo.getContent(); WordBean[] result = demo.countSize(content); for (WordBean bean : result) { System.out.println(bean); } } } ``` 在这个实现中,我们首先使用正则表达式统计了一个字符串中的英文单词个数。然后,我们定义了一个 `WordBean` 类来封装单词和它的出现次数。接着,我们使用 `Map` 来存储每个单词出现的次数,并将结果封装到 `WordBean` 数组中返回。最后,我们对结果进行了按出现次数降序排序,并在测试类中进行了输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值