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 ...
随机推荐
- SpringCloud及其五大常用组件之Eureka和Zuul
1.springcloud简介 SpringCloud是Spring旗下的项目之一,它是微服务架构的一种实现方式. 官网地址:http://projects.spring.io/spring-clou ...
- 微信小程序——单选项
对于小程序单选,官方文档已经贴出了代码,我这里也不做过多解释,只是分享给大家一个小功能 一般在单选或者多选时,都会出现“其他”这个选项,如果通过input焦点事件.失焦事件来控制,代码会很繁琐 这里可 ...
- 【题解】洛谷 P1061 Jam的计数法
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- Django rest framework (视图类详解)
官网:https://www.django-rest-framework.org/api-guide/viewsets/ 在django rest framework 视图中一共有N个类 第一类:AP ...
- 深入浅出Java中的clone克隆方法,写得太棒了!
作者:张纪刚 blog.csdn.net/zhangjg_blog/article/details/18369201/ Java中对象的创建 clone 顾名思义就是 复制 , 在Java语言中, c ...
- Spring MVC上传、下载 文件
1,上传文件 public static String upload(MultipartFile file, SysUserBean sysUserBean, HttpServletRequest r ...
- spark 应用场景2-身高统计
原文引自:http://blog.csdn.net/fengzhimohan/article/details/78564610 a. 案例描述 本案例假设我们需要对某个省的人口 (10万) 性别还有身 ...
- 创建 linuxrc 文件
创建 linuxrc,加入如下内容: [arm@localhost my_rootfs]#vi linuxrc #!/bin/sh #挂载/etc 为 ramfs, 并从/mnt/etc 下拷贝文件到 ...
- 几道关于this的经典练习题的理解与分析
1. var num = 1; var myObject = { num: 2, add: function() { this.num = 3; (function() { console.log(t ...
- git安装与上传
git安装与上传 上一篇 / 下一篇 2017-03-10 10:09:42 / 个人分类:代码管理工具 查看( 63 ) / 评论( 0 ) / 评分( 0 / 0 ) 1.安装Git-2.11. ...