BUPT2017 springtraining(16) #4 ——基础数论
题目在这里
A.手动打表找规律得组合数
n -= 2, m -= 2, ans = C(n, m)
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int Mod = 1e9 + ;
- ll fac[];
- ll calc(ll x, int k = Mod - ) {
- ll ret = ;
- for(;k;k >>= , x = x * x % Mod)
- if(k & ) ret = ret * x % Mod;
- return ret;
- }
- int main() {
- ios::sync_with_stdio(false);
- int n, m;
- fac[] = fac[] = ;
- for(int i = ;i <= ;i ++)
- fac[i] = fac[i - ] * i % Mod;
- while(cin >> n >> m) {
- if(n > m) swap(n, m);
- cout << fac[m + n - ] * calc(fac[n - ]) % Mod * calc(fac[m - ]) % Mod << endl;
- }
- return ;
- }
B.
C.裸快速幂
- #include <cstdio>
- using namespace std;
- double x = 1.000000011;
- double n;
- long long k;
- int main() {
- scanf("%lf %lld", &n, &k);
- for(;k;k >>= , x = x * x)
- if(k & ) n = n * x;
- printf("%.8f\n", n);
- return ;
- }
D.递推式那么明显当然是矩阵快速幂啦
唯一需要注意的是,自乘矩阵的填充方式...
开始时,a1 对应 f(d) , a2 对应 f(d - 1) ...
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int d, n, m;
- struct matrix1{
- ll c[][];
- matrix1() {
- memset(c, , sizeof c);
- }
- void init() {
- memset(c, , sizeof c);
- for(int i = ;i < d;i ++) cin >> c[d - - i][d - ], c[d - - i][d - ] %= m;
- for(int i = ;i < d;i ++) c[i][i - ] = ;
- }
- matrix1 operator *(const matrix1 &a) const {
- matrix1 ret;
- for(int k = ;k < d;k ++)
- for(int i = ;i < d;i ++)
- if(c[i][k])
- for(int j = ;j < d;j ++)
- ret.c[i][j] = (ret.c[i][j] + c[i][k] * a.c[k][j]) % m;
- return ret;
- }
- };
- struct matrix2{
- ll c[];
- matrix2() {
- memset(c, , sizeof c);
- }
- void init() {
- memset(c, , sizeof c);
- for(int i = ;i < d;i ++) cin >> c[i], c[i] %= m;
- }
- matrix2 operator *(const matrix1 &a) const {
- matrix2 ret;
- for(int i = ;i < d;i ++)
- for(int j = ;j < d;j ++)
- ret.c[i] = (ret.c[i] + c[j] * a.c[j][i]) % m;
- return ret;
- }
- };
- int main() {
- matrix1 c1;
- matrix2 c2;
- while(cin >> d >> n >> m, d != ) {
- c1.init(), c2.init();
- for(n --;n;n >>= , c1 = c1 * c1)
- if(n & ) c2 = c2 * c1;
- cout << c2.c[] << endl;
- }
- return ;
- }
E.参考 CodeForces - 785D
F.看样例猜结论就好了
输出所有质数的幂就好了
- #include <iostream>
- using std::cin;
- using std::endl;
- using std::cout;
- int n, v[], p[];
- int main() {
- cin >> n;
- for(int i = ;i <= n;i ++) {
- if(!v[i]) {
- for(int j = i;j <= n;j *= i)
- p[++ p[]] = j;
- }
- for(int j = i << ;j <= n;j += i)
- v[j] = ;
- }
- cout << p[] << endl;
- for(int i = ;i <= p[];i ++)
- cout << p[i] << " ";
- return ;
- }
G.参考 CodeForces - 785C
H.
I.能被 6 整除一定能被 2 和 3 整除,8 和 9 同理
于是就是求 1 - n 内2 3 5 7的倍数之和
容斥原理即可
- #include <iostream>
- using namespace std;
- int main() {
- long long n;
- cin >> n;
- cout << n - n / - n / - n / - n / + n / + n / + n / + n / + n / + n / - n / - n / - n / - n / + n / ;
- return ;
- }
J.
K.对于n 的每一个因数 t
它对答案贡献为 t * phi(n / t)
效率玄学吧
- #include <cstdio>
- int v[], p[];
- int phi(int x) {
- int ret = x;
- for(int i = ;1ll * p[i] * p[i] <= x;i ++)
- if(x % p[i] == ) {
- ret = ret - ret / p[i];
- while(x % p[i] == ) x /= p[i];
- }
- if(x != ) ret -= ret / x;
- return ret;
- }
- int main() {
- long long n, ans;
- for(int i = ;i < ;i ++) {
- if(!v[i]) p[++ p[]] = i;
- for(int j = ;j <= p[] && p[j] * i < ;j ++) {
- v[p[j] * i] = ;
- if(i % p[j] == ) break;
- }
- }
- while(scanf("%lld", &n) != EOF) {
- ans = ;
- for(long long i = ;i * i <= n;i ++) {
- if(n % i) continue;
- ans += i * phi(n / i);
- if(i * i != n) ans += n / i * phi(i);
- }
- printf("%lld\n", ans);
- }
- return ;
- }
L.和式加一项减一项变成了C(n + m, n)
n,m 大 p 小且 p 保证为质数,使用卢卡斯定理
Lucas(n,m) % p = Lucas(n / p,m / p) * Comb(n % p,m % p) % p
注意lucas里面的取模后计算组合数,可能会出现 C(n,m) 里 n < m
所以Comb函数里需要判断的
考虑 p 为读入的不能预处理,所以我们有两种解决方案
1.每组数据 O(p) 预处理处阶乘 fac
Comb就可以 O(logn) 回答,理论效率 O(p + logn * log(p)(n + m))
大概就是 O(p)
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int t, n, m, p;
- ll fac[];
- ll calc(ll x, int k = p - ) {
- ll ret = ;
- for(;k;k >>= , x = x * x % p)
- if(k & ) ret = ret * x % p;
- return ret;
- }
- ll C(int n, int m) {
- if(n < m) return ;
- return fac[n] * calc(fac[m]) % p * calc(fac[n - m]) % p;
- }
- ll lucas(int n, int m) {
- if(!m) return ;
- return C(n % p, m % p) * lucas(n / p, m / p) % p;
- }
- int main() {
- ios::sync_with_stdio(false);
- fac[] = ;
- cin >> t;
- while(t --) {
- cin >> n >> m >> p;
- for(int i = ;i < p;i ++) fac[i] = fac[i - ] * i % p;
- cout << lucas(n + m, n) << endl;
- }
- return ;
- }
2.不进行预处理,Comb直接 O(p + logn) 回答
理论效率O(p * log(p)(n + m))
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int t, n, m, p;
- ll calc(ll x, int k = p - ) {
- ll ret = ;
- for(;k;k >>= , x = x * x % p)
- if(k & ) ret = ret * x % p;
- return ret;
- }
- ll C(int n, int m) {
- if(n < m) return ;
- ll ret1 = , ret2 = ;
- for(int i = , j = n;i <= m;i ++, j --) {
- ret1 = ret1 * i % p;
- ret2 = ret2 * j % p;
- }
- return ret2 * calc(ret1) % p;
- }
- ll lucas(int n, int m) {
- if(!m) return ;
- return C(n % p, m % p) * lucas(n / p, m / p) % p;
- }
- int main() {
- ios::sync_with_stdio(false);
- cin >> t;
- while(t --) {
- cin >> n >> m >> p;
- cout << lucas(n + m, n) << endl;
- }
- return ;
- }
然而第二种的表现更好...因为都是最坏时间估计
而第一种稳定在O(p),第二种实际是几倍优于最坏效率的
M.
BUPT2017 springtraining(16) #4 ——基础数论的更多相关文章
- BUPT2017 springtraining(16) #2 ——基础数据结构
题目在这里 A.似乎是个并查集+??? B.10W的范围,似乎可以暴力来一发二分+sort? 但我猜正解可以O(nlogn)? C.单调队列入门题目 #include <cstdio> ] ...
- BUPT2017 springtraining(16) #1 题解
https://vjudge.net/contest/162590 A: 不难发现,当L=R时输出L,当L<R时输出2. B: 贪心得配对.1和n配 2和n-1配,对与对直接只要花1个代价就可以 ...
- BUPT2017 springtraining(16) #6 ——图论
题目链接 A.容易发现最后字符的对应都是一对一的 或者说我们没办法出现最后多对一或者一对多的情况 所以只要算出 ‘a’ - 'z' 每个字符最后对应的字符即可 #include <cstdio& ...
- BUPT2017 springtraining(16) #3 ——搜索与动态规划
题目在这里啊 A.最长上升子序列,范围很小所以写了简单的O(n^2)算法 #include <iostream> #define rep(i, j, k) for(int i = j;i ...
- BUPT2017 springtraining(16) #1 ——近期codeforces简单题目回顾
这里是contest 8道题全部来源于 cf 的两场contest (出题人可真懒啊 Codeforces Round #411 (Div. 2)的ABCDE Codeforces Round #40 ...
- LightOJ1214 Large Division 基础数论+同余定理
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...
- HDU-1576 A/B 基础数论+解题报告
HDU-1576 A/B 基础数论+解题报告 题意 求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973) (我们给定的A必能被B整除,且gcd(B,9973) = 1). 输入 数据 ...
- RSA算法原理——(2)RSA简介及基础数论知识
上期为大家介绍了目前常见加密算法,相信阅读过的同学们对目前的加密算法也算是有了一个大概的了解.如果你对这些解密算法概念及特点还不是很清晰的话,昌昌非常推荐大家可以看看HTTPS的加密通信原理,因为HT ...
- ACM&OI 基础数论算法专题
ACM&OI 基础数学算法专题 一.数论基础 质数及其判法 (已完结) 质数的两种筛法 (已完结) 算数基本定理与质因数分解 (已完结) 约数与整除 (已完结) 整除分块 (已完结) 最大公约 ...
随机推荐
- bzoj 3993 星际战争
题目大意: X军团和Y军团正在激烈地作战 在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai 当一个巨型机器人的装甲值减少到0或者以下时,这个巨型机 ...
- 【WIP】Ruby CSV文件操作
创建: 2017/09/30 ...
- php排序函数测试
1.sort,asort,arsort函数 十万个数的数组排序,用了0.17秒 $starttime=explode(' ',microtime());;for ($i=0; $i <10000 ...
- P2258 子矩阵(dp)
P2258 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4 ...
- [Swift通天遁地]四、网络和线程-(4)使用Alamofire实现网络请求
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Django day32 跨域问题,创建vue项目,axios的使用
一:跨域问题 1.同源策略(浏览器的安全策略) 只允许当前页面朝当前域下发请求,如果向其他域发请求,请求可以正常发送,数据也可以拿回,但是被浏览器拦截了 2.cors:只要服务器实现了CORS,就可以 ...
- 音频处理中的尺度--Bark尺度与Mel尺度
由于人耳对声音的感知(如:频率.音调)是非线性的,为了对声音的感知进行度量,产生了一系列的尺度(如:十二平均律),这里重点说下Bark尺度与Mel尺度.刚开始的时候,我自己也没弄明白这两个尺度的区别. ...
- BZOJ 3514 LCT+主席树
思路: //By SiriusRen #include <bits/stdc++.h> using namespace std; ; ],fa[N],minn[N],rev[N],q[N] ...
- 【USACO2002 Feb】奶牛自行车队
[USACO2002 Feb]奶牛自行车队 Time Limit: 1000 ms Memory Limit: 131072 KBytes Description N 头奶牛组队参加自行车赛.车队在比 ...
- PHP 在线 编辑 解析
http://www.w3schools.com/php/default.asp http://www.w3schools.com/php/showphp.asp?filename=demo_s ...