1. n个犯人,m个省份, 如果相邻的2个犯人来自同一省份,则是不安全的,求不安全的个数。

正难则反,用全部的个数减去非法的个数,就是最后的答案。 m^n - m * (m - 1) ^ (n - 1).  这里的m,n很大,所以就是快速幂乘法。

 #include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e3 + ;
const int mod = ;
ll n, m;
ll pow(ll x, ll y) {
if(y == ) return ;
ll r = ;
ll b = x;
while(y) {
if(y & ) {
r = r * b % mod;
}
y >>= ;
b = b * b % mod;
}
return r;
}
void solve() {
cin >> m >> n;
//cout << m<< " " << n <<endl;
//cout << pow(m, n) << endl;
//cout << pow(m, n - 1) << endl;
ll res = ((pow(m, n) - pow(m - , n - ) * m) % mod + mod)%mod;
cout << res << endl;
} int main() {
freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
ios::sync_with_stdio();
cin.tie(); cout.tie();
solve();
return ;
}

2. 从n个数,选取一些数,使得能被m整除。

这种题,就是一般的套路, 搞一下余数, 然后就是0,1背包。注意一维数组优化,滚动数组, 取余。

 #include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e6 + ;
int n, m;
int a[maxn];
void yes() {
cout << "Yes" << endl;
}
void no() {
cout << "No" << endl;
}
int dp[][maxn];
void solve() {
scanf("%d%d", &n, &m);
int x, y;
for (int i = ; i < n; i++) {
scanf("%d", &x);
x = x % m;
a[x]++;
}
if(a[] > ) {
yes(); return;
} dp[][] = ;
vector<int> av;
for (int i = ; i <= m - ; i++) {
for (int j = ; j < a[i]; j++)
av.push_back(i);
//cout << i << endl;
}
//cout << "asd" << endl;
int cur = , nxt = ;
for (int tx : av) {
for (int i = m - ; i >= ; i--) {
if(dp[cur][i] > ) {
dp[nxt][i] = ;
int t = (i + tx) % m;
if(t == ) t = m;
dp[nxt][t] = ;
//cout << t << endl;
}
}
swap(cur, nxt);
for (int i = ; i < m; i++)
dp[nxt][i] = ;
//cout << tx << endl;
} if(dp[cur][m]) yes();
else no();
} int main() {
freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
solve();
return ;
}

3. 有一些操作,插入和查询,插入是往末尾进行插入,查找的是末尾长度为l的里面的最大值。

由于每次动态更新,区间不停的变换,我只想到线段树的做法,就写了线段树的。logn单点更新, logn区间查询。

 #include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 2e5 + ;
ll f[maxn * ];
int n;
int x, y;
ll v;
void bt(int o, int left, int right) {
}
void add(int o, int left, int right) {
if(left > right) return;
if(left == right && left == x) {
f[o] = v;
} else {
int mid = (left + right) / ;
if(x <= mid) add(o * , left, mid);
else add(o * + , mid + , right);
f[o] = max(f[o * ], f[o * + ]);
}
}
ll ask(int o, int left, int right) {
if(right < x || y < left) return ;
if(x <= left && right <= y) return f[o];
int mid = (left + right) / ;
ll ml, mr;ml = mr = ;
if(x <= mid) ml = ask(o * , left, mid);
if(y >= mid + ) mr = ask(o * + , mid + , right);
return max(ml, mr);
}
int m;
ll mod;
char ch[];
void solve() {
scanf("%d%lld", &m, &mod);
int p = ;
ll lst = , t;
for (int i = ; i < m; i++) {
//cout << i << endl;
scanf("%s%lld", ch, &t);
if(ch[] == 'I') {
t = (t + lst) % mod;
x = ++p; v = t;
add(, , m);
} else {
if(t == ) {
printf("0\n");
continue;
}
x = p - t + ;
y = p;
lst = ask(, , m);
printf("%lld\n", lst);
}
}
} int main() {
freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
solve();
return ;
}

4. 逆序对,现在有1-n,一共n个数, 求逆序对的个数为m的排列的个数。1 <= n,m <= 1000.

枚举,数个数,贪心的方式是不行的,那就是dp了。

枚举最大的一个数,看如何进行转移。dp[i][j] 代表长度为i, 逆序对的个数为j的个数。考虑从dp[i-1]转移过来,增加的逆序对的个数,就很容易写出来转移方程。

写出来之后,发现时间复杂度是n^3的,不满足要求, 然后用前缀和维护一下,使得0(1)的求取区间的和。使得复杂度降为n^2.

 #include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e3 + ;
