题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1+ a2 * b2 + a3 * b3 + …… + m * (bn + 1) = 1;

根据欧几里得,则a1, a2 a3 …… an, m 最大公约数为1,m已知且a1, a2, a3 …… an 均小于等于m, 一共有m ^ n可能, 将m 唯一分解之后, 假设m = 2 * 3 * 5, 则不大于m且包含2因子的 共 m / 2, 包含3的 为 m/ 3, 容斥

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int Max = + ;
typedef long long LL;
LL n, m;
int prime[Max + ], vis[Max + ], tot;
LL em[], a[], cnt;
LL res;
void getPrime()
{
tot = , cnt = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= Max; i++)
{
if (!vis[i])
{
prime[tot++] = i;
for (int j = i; j <= Max / i; j++)
vis[i * j] = ;
}
} }
void getfactor(int b)
{
memset(em, , sizeof(em));
for (int i = ; i < tot; i++)
{
if (prime[i] > b)
break;
if (b % prime[i] == )
{
em[cnt++] = prime[i];
while (b % prime[i] == )
{
b /= prime[i];
}
}
}
if (b > )
em[cnt++] = b;
}
LL Pow(LL a, LL b)
{
LL ans = ;
while (b > )
{
if (b & )
ans *= a;
b >>= ;
a *= a;
}
return ans;
}
void dfs(int cur, int num, int Cnt) // cur当前位置, num当前已包含个数,Cnt目标个数
{
if (num == Cnt)
{
//达到目标个数时, 用 m 去除以 所有包含的数
LL u = m;
for (int i = ; i < num; i++)
u /= a[i];
res += Pow(u, n);
return;
}
for (int i = cur; i < cnt; i++)
{
a[num] = em[i];
dfs(i + , num + , Cnt);
}
return;
}
int main()
{
getPrime();
while (scanf("%I64d%I64d", &n, &m) != EOF)
{
getfactor(m);
LL sum = Pow(m, n);
for (int i = ; i <= cnt; i++) // 枚举每一个组合,i表示每个组合中数的个数
{
res = ;
dfs(, , i);
if (i & )
sum -= res;
else
sum += res;
}
printf("%I64d\n", sum);
} return ;
}

POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)的更多相关文章

  1. HUST 1569(Burnside定理+容斥+数位dp+矩阵快速幂)

    传送门:Gift 题意:由n(n<=1e9)个珍珠构成的项链,珍珠包含幸运数字(有且仅由4或7组成),取区间[L,R]内的数字,相邻的数字不能相同,且旋转得到的相同的数列为一种,为最终能构成多少 ...

  2. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  3. bzoj 3930: [CQOI2015]选数【快速幂+容斥】

    参考:https://www.cnblogs.com/iwtwiioi/p/4986316.html 注意区间长度为1e5级别. 则假设n个数不全相同,那么他们的gcd小于最大数-最小数,证明:则gc ...

  4. 刷题总结——跳蚤(poj1091容斥+分解质因数)

    题目: Description Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有 ...

  5. BZOJ 1008--[HNOI2008]越狱(容斥&快速幂)

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 12593  Solved: 5439[Submit][Status ...

  6. [HNOI2002]跳蚤 【容斥】

    题目描述 Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最 ...

  7. hdu 5768 Lucky7 中国剩余定理+容斥+快速乘

    Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  8. HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others)    M ...

  9. 【GDOI2016模拟3.16】幂(容斥 + 模型复杂转化)

    [GDOI2016模拟3.16]幂 \(X\in[1,A],Y\in[1,B]\),问:\(x^y\)的不用取值个数. \(A,B\)都是\(10^9\)级别. 然后我们开搞. 首先,假设一个合法的\ ...

随机推荐

  1. Java导入的项目乱码怎么解决?(Ⅱ)

    1.首先 打开  >>  Eclipse或Myeclipse.(我用的是Myeclipse) 2.打开  >>  Window  >>  Preferences  ...

  2. IE8 ajax缓存问题

    娘希匹,又遇到缓存问题了. 下面的代码,在其他浏览器都是正常的,但是在IE8中出现诡异问题. $.ajax({ url:dataUrl, data:encodeURI(currentjsonform) ...

  3. Redis学习和环境搭建

    基本的redis教程,搭建,可以参照下面任一教程: 地址一:http://www.yiibai.com/redis/redis_quick_guide.html 地址二:http://www.runo ...

  4. 常用Linux命令记录

    [RSYNC] 指定SSH端口从远程服务器同步文件至本地目录 rsync -avH --progress '-e ssh -p 3600' user@remote_ip:remote_dir  loc ...

  5. maven archetype生成自定义项目原型(模板)

    maven archetype可以将一个项目做成项目原型,之后只需要以此原型来创建项目,那么初始创建的项目便具有原型项目中的一切配置和代码.通俗讲就是一个项目模板. eclipse中那些快速生成mav ...

  6. WPF Tookit Chart

      如何使用Chart 实例: Binding数据源中是一个KeyValuePair对象.可以是Dictionary. <charting:Chart x:Name="chtSumma ...

  7. 【2016-11-5】【坚持学习】【Day20】【通过委托事件,关闭窗口】

    Window1 UserControl viewModel 在viewModel 关闭window1

  8. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

  9. POJ2505 A multiplication game[博弈论]

    A multiplication game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6028   Accepted:  ...

  10. [NOIP2014]自测

    这两天做完了2014年的noip提高. 因为以前看了SDSC2016时gty的课件,题目思路都知道了一点,做起来没多大困难. 100+100+75+100+100+70=545 里面水分好多,好多题都 ...