容斥原理 + 大数模板(跳蚤 POJ
来源:互联网 发布:淘宝头像 编辑:程序博客网 时间:2024/05/22 12:38
欢迎大家来刷我的专题:https://vjudge.net/contest/177165#problem/I
这是一道数范围很大的题,据说数据很水.
首先题目的意识也就是 一个方程有解,系数都是<=m,最后一个系数为m,这个等于1的方程一定有解的话,也就是说这n+1个系数的gcd=1,互质,那么解决问题的思路就有莫比乌斯反演,欧拉函数,容斥原理等等,不过这道题的话,前两个不是很好想.
这里简要说一下容斥原理的思路.
容斥原理: 一共有
答案显然就是
#include<cstdio>#include<iostream>#include<bitset>#include<cstring>#define ll long longconst int maxn = 1e3;using namespace std;int tot = 0;int n,m;struct BigInt{ const static int mod = 10; int a[maxn], len; BigInt(){ memset(a, 0, sizeof a); len = 1; } BigInt(ll v){ memset(a, 0, sizeof a); len = 0; do{ a[len++] = v%mod; v/=mod; }while(v); } BigInt operator +(const BigInt &b)const{ BigInt res; res.len = max(len, b.len); for(int i = 0; i <= res.len; i++){ res.a[i] = 0; } for(int i = 0; i < res.len; i++){ res.a[i] += ((i < len)?a[i]:0) + ((i < b.len)?b.a[i]:0); res.a[i+1] += res.a[i]/mod; res.a[i] %= mod; } if(res.a[res.len] > 0) res.len++; return res; } //保证a>=b BigInt operator -(const BigInt &b)const{ BigInt res; res.len = len; for(int i = 0; i <= res.len; i++){ res.a[i] = 0; } for(int i = 0; i < res.len; i++){ res.a[i] += (a[i]-b.a[i]); if(res.a[i]<0){ res.a[i]+=10; res.a[i+1]--; } } while(res.a[res.len-1]==0 && res.len > 1)res.len--; return res; } BigInt operator *(const BigInt &b)const{ BigInt res; for(int i = 0; i < len; i++){ int up = 0; for(int j = 0; j < b.len; j++){ int temp = a[i]*b.a[j] + res.a[i+j] + up; res.a[i+j] = temp%mod; up = temp/mod; } if(up != 0) res.a[i + b.len] = up; } res.len = len + b.len; while(res.a[res.len - 1] == 0 && res.len > 1)res.len--; return res; } void output(){ for(int i = len-1; i >= 0; i--) printf("%d",a[i]); } void init(){ memset(a, 0, sizeof a); len = 1; }};BigInt power(BigInt a, int b){ BigInt ans = BigInt(1); while(b){ if(b&1) ans = ans*a; a = a*a; b >>= 1; } return ans;}int p[maxn];int main(){ while(~scanf("%d%d",&n,&m)){ int tot = 0; int mm = m; for(int i = 2; i <= m/i; i++){ if(m%i==0){ p[tot++] = i; } while(m%i==0){ m/=i; } } if(m>1){ p[tot++] = m; } BigInt ans = power(mm, n); ll tmp = 1; int c = 0; for(int i = 1; i < 1<<tot; i++){ c = 0; tmp = mm; for(int j = 0; j < tot; j++) if(i & (1 << j)){ c++; tmp/=p[j]; } BigInt it = BigInt(tmp); if(c&1) ans = ans - power(it, n); else ans = ans + power(it, n); //ans.output(); //puts(""); } ans.output(); puts(""); } return 0;}
阅读全文
1 0
- 容斥原理 + 大数模板(跳蚤 POJ
- poj 1091 跳蚤 (数论,容斥原理)
- 跳蚤 - POJ 1091 容斥原理
- POJ 1091 跳蚤 容斥原理
- POJ 1091 跳蚤(容斥原理)
- poj 1091 跳蚤(容斥原理)
- POJ 1091 跳蚤(分解质因数 + 容斥 + 大数)
- poj 1091 跳蚤(最大公约数原理+容斥原理)
- POJ 1091 跳蚤 数论-容斥原理、扩展欧几里得
- POJ 1091 跳蚤 数论-容斥原理、扩展欧几里得
- poj 1091 跳蚤 扩展欧几里得性质+容斥原理
- poj 1091 跳蚤 扩展欧几里得+容斥原理
- POJ 1091 跳蚤 [容斥原理]【组合数学】
- POJ 1091 - 跳蚤 - 容斥原理 +扩展欧几里德+高精度
- HNOI2002跳蚤--容斥原理
- poj 1091 跳蚤(分解质因数+容斥)
- poj 1091-跳蚤(数论+容斥)
- POJ 1091 跳蚤(数论+容斥)
- less简单笔记
- Simple Class Serialization With JsonCpp
- 【HDU1069】 Monkey and Banana(动态规划)
- windows10 64位 redis3.0.*下载以及安装
- 多线程入门
- 容斥原理 + 大数模板(跳蚤 POJ
- 中缀表达式转换为后缀表达式&后缀表达式的计算
- 密码学----基础概念(二)
- 单调队列
- html各种分割线
- 单调栈/LIS的log n做法
- 深入学习js之浅谈作用域(RHS和LHS)
- 【POJ2533】Longest Ordered Subsequence (动态规划&最长上升子序列)
- iOS应用兼容测试策略学习笔记