POJ 1091 跳蚤 容斥原理
分析:其实就是看能否有一组解x1,x2, x3, x4....xn+1,使得sum{xi*ai} = 1,也就是只要有任意一个集合{ai1,ai2,ai3, ...aik|gcd(ai1, ai2, ai3...aik) = 1} ,也就是要求gcd(a1, a2, a3...an+1) = 1.an+1一定为M,那么前面n个数总共M^n种方案,减去gcd不为1的,也就是减去gcd为奇数个素数的乘积的情况,加上偶数个素数的乘积的情况。
代码:
#include <cstdio>
#include <iostream>
#include <map>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#define pb push_back
#define mp make_pair
#define esp 1e-8
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define sz(x) ((int)((x).size()))
#define pb push_back
#define in freopen("solve_in.txt", "r", stdin);
#define out freopen("solve_out.txt", "w", stdout); #define bug(x) printf("Line : %u >>>>>>\n", (x));
#define inf 0x7f7f7f7f
using namespace std;
typedef long long LL;
typedef map<int, int> MPS;
typedef pair<int, int> PII; const int maxn = ;
const int B = ;
struct BigInt{
int dig[maxn], len;
BigInt(int num = ):len(!!num){
memset(dig, , sizeof dig);
dig[] = num;
}
int operator [](int x)const{
return dig[x];
}
int &operator [](int x){
return dig[x];
}
BigInt normalize(){
while(len && dig[len-] == )
len--;
return *this;
}
void output(){
// cout << len << endl; if(len == ) puts("");
else {
printf("%d", dig[len-]);
for(int i = len-; i >= ; i--)
printf("%04d", dig[i]);
}
puts("");
}
};
BigInt operator * (BigInt a, BigInt b){
BigInt c;
c.len = a.len+b.len+;
for(int i = ; i < a.len; i++)
for(int j = , delta = ; j < b.len+; j++){ delta += a[i]*b[j]+c[i+j];
c[i+j] = delta%B;
delta /= B;
}
// c.normalize().output();
return c.normalize();
}
BigInt operator + (BigInt a, BigInt b){ BigInt c;
c.len = max(a.len, b.len)+;
for(int i = , delta = ; i < c.len; i++){
delta += a[i]+b[i];
c[i] = delta%B;
delta /= B;
}
return c.normalize();
}
BigInt operator - (BigInt a, BigInt b){
BigInt c;
c.len = a.len;
for(int i = , delta = ; i < c.len; i++){
delta += a[i]-b[i];
c[i] = delta;
delta = ;
if(c[i] < ){
delta = -;
c[i] += B;
}
}
return c.normalize();
}
vector<PII> arr;
int getNum(int x) {
int ans = ;
for(int i = ; i*i <= x; i++) {
if(x%i == ) {
x /= i;
ans++;
if(x%i == ) return ;
}
}
if(x != ) ans++;
return ans;
}
BigInt getPow(int x, int y){
BigInt res = ;
BigInt c;
int len = ;
while(x){
c[len] = x%B;
c.len = max(c.len, ++len);
x /= B;
}
while(y){
if(y&) res = res*c;
c = c*c;
y >>= ;
}
return res;
}
int main() { // BigInt x = getPow(2, 1);
// x.output(); int n, m;
while(scanf("%d%d", &n, &m) == ) {
BigInt ans = getPow(m, n);
// ans.output();
int y = m;
arr.clear();
for(int i = ; i*i <= y; i++) if(y%i == ) {
int tmp = getNum(i);
if(tmp) {
arr.pb(PII(i, tmp));
}
if(y/i != i) {
tmp = getNum(y/i);
if(tmp) {
arr.pb(PII(y/i, tmp));
}
}
}
for(int i = ; i < sz(arr); i++){
int x = arr[i].first;
int y = arr[i].second;
BigInt tmp = getPow(m/x, n);
if(y&) ans = ans - tmp;
else ans = ans + tmp;
}
ans.output();
}
return ;
}
另一道很类似的题目:http://www.cnblogs.com/rootial/p/4082340.html
POJ 1091 跳蚤 容斥原理的更多相关文章
- poj 1091 跳蚤
跳蚤 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8482 Accepted: 2514 Description Z城 ...
- poj 1091 跳骚
/** 题意: 求对于小于m的n个数, 求x1*a1 + x2*a2+x3*a3........+xn*an = 1 即求 a1,a2,a3,....an 的最大公约数为1 , a1,a2....an ...
- POJ 1091
这题确实是好. 其实是求x1*a1+x2*a2+....M*xn+1=1有解的条件.很明显,就是(a1,a2,...M)=1了.然后,可以想象,直接求有多少种,很难,所以,求出选择哪些数一起会不与M互 ...
- ZROI week3
作业 poj 1091 跳蚤 容斥原理. 考虑能否跳到旁边就是卡牌的\(gcd\)是否是1,可以根据裴蜀定理证明. 考虑正着做十分的麻烦,所以倒着做,也就是用\(M^N - (不合法)\)即可. 不合 ...
- POJ题目排序的Java程序
POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...
- [原]携程预选赛A题-聪明的猴子-GCD+DP
题目: 聪明的猴子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- POJ 3904 Sky Code (容斥原理)
B - Sky Code Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 2773(容斥原理)
容斥原理入门题吧. Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9798 Accepted: 3 ...
- POJ 2773 Happy 2006#素数筛选+容斥原理+二分
http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...
随机推荐
- PHP学习笔记 - 进阶篇(4)
PHP学习笔记 - 进阶篇(4) 字符串操作 字符串介绍 PHP开发中,我们遇到最多的可能就是字符串. 字符串变量用于包含字符串的值. 一个字符串 通过下面的3种方法来定义: 1.单引号 2.双引号 ...
- 使用Ctex总结1
使用Ctex应该是每一个做学术研究的人要学会掌握的. 它的基本结构: \documentclass[11pt,two side,a4paper]{cctart}%使用cctart可以让摘要变成中文,比 ...
- YII Framework 1.0运行时序图分析过程
- Java流的正确关闭方式
因为流是无论如何一定要关闭的,所以要写在finally里.如下: BufferedReader reader = null; try { reader = (BufferedReader) getRe ...
- [摘抄] 为什么 Linq 可以高效率查询 SQL ?
From C# in Depth(3rd) - Jon Skeet using (LinqDemoDataContext db = new LinqDemoDataContext()) { var f ...
- mysql table readonly
if the table does not have primary key or unique non-nullable defined, then MySql workbench could no ...
- ASP.NET 将DataTable解析成JSON简介
这里解析json使用的是Newtonsoft.Json.dll程序集.下面请看code: using System; using System.Collections.Generic; using S ...
- 服务器迁移之debian重新配置Web服务的细节
之前配置Linux服务器时采用的是Debian系统一直很稳定,这次准备迁移到新的服务器环境上,好在以前的配置我在博客都做了备忘,所以很容易就搞定了,这次服务系统采用的是最新的Debian 7.0,但是 ...
- linux关闭服务的方法
本文介绍下,在linux下关闭服务的方法,主要学习chkconfig的用法,有需要的朋友参考下. 先来看一个在linux关闭服务的例子,例如,要关闭sendmail服务,则可以按如下操作. 例1, 复 ...
- Oracle 内核参数
安装Oracle的时候,可以参考Oracle 的安装文档,来设置相关内核参数的值,但是有些参数的值还是需要根据我们自己的情况来进行调整.注:不同系统的参数不同,本篇针对linux. 一.Linux 系 ...