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 ...
随机推荐
- c++与js脚本交互,C++调用JS函数JS调用C++函数
一.javascript调用c++,方法有两种 方案1: 1.html编写 <html><head></head><body><h1>TES ...
- 总结加密、机密jar中的class
1.加密和解密部署到jboss中间件中的的单个class文件,原理:使用“java源程序加密解决方案(基于Classloader解密) (2014-07-13 11:31)”blog即可实现: imp ...
- 类的反射实例(servlet的抽取)
类的反射实例 具体以后我们写的时候不用写BaseServlet,因为各种框架都已经给我们写好了 所以,user对应的servlet的界面长这样:
- 夏令营501-511NOIP训练17——蛇形矩阵
传送门:QAQQAQ 题意:话说小X在孩提时,都会做标准的蛇形矩阵了,发现很好玩.现在的小X很想对其进行改版,变为如下类型的一个无限大蛇形数阵:令S(x)表示以1为左上角,x为右下角的矩形内所有数之和 ...
- windows安装vscode,配置golang环境
出现的问题: 进行如下命令进行目录切换:cd %GOPATH%\src\github.com\golang我这里的GOPATH是在D:\GoPath,大家这里一定要注意些如果src目录下面没有gith ...
- <scrapy爬虫>基本操作
scrapy选择器的用法 //selector可以加可以不加 response.selector.xpath("//title/text()").extract_first() r ...
- TIB、TEB 信息
https://en.wikipedia.org/wiki/Win32_Thread_Information_Block 这是重点 Position Length Windows Versions D ...
- 2018-8-10-win10-uwp-如何判断一个对象被移除
title author date CreateTime categories win10 uwp 如何判断一个对象被移除 lindexi 2018-08-10 19:16:50 +0800 2018 ...
- [JZOJ4759] 【雅礼联考GDOI2017模拟9.4】石子游戏
题目 描述 题目大意 在一棵树上,每个节点都有些石子. 每次将mmm颗石子往上移,移到根节点就不能移了. 双方轮流操作,问先手声还是后手胜. 有三种操作: 1. 询问以某个节点为根的答案. 2. 改变 ...
- Oracle - 用户及表空间的创建和删除
-- 查询所有用户 SELECT USERNAME FROM ALL_USERS; -- 查询所有表空间 SELECT TABLESPACE_NAME FROM USER_TABLESPACES; - ...