分析:其实就是看能否有一组解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 跳蚤 容斥原理的更多相关文章

  1. poj 1091 跳蚤

    跳蚤 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8482   Accepted: 2514 Description Z城 ...

  2. poj 1091 跳骚

    /** 题意: 求对于小于m的n个数, 求x1*a1 + x2*a2+x3*a3........+xn*an = 1 即求 a1,a2,a3,....an 的最大公约数为1 , a1,a2....an ...

  3. POJ 1091

    这题确实是好. 其实是求x1*a1+x2*a2+....M*xn+1=1有解的条件.很明显,就是(a1,a2,...M)=1了.然后,可以想象,直接求有多少种,很难,所以,求出选择哪些数一起会不与M互 ...

  4. ZROI week3

    作业 poj 1091 跳蚤 容斥原理. 考虑能否跳到旁边就是卡牌的\(gcd\)是否是1,可以根据裴蜀定理证明. 考虑正着做十分的麻烦,所以倒着做,也就是用\(M^N - (不合法)\)即可. 不合 ...

  5. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  6. [原]携程预选赛A题-聪明的猴子-GCD+DP

    题目: 聪明的猴子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. POJ 3904 Sky Code (容斥原理)

    B - Sky Code Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  8. poj 2773(容斥原理)

    容斥原理入门题吧. Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9798   Accepted: 3 ...

  9. POJ 2773 Happy 2006#素数筛选+容斥原理+二分

    http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...

随机推荐

  1. Swift静态方法

    与静态属性类似,Swift中还定义了静态方法,也称为类型方法,所谓“类型”是指枚举.结构体和类.静态方法定义的方法也是与静态属性类似的,枚举和结构体的静态方法使用的关键字是static,类的静态方法使 ...

  2. java SimpleDateFormat非线程安全测试

    public class MyThread extends Thread { private SimpleDateFormat sdf; private String dateString; publ ...

  3. 一个JS内存泄露实例分析

    在看JS GC 相关的文章时,好几次看到了下面这个设计出来的例子,比较巧妙,环环相扣.   var theThing = null; var replaceThing = function () { ...

  4. DOM_节点层次_Element类型

    一.Element类型: nodeType: 1; nodeName: 元素名; nodeValue: null; parentValue: Document 或者 Element; var oDiv ...

  5. xml的生成与解析_老师笔记

    使用序列化器生成一个xml文件 //1,初始化一个xml文件的序列化器 XmlSerializer serializer = Xml.newSerializer(); //2.初始化序列器参数 Fil ...

  6. JPA && Spring Data && Spring Data JPA

    1.JPA  Java Persistence API,用于对象持久化的一组API,JPA本身是一组规范,让开发者用同一种方式访问不同的ORM框架.其实也就是java实体对象和关系型数据库建立起映射关 ...

  7. web网页的表单排版利器--960css

    表单排版样式 960css 前言 一般web网页的表单排版,大家都习惯用table排版,自己需要根据实际需要去定义TR和TD,很多时候对于TD的高宽度.是否合并行,合并列,都要去做一些处理,这些都是比 ...

  8. JQuery 解决 鼠标快速滑过后,会执行多次滑出的问题

    如果用slideToggle,鼠标快速滑过后,滑进滑出很多次,要解决这个问题,用stop(false,true) $(".Nav_L").hover(function () { $ ...

  9. c++,C# 转换

    //C++中的DLL函数原型为        //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsi ...

  10. 搭建pptpd实现vpn

    PPTP(Point to Point Tuneling Protocol,点对点隧道协议)是一种主要用于VPN的数据链路层网络协议. 环境:debian 7.0 在linux下安装pptpd服务实现 ...