2022-04-27每日刷题打卡

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;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值