分析:其实就是看能否有一组解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. using System.Reflection;

    基础代码: public interface IDBHelper { void Query(); } public class DBHelper : IDBHelper { public int Id ...

  2. 创建featureclass,为它赋别名,并移动到数据集下

    if (pOutFtrClass == null) { //continue; //创建featureclass //得到规范的字段集 IFields pFields = pFeatureClass. ...

  3. 在ThinkPHP3.x框架中实现将原创文章第一时间推送到百度收录

    前两天自己写的一篇文章“针对BootStrap中tabs控件的美化和完善”被别的网站给转载了,这也许是值得高兴的一件事情,但是有些网站并没有注明来源和作者.而去百度搜索这篇文章,排名第一的居然是那些转 ...

  4. MoneyUtil

    public class MoneyUtil {       private final static String[] CN_Digits = { "零", "壹&qu ...

  5. emoji处理方法汇总

    emoji资料 今天研究了emoji,挺有意思,资料挺多,摘要一些信息给大家分享,也算是自己记录学习. emoji介绍 Emoji (絵文字,词义来自日语えもじ,e-moji,moji在日语中的含义是 ...

  6. Ecshop实现仿Taobao地区运费模板

    目录: 1.Ecshop后台配送方式创建 2.商品绑定配送方式的运费模板 2.1 数据表“ecs_goods”增加一个字段,执行下面SQL语句: 2.2 后台添加/编辑 商品 调出已经安装配送方式 & ...

  7. delphi xe memory leak produced in WSDLLookup.pas

    constructor TWSDLLookup.Create; begin FLookup := TDictionary<string, Variant>.Create; end; des ...

  8. 《C和指针》读书笔记——第五章 操作符和表达式

    1.当/操作符的两个操作数都是整数时,它执行整除运算:其他都是执行浮点数除法. 2.逻辑移位:左边移入的位用0填充: 算数移位:左边移入的位用符号位填充: 3.位置1 :value |= 1<& ...

  9. Python urllib2多进程共享cookies

    如果想多个进程共享同一个cookies,不用每个进程都重新登录,可以就cookies保存到一个文件,然后多个进程直接共享一个锁来实现 1.一个进程登录完成后,把cookies保存到一个文件里面 sel ...

  10. 《WPF程序设计指南》读书笔记——第5章 Stack与Wrap

    1.StackPanel面板 using System; using System.Windows; using System.Windows.Input; using System.Windows. ...