2022-04-27每日刷题打卡

代码源——每日一题

素数之欢 - 题目 - Daimayuan Online Judge

现给定两个 四位素数 a,b。 你可以执行多次下面的操作:

修改数字 a 的某一位, 使其成为另一个 四位素数。

例如,1033→1733,其中 1033 与 1733 均为素数。

问至少多少次变换后能从 a 得到 b ? 或回答不可能。

数据规模

多组数据 1≤T≤100

输入格式

第一行一个数字 T,表示接下来将会有 T 组数据。

接下来包含 T 行,每行包含用空格分开的两个 四位素数 a,b。

输出格式

输出 T 行,如果可以,输出最小变换次数。反之输出 −1。

样例输入

2
1033 1033
1033 8779

样例输出

0
5

说明

1033→1733→3733→3739→3779→8779

直接暴力bfs搜就可以,题目说了就4位数,我们每次修改4个位置上的数,用0~9替换,替换后判断是否是质数即可,然后计算变换到b一共需要几步。

为了防止t,我们可以先把所有四位数的质数都求出来,这样修改一个位置上的数后,就可以通过直接判断一个数是否在质数集里来判断它是不是质数了,顺便记录一下已经变换过的数,这样下次遇到这个数的时候我们直接不理他即可。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>#define endl '\n';
typedef long long ll;
typedef pair<ll, ll>PII;
const int N = 2e6 + 50;
int f[100000], a[N];
unordered_map<int, int>mymap;void get_p()
{for (int i = 1000; i <= 9999; i++){bool flag = true;for (int j = 2; j * j <= i; j++){if (i % j == 0){flag = false;break;}}if (flag){mymap[i] = 1;}}
}int main()
{int t;cin >> t;get_p();while (t--){int a, b;cin >> a >> b;if (a == b){cout << 0 << endl;continue;}queue<int>que;que.push(a);memset(f, 0, sizeof f);int res = 1;bool flag = false;while (!que.empty()){int len = que.size();for (int i = 0; i < len; i++){int num = que.front();que.pop();for (int j = 10; j <= 10000; j*=10){int l = num / j, r = num % (j/10);for (int k = 0; k <= 9; k++){int ans = l * j + k*(j/10) + r ;if (ans == b){flag = true;break;}if (f[ans] == 0 && mymap[ans] == 1){f[ans] = 1;que.push(ans);}}if (flag)break;}if (flag)break;}if (flag)break;res++;}cout << res << endl;}return 0;
}

CodeForces

Problem - M. Bottle Arrangements

Gabriella has been instructed to organize a renowned wine tasting event which will be attended by m critics. On display, there will be n different varieties of wine, each of which can either be a red wine or a white wine.

