poj 1091 跳蚤(不定方程+容斥)
2015-04-07 22:13
393 查看
跳蚤
Description
Z城市居住着很多只跳蚤。在Z城市周六生活频道有一个娱乐节目。一只跳蚤将被请上一个高空钢丝的正中央。钢丝很长,可以看作是无限长。节目主持人会给该跳蚤发一张卡片。卡片上写有N+1个自然数。其中最后一个是M,而前N个数都不超过M,卡片上允许有相同的数字。跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向右跳S个单位长度。而他最终的任务是跳到距离他左边一个单位长度的地方,并捡起位于那里的礼物。
比如当N=2,M=18时,持有卡片(10, 15, 18)的跳蚤,就可以完成任务:他可以先向左跳10个单位长度,然后再连向左跳3次,每次15个单位长度,最后再向右连跳3次,每次18个单位长度。而持有卡片(12, 15, 18)的跳蚤,则怎么也不可能跳到距他左边一个单位长度的地方。
当确定N和M后,显然一共有M^N张不同的卡片。现在的问题是,在这所有的卡片中,有多少张可以完成任务。
Input
两个整数N和M(N <= 15 , M <= 100000000)。
Output
可以完成任务的卡片数。
Sample Input
Sample Output
Hint
这12张卡片分别是:
(1, 1, 4), (1, 2, 4), (1, 3, 4), (1, 4, 4), (2, 1, 4), (2, 3, 4),
(3, 1, 4), (3, 2, 4), (3, 3, 4), (3, 4, 4), (4, 1, 4), (4, 3, 4)
a1*x1+a2*x2+a3*x3+……+anxn+M*x(n+1)=1
(a1,a2,a3,……,an,M)=1 排除公因子非1的情况 总共的情况数是M^n
a1-an都是小于等于M
初始化出M范围内所有的素因子 枚举所有范围内的素因子
例如 有素因子2 所以M内有M/2个数有2这个素因子 有素因子3 所以M内有M/3个数有3这个素因子
所以重复计算了6这个因子 多算了M/6个数 诸如此类。。。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8731 | Accepted: 2605 |
Z城市居住着很多只跳蚤。在Z城市周六生活频道有一个娱乐节目。一只跳蚤将被请上一个高空钢丝的正中央。钢丝很长,可以看作是无限长。节目主持人会给该跳蚤发一张卡片。卡片上写有N+1个自然数。其中最后一个是M,而前N个数都不超过M,卡片上允许有相同的数字。跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向右跳S个单位长度。而他最终的任务是跳到距离他左边一个单位长度的地方,并捡起位于那里的礼物。
比如当N=2,M=18时,持有卡片(10, 15, 18)的跳蚤,就可以完成任务:他可以先向左跳10个单位长度,然后再连向左跳3次,每次15个单位长度,最后再向右连跳3次,每次18个单位长度。而持有卡片(12, 15, 18)的跳蚤,则怎么也不可能跳到距他左边一个单位长度的地方。
当确定N和M后,显然一共有M^N张不同的卡片。现在的问题是,在这所有的卡片中,有多少张可以完成任务。
Input
两个整数N和M(N <= 15 , M <= 100000000)。
Output
可以完成任务的卡片数。
Sample Input
2 4
Sample Output
12
Hint
这12张卡片分别是:
(1, 1, 4), (1, 2, 4), (1, 3, 4), (1, 4, 4), (2, 1, 4), (2, 3, 4),
(3, 1, 4), (3, 2, 4), (3, 3, 4), (3, 4, 4), (4, 1, 4), (4, 3, 4)
a1*x1+a2*x2+a3*x3+……+anxn+M*x(n+1)=1
(a1,a2,a3,……,an,M)=1 排除公因子非1的情况 总共的情况数是M^n
a1-an都是小于等于M
初始化出M范围内所有的素因子 枚举所有范围内的素因子
例如 有素因子2 所以M内有M/2个数有2这个素因子 有素因子3 所以M内有M/3个数有3这个素因子
所以重复计算了6这个因子 多算了M/6个数 诸如此类。。。
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <string.h> #include <string> #include <vector> #include <queue> #define MEM(a,x) memset(a,x,sizeof a) #define eps 1e-8 #define MOD 10009 #define MAXN 10010 #define MAXM 100010 #define INF 99999999 #define ll long long #define bug cout<<"here"<<endl #define fread freopen("ceshi.txt","r",stdin) #define fwrite freopen("out.txt","w",stdout) using namespace std; ll Read() { char ch; ll a = 0; while((ch = getchar()) == ' ' | ch == '\n'); a += ch - '0'; while((ch = getchar()) != ' ' && ch != '\n') { a *= 10; a += ch - '0'; } return a; } void Prll(ll a) //输出外挂 { if(a>9) Prll(a/10); putchar(a%10+'0'); } ll p[200],tot; ll ans,tmp; ll a[200],m,n; void divide(ll x) { tot=0; for(ll i=2;i*i<=x;i++) { if(x%i==0) { tot++; p[tot]=i; while(x%i==0) x/=i; } } if(x!=1) { tot++; p[tot]=x; } } ll mult(ll a,ll b) { ll x=1; while(b) { x*=a; b--; } return x; } void dfs(ll x,ll cnt,ll c)//共有c个公共因子 { if(cnt==c) { ll num=m; for(ll i=1;i<=c;i++) num/=a[i]; tmp+=mult(num,n); return; } for(ll i=x+1;i<=tot;i++) { a[cnt+1]=p[i]; dfs(i,cnt+1,c); } } int main() { //fread; while(scanf("%lld%lld",&n,&m)!=EOF) { ans=tot=0; divide(m); for(ll i=1;i<=tot;i++) { tmp=0; dfs(0,0,i); if(i&1) ans+=tmp; else ans-=tmp; } ans=mult(m,n)-ans; printf("%lld\n",ans); } return 0; }
相关文章推荐
- POJ 1091 跳蚤
- poj 1091 跳蚤(分解质因数+容斥)
- POJ 1091 跳蚤
- poj 1091 跳蚤
- POJ-1091:[NOIP模拟](二)T2-跳蚤
- POJ 1091 跳蚤(容斥原理)
- POJ 1091 跳蚤
- [NOIP2017模拟][poj1091]跳蚤
- POJ 1091 跳蚤
- poj 1091 跳蚤(组合数学-容斥原理)这题要求的数据范围根本就是胡说八道!
- POJ 1091 跳蚤(经过三天的研究,终于真的把它搞明白了)
- POJ 1091 - 跳蚤 (容斥)
- POJ 1091 跳蚤 [容斥原理]【组合数学】
- POJ 1091 跳蚤(分解质因数 + 容斥 + 大数)
- POJ 1091 跳蚤
- POJ 1091 跳蚤
- poj 1091 跳蚤
- poj 1091 跳蚤 扩展欧几里得+容斥原理
- POJ 1091 跳蚤
- poj 1091-跳蚤(数论+容斥)