Ad Infinitum 8 - Math Programming Contest
A题
如果当前数是1,那么后面无论是几都会加1或者当后面数是1的时候加2,所以记录一下后面的数中1的个数(加2)即可。
如果当前数是2,那么只有当后面的数为1或者为2时才可以加1,所以再记录一下后面数中2的个数即可。
#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int a[], t, n, num1[], num2[]; int main() {
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
cin >> n;
for (int i = ; i <= n; i++) cin >> a[i];
memset(num1, , sizeof(num1));
memset(num2, , sizeof(num2));
for (int i = n; i >= ; i--) {
num1[i] = num1[i + ];
num2[i] = num2[i + ];
if (a[i] == ) num1[i]++;
else if (a[i] == ) num2[i]++;
}
long long res = ;
for (int i = ; i <= n; i++) {
if (a[i] == ) {
res += n - i + num1[i + ];
} else if (a[i] == ) {
res += num1[i + ] + num2[i + ];
} else {
res += num1[i + ];
}
}
cout << res << endl;
}
return ;
}
B题
这题需要敏锐的洞察力啊,起码对我来说是这样!
手动向后推两项就可以发现,当N为2时,S = (a1 + 1) * (a2 + 1) - 1;
当N为3时,S = (a1 + 1) * (a2 + 1) * (a3 + 1) - 1
可以发现,一个序列的S值和其顺序无关,只和数有关。
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; const int MOD = 1e9 + ;
const int MAX_N = ;
const int MAX_V = ;
typedef long long LL;
#define rep(i, n) for (int i = (1); i <= (n); i++) int main() {
ios::sync_with_stdio(false);
int N;
LL res = ;
cin >> N;
rep (i, N) {
int x;
cin >> x;
res = (res + x + res * x) % MOD;
}
cout << res << endl;
return ;
}
C题
很裸的求逆,快速幂。
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; int extgcd(int a, int b, int &x, int &y) {
int d = a;
if (b != ) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = ; y = ;
}
return d;
} int mod_inverse(int a, int m) {
int x, y;
extgcd(a, m, x, y);
return (m + x % m) % m;
} int mod_pow(int a, int b, int m) {
long long res = ;
while (b) {
if (b & ) res = res * a % m;
a = ((long long)a * a) % m;
b >>= ;
}
return res;
} int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int a, b, x;
cin >> a >> b >> x;
if (b < ) {
a = mod_inverse(a, x);
b = -b;
}
cout << mod_pow(a, b, x) << endl;
}
return ;
}
D题
容斥原理模板题
#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
const int MAX_N = ;
int N;
int num[MAX_N];
LL n, ans;
LL gcd(LL a, LL b){
LL r;
while( b ){
r = a%b;
a = b;
b = r;
}
return a;
} void dfs( int id, LL Lcm, bool flag ){
Lcm = Lcm*num[ id ] / gcd( Lcm,num[ id ] );
if( flag ) ans += n/Lcm;
else ans -= n/Lcm;
for( int i=id+;i<= N;i++ ){
dfs( i,Lcm,!flag );
}
return;
} int main() {
ios::sync_with_stdio(false);
cin >> N;
for (int i = ; i <= N; i++) cin >> num[i];
int D;
cin >> D;
for (int i = ; i <= D; i++) {
LL l, r;
cin >> l >> r;
LL ansr = , ansl = ;
ans = ;
n = r;
for (int i = ; i <= N; i++) dfs(i, num[i], true);
ansr = ans;
ans = ;
n = l - ;
for (int i = ; i <= N; i++) dfs(i, num[i], true);
ansl = ans;
cout << ansr - ansl << endl;
}
return ;
}
E题
从难度来看这题算是简单的规律题。。可是比赛中却没敢去写。
通过数列的前几项可以很自然的和fib数联系在一起。我们需要做的就是统计每个二进制位上的值。
通过本题学习了bitset的使用,很好用,支持两个bitset间进行位运算,很方便。
#include <cmath>
#include <bitset>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; const int MOD = 1e9 + ;
const int MAX_N = ;
typedef long long LL;
#define rep(i, n) for (int i = (1); i <= (n); i++)
const int NUM = ;
LL fib[NUM], sum[NUM]; LL mod_pow(LL a, LL b) {
LL res = ;
while (b) {
if (b & ) res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= ;
}
return res;
} bitset<NUM> solve(LL x) {
bitset<NUM> res;
while (x) {
int pos = upper_bound(sum, sum + NUM, x) - sum - ;
x -= sum[pos];
res.set(pos);
}
return res;
} int main() {
fib[] = ; fib[] = ;
sum[] = ; sum[] = ;
for (int i = ; i < NUM; i++) {
fib[i] = fib[i - ] + fib[i - ];
sum[i] = sum[i - ] + fib[i];
} ios_base::sync_with_stdio(false);
int N;
cin >> N;
bitset<NUM> res;
rep (i, N) {
LL x;
cin >> x;
res ^= solve(x);
}
LL ans = ;
for (int i = ; i < res.size(); i++) if (res.test(i)) ans = (ans + mod_pow(, i)) % MOD;
cout << ans << endl;
return ;
}
F题
待续。。
G题
待续。。
H题
待续。。
Ad Infinitum 8 - Math Programming Contest的更多相关文章
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第二部分)
Floor Function Time Limit: 10 Seconds Memory Limit: 65536 KB a, b, c and d are all positive int ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5503 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5502 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest May Day Holiday
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5500 The 12th Zhejiang Provincial ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5504 The 12th Zhejiang Provincial ...
随机推荐
- CF459E Pashmak and Graph (Dag dp)
传送门 解题思路 \(dag\)上\(dp\),首先要按照边权排序,然后图都不用建直接\(dp\)就行了.注意边权相等的要一起处理,具体来讲就是要开一个辅助数组\(g[i]\),来避免同层转移. 代码 ...
- 尚学linux课程---5、linux操作系统介绍
尚学linux课程---5.linux操作系统介绍 一.总结 一句话总结: centos开源免费,用的特别多 1.库是什么意思? 没有执行入口的应用程序 2.linux和window下的动态库文件是什 ...
- (一)PHP基础知识考察点
1,PHP引用变量的考察点: 概念:引用就是用不同的名字访问同一个变量内容. 定义方式: 使用&符号. PHP引用变量的工作原理 这里有个COW copy on write 用zval() ...
- 在ubuntu下编写python
一般情况下,ubuntu已经安装了python,打开终端,直接输入python,即可进行python编写. 默认为python2 如果想写python3,在终端输入python3即可. 如果需要执行大 ...
- 普通的maven项目变成web项目
command+: 或者 这个修改同样可以解决idea中不能新建servlet的问题. 这里最后的目录结构是这样的,如果在上面的设置中尝试修改目录,会导致无法创建servlet,比如我希望将根目录改成 ...
- 按指定规则统计list中数据,groupby用法
有的情况下,只是想要简单地对list中数据,进行分组,查看,可以考虑使用groupby 代码: # groupby需要排序后才能使用 def gb(num): if 0 <= num < ...
- selenium基础(鼠标和键盘事件)
selenium鼠标和键盘的操作事件 webdriver常见的几种操作方法 clear():清楚文本文字 send_keys(values):模拟按键输入,values是输入的内容 click():单 ...
- JS规则 我或你都可以 (逻辑或操作符)||逻辑或操作符,相当于生活中的“或者”,当两个条件中有任一个条件满足,“逻辑或”的运算结果就为“真”
我或你都可以 (逻辑或操作符) "||"逻辑或操作符,相当于生活中的"或者",当两个条件中有任一个条件满足,"逻辑或"的运算结果就为&quo ...
- Vagrant box ubuntu/xenial64 添加vagrant用户解决没有登录密码的问题
参考了Vagrant box ubuntu/xenial64 の ubuntuユーザ の passwordについて 1. 可以通过 Git Bash 使用 vagrant ssh 登录到Ubuntu ...
- css---4表单相关伪类
input:enabled{ color:red;} input:disabled{ color:blue;} enabled or disable 表单的状态 input:checked{ widt ...