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在网页中打印的图纸里面添加页眉及页脚

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  2. Python学习【第6篇】:Python之常用模块1

    常用模块一. collocations 模块 时间模块 random模块 os模块 sys模块 序列化模块 re模块 常用模块二:这些模块和面向对象有关 hashlib模块 configparse模块 ...

  3. 网络:NAT使用场景

    NAT:Network Address Translation  网络地址转换 使用场景:家庭局域网,公司局域网的网络设备没有公网IP地址如何访问互联网? 简单图示: 理解一些原理: 1,互联网中网络 ...

  4. ubuntu系统中java开发环境的搭建

    Java环境可选择 Oracle 的 JDK,或是 OpenJDK,按http://wiki.apache.org/hadoop/HadoopJavaVersions中说的,新版本在 OpenJDK ...

  5. 发现:Click事件也能获取鼠标单击的坐标

    按照MSDN的说明以及平时的习惯,我们要获取鼠标单击时的相对坐标,都会使用MouseClick等事件,今天,偶然发现,原来Click事件也可以. /* 惊天地泣鬼神的考古业绩. * 原来Cilck事件 ...

  6. Eclipse集成Maven的Web工程demo(独立及Maven集成tomcat)

    用到的工具JDK1.8Eclipse Luna j2eeEclipse 集成的Mavetomcat7 (集成在xampp中的独立web服务器,或者通过Maven plugin集成web服务器) 步骤如 ...

  7. springcloud(三):Eureka服务端

    一. 因为使用一个注册中心服务器端,n个客户端:n个生产者客户端.n消费者客户端....,所有的客户端最好的方式就是通过对象传递参数,因此需要创建一个公共组件项目,为n个客户端传值提供方便 二.创建公 ...

  8. 利用Calendar类测试电脑运行速度

    今天学习了很多新知识! 这里使用了Calender类来获取系统时间,并计算循环1w次的时间,判断电脑处理时间. import java.util.Calendar; public class Arra ...

  9. ZooKeeper可视化Web管理工具收集(待实践)

    原来ZooKeeper是有Web管理后台的.但是仅限于操作ZooKeeper的数据,如果要监控性能,估计要借助Nagios去配合. 这些工具应该ZK UI最好用,下面是收集的一些工具安装教程: htt ...

  10. Codeforces Round #305 (Div. 2) D题 (线段树+RMQ)

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...