const int mod = ;
int n, k;
int dp[maxn][maxn];
int s[maxn][maxn];
void solve() {
cin >> n >> k; for (int i = ; i <= n; i++) {
dp[i][] = ;
s[i][] = ;
for (int j = ; j <= k; j++) {
dp[i][j] = s[i - ][j];
if(j - i >= )
dp[i][j] = (dp[i][j] - s[i - ][j - i] + mod) % mod;
s[i][j] = (s[i][j - ] + dp[i][j]) % mod;
//cout << i << " " << j << " " << dp[i][j] << endl;
} }
cout << dp[n][k] << endl; } int main() {
freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
ios::sync_with_stdio();
cin.tie(); cout.tie(); solve();
return ;
}

wap 5.23 网测几道题目的更多相关文章

  1. C语言超级经典400道题目

    C语言超级经典400道题目 1.C语言程序的基本单位是____ A) 程序行 B) 语句 C) 函数 D) 字符.C.1 2.C语言程序的三种基本结构是____构A.顺序结构,选择结构,循环结 B.递 ...

  2. hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)

    HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包 ...

  3. 小白欢乐多——记ssctf的几道题目

    小白欢乐多--记ssctf的几道题目 二哥说过来自乌云,回归乌云.Web400来源于此,应当回归于此,有不足的地方欢迎指出. 0x00 Web200 先不急着提web400,让我们先来看看web200 ...

  4. 在 n 道题目中挑选一些使得所有人对题目的掌握情况不超过一半。

    Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...

  5. wap网测一道题目

    1. 给定一个字符串s, 1 <= len(s) <= 3000, 定义odd palindrome string为长度为奇数的回文串, 求s中该奇回文串的个数. 比如axbcba , 结 ...

  6. SQL的几道题目

    1.构造数据插入方案表t_project_finish表 a)将addtime更新为当前时间的前一天 首先想到的是addtime=addtime-1,然后就开始验证这个想法. 插入一行数据,包括主键和 ...

  7. C++面试出现频率最高的30道题目

    http://blog.csdn.net/wangshihui512/article/details/9092439 1.new.delete.malloc.free关系 delete会调用对象的析构 ...

  8. Java 递归 常见24道题目 总结

    1.N个台阶的走法递归[这里设为10个台阶] /** * N个台阶的走法递归 * <p> * 有个楼梯,台阶有10个,每次可以跳上1阶 或者 2阶 ,那么台阶的走法一共有多少种 */ @T ...

  9. codeforces 几道题目

    BZOJ挂了....明天就要出发去GDKOI了....不能弃疗. 于是在cf水了几道题, 写写详(jian)细(dan)题解, 攒攒RP, 希望GDKOI能好好发挥.......  620E. New ...

随机推荐

  1. CAD从二制流数据中加载图形(com接口VB语言)

    主要用到函数说明: _DMxDrawX::ReadBinStream 从二制流数据中加载图形,详细说明如下: 参数 说明 VARIANT varBinArray 二制流数据,是个byte数组 BSTR ...

  2. 在vue中通过js动态控制图片按比列缩放

    1.html 通过外层的div来给img对应的class,隐藏的img是得到img图片请求回来时的原始尺寸.外层div是固定大小,因此,图片有两种情况去适应外部div的尺寸.一种是宽度大于高度的情况, ...

  3. soui edit passwrod模式下禁用输入法

    一直在用soui做客户端界面,今天发现密码edit在中文输入法下不能输入密码.我在想难道不是这样吗,密码就该用英文输入法啊. 然后我就用mfc的做了个demo,发现mfc的edit在密码模式下是可以用 ...

  4. Win32 线程同步

    Win32 线程同步 ## Win32线程同步 ### 1. 原子锁 ### 2. 临界区 {全局变量} CRITICAL_SECTION CS = {0}; // 定义并初始化临界区结构体变量 {线 ...

  5. cin输入过慢用scanf???现在才知道cin可以加速

    今天才发现可以加速原帖 只需要两行放在main开头即可 ios_base::sync_with_stdio(0); cin.tie(NULL);

  6. [luogu2148 SDOI2009] E&D (博弈论)

    传送门 Solution 我们知道当SG不为0则先手必胜,然后就可以打表了 ̄▽ ̄ Code //By Menteur_Hxy #include <cmath> #include <c ...

  7. RESTful API - 介绍

    目录 RESTful API 介绍 RESTful介绍 RESTful API设计指南 API与用户的通信协议 域名 版本(Versioning) 路径(Endpoint) 浏览器请求方式(metho ...

  8. STM32学习笔记:读写内部Flash(介绍+附代码)

    一.介绍 首先我们需要了解一个内存映射: stm32的flash地址起始于0x0800 0000,结束地址是0x0800 0000加上芯片实际的flash大小,不同的芯片flash大小不同. RAM起 ...

  9. mode-c++

    /*感谢机房JYW的友情馈赠*/#include <iostream> #include <cstdio> #include <cstring> #include ...

  10. eclipse 执行MapReduce程序错误异常汇总(解决Map not fount)

    错误一: Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class wordCount.wordCount$ ...