The wines will come in n bottles arranged in a line on the table, and, for convenience, each critic will sip from a contiguous interval of bottles: that is, he![img](file:///C:\Users\Aoxue\AppData\Roaming\Tencent\QQTempSys\VJF%JZ05B[YH0[V%7ZB2[N.gif)e will taste exactly the bottles at position a,a+1,…,b for some 1≤a≤b≤n. The interval depends on the critic, who will select it on the spot according to their preferences. In fact, the i-th critic (1≤i≤m) has requested that he![img](file:///C:\Users\Aoxue\AppData\Roaming\Tencent\QQTempSys\VJF%JZ05B[YH0[V%7ZB2[N.gif)e wants to taste exactly ri red wines and wi white wines.

Gabriella has yet to choose how many bottles of red wine and white wine there will be, and in what order they will appear. Help her find an arrangement (that is, a sequence of n bottles of either red or white wine) that satisfies the requests of all the critics, or state that no such arrangement exists.

Input

Each test contains multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases. The descriptions of the t test cases follow.

The first line of each test case contains two integers n and m (1≤n≤100, 1≤m≤100) — the number of bottles of wine and the number of critics.

Each of the next m lines contains two integers ri and wi (0≤ri,wi≤100, ri+wi≥1) — the number of red and white wines that the i-th critic wants to taste.

Output

For each test case, if at least one solution exists, print a string of length n made up of the characters R and W, where the j-th character (1≤j≤n) denotes the type of the wine in the j-th bottle of the arrangement (R for red and W for white). If there are multiple solutions, print any.

If no solution exists, print the string IMPOSSIBLE.

Example

input

3
5 3
1 0
3 2
2 2
4 3
2 1
1 1
0 3
3 2
0 2
0 3

output

RWRRW
IMPOSSIBLE
WWW

Note

In the first test case, there are n=5 bottles of wine to be arranged and m=3 critics. The arrangement RWRRW satisfies the requests of all three critics. Indeed:

  • the first critic can choose the interval [3,3], which contains exactly one bottle of red wine (note that [1,1] and [4,4] are other valid choices);
  • the second critic can choose the interval [1,5], which contains 33 bottles of red wine and 22 bottles of white wine;
  • the third critic can choose the interval [2,5], which contains 22 bottles of red wine and 22 bottles of white wine.

这题是说,有n瓶酒,这些酒有两种品种,白或红,有多少红多少白你决定反着两者加一起要等于n,有m个人来喝酒,他们只喝一个区间内(这个区间你定)的酒,这个区间要有a个红,b个白,问你能不能把这些酒按顺序摆好,使得满足所有人的要求,不能就输出impossible,能就输出任意一种方法。

先记录下每个人要和的红酒最大数量,和白酒最大数量,如果这两个数量相加大于n,那肯定不能满足所有人。不然,我们直接把这些酒挨近摆就可以了,前面都是白后面都是红,或者前面都是红后面都是白也可以,这样必然能满足所有人要求。要注意,这两个值相加不一定等于n,所有我们摆的时候,某个品种要多摆一点,直到等于n。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>#define endl '\n';
typedef long long ll;
typedef pair<ll, ll>PII;
const int N = 1e6 + 50;int main()
{int t;cin >> t;while (t--){map<int, PII>mymap;ll n, m, a = 0, b = 0;cin>>n>>m;vector<PII>v(m);for (int i = 0; i < m; i++){cin >> v[i].first >> v[i].second;a = max(a, v[i].first);b = max(b, v[i].second);mymap[v[i].first + v[i].second] = v[i];}if (a + b > n){cout << "IMPOSSIBLE" << endl;continue;}string s;for (int i = 0; i < a; i++)s += 'R';for (int i = 0; i < b; i++)s += 'W';for (int i = s.size(); i < n; i++)s += 'W';cout << s << endl;}return 0;
}

2022-04-27每日刷题打卡相关推荐

  1. 2022.10.14每日刷题打卡

    リモコン 题意: 题目描述: 高桥君要调整空调的设定温度.现在的设定温度是A度,而他想调到B度. 空调遥控器按一次可以: 上调或下调1度 上调或下调5度 上调或下调10度 高桥君想求出从A调到B度的最 ...

  2. 2022.11.14每日刷题打卡

    过山车 原题链接:传送门 二分图最大匹配模板题,但sb了数组开小了一直TLE,虽然是模板但很长教训. #include <bits/stdc++.h> using namespace st ...

  3. 2021-11-15每日刷题打卡

    2021-11-15每日刷题打卡 AcWing--算法基础 AcWing 794. 高精度除法 - AcWing 给定两个非负整数(不含前导 00) A,B,请你计算 A/B 的商和余数. 输入格式 ...

  4. 2022-04-14每日刷题打卡

    2022-04-14每日刷题打卡 代码源--每日一题 上帝的集合 - 题目 - Daimayuan Online Judge 题目描述 现在上帝有一个空集合,现在他命令你为他执行下列三种操作 n 次, ...

  5. 2022-04-01每日刷题打卡

    2022-04-01每日刷题打卡 代码源--每日一题 Lusir的游戏 - 题目 - Daimayuan Online Judge Lusir 正在玩一个古老的基于 DOS 的游戏. 游戏中有 N+1 ...

  6. 2021-12-11每日刷题打卡

    2021-12-11每日刷题打卡 力扣--剑指offer 剑指 Offer 40. 最小的k个数 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字, ...

  7. 2022-03-10每日刷题打卡

    2022-03-10每日刷题打卡 力扣--每日一题 589. N 叉树的前序遍历 给定一个 n 叉树的根节点 root ,返回 其节点值的 前序遍历 . n 叉树 在输入中按层序遍历进行序列化表示,每 ...

  8. 2022-03-02每日刷题打卡

    2022-03-02每日刷题打卡 代码源--div2每日一题 Alice的德州扑克 - 题目 - Daimayuan Online Judge 德州扑克是目前世界上最流行的扑克游戏,全世界有众多相关的 ...

  9. 2022-03-03每日刷题打卡

    2022-03-03每日刷题打卡 力扣--每日一题 258. 各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数.返回这个结果. 示例 1: 输入: num = 38 输出 ...

  10. 2022-03-31每日刷题打卡

    2022-03-31每日刷题打卡 代码源--每日一题 完美数 - 题目 - Daimayuan Online Judge 对于给定的数字 a , b ,当整数 n 在十进制下的所有数位都为 a 或 b ...

最新文章

  1. 四大运营商的5G部署路线一览
  2. svn +nginx
  3. 文本编辑器实现拖放功能
  4. python生成一定范围的符合正态分布的数
  5. 配置阿里巴巴的数据源
  6. 关于web工程项目路径的一些说明
  7. 06 ansible剧本功能实践介绍
  8. linux 80中断,在64位Linux上使用中断0x80
  9. linux 切换pip路径_windows环境下面如何快速配置pip环境变量
  10. linux拷贝文件夹到另一台机器,linux肿么一个文件拷贝到另一个文件夹
  11. 解决XP中CPU占用率过高问题, 关了445这个端口(ZT)
  12. 2021中国华录杯·算法大赛直通车!
  13. 人生七年 全系列 英文版(中文字幕)
  14. 路由器桥接成功后,仍然没有网络
  15. 怎么从扫描的PDF文档/图片里提取文字
  16. python特征数据类型及常用操作对比总结_如何全面解析数据并创造数据故事
  17. SQL Server 2008 导出、导入包含数据的脚本 保存脚本法
  18. [电脑]电脑面前的自我保护
  19. 德国计算机课程匹配度,为什么德国大学就这么看重本科的课程匹配?
  20. 闪兼云带你游历互联网网赚的不同时代

热门文章

  1. 华为,今年第一位天才少年诞生
  2. PermissionError: [Errno 13] Permission denied 如何解决
  3. Sonar Qube安装
  4. Oozie Namenode/jobTracker not allowed, not in Oozies Whitelist
  5. 什么是PaaS平台 ?什么是IaaS平台?什么是SaaS平台?
  6. 线性模型|| 线性回归(Linear Regression)
  7. BSOJ 1480 贪吃的九头龙
  8. nfs服务配置为固定端口
  9. [C题目]九九乘法口诀表
  10. 如何下载生物数据(三):GATK数据下载