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, ...
随机推荐
- Cocos2d-x开发实例介绍帧动画使用
下面我们通过一个实例介绍一下帧动画的使用,这个实例如下图所示,点击Go按钮开始播放动画,这时候播放按钮标题变为Stop,点击Stop按钮可以停止播放动画. 下面我们再看看具体的程序代码,首先看一下看H ...
- 博客迁到CSDN
之前一直是博客园和CSDN博客同步更新 现在不在博客园继续写博客,十一国庆节假后只用CSDN博客了.祝各位访客国庆节快乐! CSDN博客地址: http://blog.csdn.net/it_liuc ...
- Smarty控制foreach输出数量
最近在搞ecshop,ecshop用的是smarty.在首页中想要修改展示的商品数量,但是用的是foreach,让我无从下手,在网上找了好久终于找到解决方案. <!--{foreach name ...
- (转)使用Visual Studio 2015开发Android 程序
环境配置: 操作系统:win 7 64位 IDE:Visual Studio 2015 SDK:installer_r24.3.3-windows 安装前提: 编辑hosts文件(在附件可下载)因为安 ...
- CAF(C++ actor framework)使用随笔(使用类去构建actor和使用的一些思路)
Class-based actorsA class-based actor is a subtype of event_based_actor and must implement the pure ...
- SQL server 常见用法记录
-- ============================================= -- Author: tanghong -- Create da ...
- js中使用使用原型(prototype)定义方法的好处
经常在前端面试或是和其他同行沟通是,在谈到构造在JS定义构造函数的方法是最好使用原型的方式:将方法定义到构造方法的prototype上,这样的好处是,通过该构造函数生成的实例所拥有的方法都是指向一个函 ...
- PHP实现冒泡算法
<?php //php函数:count($arr)返回array的数值总数. function bubble_sort($arr){ for ($i = 6;$i > 0;$i --){ ...
- 浏览器兼容CSS代码:按钮文字垂直居中(input button text vertical align)
经过测试的浏览器:IE6, IE7, IE8, IE9, Firefox, Chrome, Safiri, Maxthon 按钮的HTML代码: <input id="btn_comm ...
- Spark Streaming揭秘 Day31 集群模式下SparkStreaming日志分析(续)
Spark Streaming揭秘 Day31 集群模式下SparkStreaming日志分析(续) 今天延续昨天的内容,主要对为什么一个处理会分解成多个Job执行进行解析. 让我们跟踪下Job调用过 ...