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, ...
随机推荐
- IE浏览器打开 「兼容性视图」
有些IE上的网页控件需要打开兼容性视图才能使用,不知道是Javascript的原因,还是CSS的原因. 使用环境是用C语言配合boa服务器实现的CGI程序.
- 内存管理算法--Buddy伙伴算法
Buddy算法的优缺点: 1)尽管伙伴内存算法在内存碎片问题上已经做的相当出色,但是该算法中,一个很小的块往往会阻碍一个大块的合并,一个系统中,对内存块的分配,大小是随机的,一片内存中仅一个小的内存块 ...
- 十天学会零基础入门学习Photoshop课程(在线观看)
适合人群:在校学生 在职工作者 淘宝运营者等一系列会操作电脑的人群 课程目录 试学课 课时11前言 8分钟1秒 课时22工作界面 试学课 课时33文件的新建 试学课 课时44文档保存 11分钟24秒 ...
- fedora下python3 安装tkinter和pygame
root 下用 “yum search tkinter”,如下图所示: 然后再根据系统选择安装就好了. tkinter安装完毕. 接下来是安装pygame在fedora的python3上,我的是pyt ...
- DTCMS自定义标签:获取所有栏目以及不显示指定栏目
DTcms.Web.UI\Label\category.cs中 添加下面代码 /// <summary> /// 返回所有类别 /// </summary> /// <r ...
- aix用命令查监听端口对应的进程
--aix$netstat -an|grep LISTEN|grep 7867tcp4 0 0 *.7867 *.* LISTEN$netstat -Aan|grep 7867f1000e00029f ...
- Jquery实现图片左右自动滚动
图片左右滚动的效果想必大家都有见到过吧,其实很简单.在本文将为大家介绍下使用Jquery是如何实现图片左右自动滚动的. 代码如下:<!DOCTYPE HTML> <html> ...
- 009.EscapeRegExChars
类型:function 可见性:public 所在单元:RegularExpressionsCore 父类:TPerlRegEx 把转义字符变成原意字符 例如\d意为0~9某个数字,通过此函数转换后则 ...
- 做了codility网站上一题:CountBoundedSlices
在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo ...
- php 时间转化总结
iQuery插件datepicker获取的时间函数为"月月/天天/年年年年"(以04/21/2015为例)的形式 (1)转化为2015-21-04形式:$start = date( ...