题目 | 类型 |
---|---|
平方和 | 结果填空 |
数列求值 | 结果填空 |
最大降雨量 | 结果填空 |
迷宫 | 结果填空 |
RSA解密 | 结果填空 |
完全二叉树的权值 | 编程题 |
外卖店优先级 | 编程题 |
修改数组 | 编程题 |
糖果 | 编程题 |
组合数问题 | 编程题 |
#include
#include
using namespace std;
unsigned long long ans;
int main()
{
for (int i = 1; i <= 2019; i++)
{
string s = to_string(i);
for (int j = 0; j < s.length(); j++)
{
if (s[j] == '2' || s[j] == '0' || s[j] == '1' || s[j] == '9')
{
ans += i * i;
break;
}
}
}
cout << ans;
return 0;
}
#include
using namespace std;
int main()
{
int a1, a2, a3, a4;
a1 = a2 = a3 = 1;
for (int i = 4; i <= 20190324; i++)
{
a4 = a1 + a2 + a3;
a4 %= 10000;
a1 = a2;
a2 = a3;
a3 = a4;
}
cout << a4;
return 0;
}
本题总分:10分
问题描述
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。
010000
000100
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这个它的上、下、左、右四个方向之一。 01010101001011001001010110010110100100001000101010
对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫,一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。
对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。请注意在字典序中D
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个字符串,包含四种字母 D、U、L、R,在提交答案时只填写这个字符串,填写多余的内容将无法得分。
解析
#include
#include
#include
#include
using namespace std;
char map[32][52];//地图,存放0和1
bool vis[32][52];//是否访问过
int record[32][52];//记录如何到达(x, y)点
string s = "DLRU";//方向
int dx[] = {
1, 0, 0,-1 };
int dy[] = {
0,-1, 1, 0 };
struct node
{
int x;
int y;
};
void bfs()
{
queue<node>q;
node now, next;
now.x = 0;
now.y = 0;
vis[0][0] = 1;
q.push(now);
while (!q.empty())
{
now = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int x0 = now.x + dx[i];
int y0 = now.y + dy[i];
//如果在范围内,并且没有被访问过,且可以访问
if (x0 >= 0 && x0 < 30 && y0 >= 0 && y0 < 50 && !vis[x0][y0] && map[x0][y0] == '0')
{
next.x = x0;
next.y = y0;
vis[x0][y0] = 1;
q.push(next);
record[x0][y0] = i;//记录如何到达此点
}
}
}
//从出口开始向入口遍历记录,因此放在栈中,便于反向输出
stack<char> st;
int x = 29, y = 49;
while (x != 0 || y != 0)
{
st.push(s[record[x][y]]);
x = x - dx[record[x][y]];
y = y - dy[record[x][y]];
}
//输出
while (!st.empty())
{
cout << st.top();
st.pop();
}
}
int main()
{
for (int i = 0; i < 30; i++) cin >> map[i];
bfs();
return 0;
}
答案
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
//对n进行因式分解,得到其因子891234941和1123984201
#include
using namespace std;
long long n= 1001733993063167141;
long long d = 212353;
long long c = 20190324;
int main()
{
for (long long i = 2; i * i < n; i++)
{
if (n % i == 0) cout << i << " " << n / i;
}
return 0;
}
#include
using namespace std;
long long n = 1001733993063167141;
long long d = 212353;
long long c = 20190324;
long long p = 891234941;
long long q = 1123984201;
long long phi = (p - 1) * (q - 1);
//欧几里得算法求逆元
void Ex_gcd(long long a, long long b, long long& x, long long& y)
{
if (b == 0)
{
x = 1;
y = 0;
return;
}
long long x1, y1;
Ex_gcd(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
}
//快速乘求每次的余数
long long quickmul(long long a, long long b)
{
long long sum = 0;
while (b)
{
if (b % 2 == 1)
sum = (sum + a) % n;
a = (a + a) % n;
b = b / 2;
}
return sum;
}
//快速幂
long long quickmod(long long a, long long b)
{
long long ans = 1;
while (b)
{
if (b % 2 == 1)//末位是1;
ans = quickmul(ans, a);//这是直接的回溯法,从最后一位起,如果,如果最后一位是1,则乘a,然后在进行乘以它本身,以为乘1之后一定为偶数,也就是b/2;
a = quickmul(a, a);
b = b / 2;
}
return ans;
}
int main()
{
long long e, x, y;
Ex_gcd(d, (q - 1) * (p - 1), e, y);
e = (e + phi) % phi;
x = quickmod(c, e);
cout << x;
return 0;
}
引用:https://www.jianshu.com/p/ec919a5d4c90
#include
#include
using namespace std;
#define ll long long
ll sum;
int n, ans, deep;
int main()
{
cin >> n;
deep = 1;
//n个结点对应有log(2)(n+1)层
while (deep <= log(n+1)/log(2))
{
int num;
ll temp = 0;
//一层一层输入计算总和
for (int i = 0; i < pow(2, deep - 1); i++)
{
cin >> num;
temp += num;
}
//如果比目前的sum更大,则更新sum和层数
if (temp > sum)
{
sum = temp;
ans = deep;
}
deep++;
}
cout << ans;
return 0;
}
#include
#include
#include
using namespace std;
int store[100005];//用于存储商店的优先级
int ans;//用于存储答案
struct node
{
int ts;
int id;
}order[100005];//用于存储菜单数据
//按照ts进行排序
bool cmp(node a, node b)
{
if (a.ts < b.ts) return 1;
else return 0;
}
int main()
{
int n, m, t;
map<int, bool> map;
//输入
cin >> n >> m >> t;
for (int i = 0; i < m; i++) cin >> order[i].ts >> order[i].id;
//初始化map
for (int i = 0; i < n; i++) map[i] = false;
//按照时间进行排序
sort(order, order + m, cmp);
int pre = 0;
for (int i = 0; i < m; i++)
{
int now = order[i].ts;
int idx = order[i].id - 1;
store[idx] -= now - pre;
if (store[idx] < 0) store[idx] = 0;
store[idx] += 2;
if (store[idx] >= 5) map[idx] = true;
else map[idx] = false;
pre = now;
}
//筛选
for (int i = 0; i < n; i++)
{
if (map[i] == true) ans++;
}
cout << ans;
return 0;
}
#include
using namespace std;
int a[100005];
bool vis[1000005];
int main()
{
int n, num;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> num;
if (vis[num] == 0)
{
a[i] = num;
vis[num] = 1;
}
else
{
while (vis[num] == 1)
{
num++;
}
a[i] = num;
vis[num] = 1;
}
}
for (int i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
#include
#include
#include
#include
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
#define Mst(a,b) memset(a,(b),sizeof(a))
using namespace std;
int dp[1 << 20]; //二进制状态压缩
int a[105];
int main()
{
//freopen("in.txt","r",stdin);
int m, n, k;
while (scanf("%d%d%d", &n, &m, &k) != EOF)
{
set<int> v; //记录n包中所有糖果的种类数
memset(dp, 0x3f, sizeof(int) * (1 << m)); //初始化
dp[0] = 0;
For(i, 1, n)
{
int x = 0, y;
For(j, 1, k)
{
scanf("%d", &y);
--y;
v.insert(y);
x |= (1 << y);
}
a[i] = x; //x的二进制形式第i位为1代表这个集合中有第i+1种糖果
dp[x] = 1;//这种状态只需要第i包糖果1包就可以达到
}
if ((int)v.size() < m) //把n包唐都买了也吃不到m中糖果
{
printf("-1\n"); continue;
}
int ed = (1 << m) - 1; //目标状态
For(i, 1, n)
{
For(state, 0, ed)
{
if (dp[state] > 200) continue; //用已有的状态去更新加上第i包糖果后的状态
int to = a[i] | state;
dp[to] = min(dp[to], 1 + dp[state]);
}
}
printf("%d\n", dp[ed]);
}
return 0;
}
引用:https://blog.csdn.net/qq_40531479/article/details/89041933
#include
#include
using namespace std;
#define ll long long
ll mod = 1000000007;
ll mat[1000][1000];
ll Rcombinat(ll m, ll n)
{
if (mat[m][n] != 0)
return mat[m][n];
if (n == 0 || m == n)
{
mat[m][n] = 1;
return 1;
}
mat[m - 1][n - 1] = Rcombinat(m - 1, n - 1);
mat[m - 1][n] = Rcombinat(m - 1, n);
mat[m][n] = mat[m - 1][n - 1] + mat[m - 1][n];
return mat[m][n];
}
int main()
{
ll t, k;
cin >> t >> k;
while (t--)
{
ll n, m, ans = 0;
cin >> n >> m;
for (ll i = 1; i <= n; i++)
{
for (ll j = 0; j <= min(i, m); j++)
{
memset(mat, 0, sizeof(mat));
ll res = Rcombinat(i, j);
if (res % k == 0) ans++;
}
}
cout << ans%mod << endl;
}
return 0;
}