Codeforces 510D.Fox And Jumping By Assassin 数论+状压dp
2016-11-22 21:13
288 查看
Description
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
Input
The first line contains an integer n (1 ≤ n ≤ 300), number of cards.
The second line contains n numbers li (1 ≤ li ≤ 109), the jump lengths of cards.
The third line contains n numbers ci (1 ≤ ci ≤ 105), the costs of cards.
Output
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
Sample Input
Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237
思路:
本来死活搞不懂这个是怎么实现的,花了一下午才看明白,数学真是差啊!参考的大牛见此。
大牛在此
说一下思路是什么,首先这个题目无疑,是选择任意个牌可以组成k1*x1+k2*x2+…+kn*xn=1最优情况,那么我们需要知道:
1.gcd(x1,x2,x3..,xn)=1,那么必有k1*x1+k2*x2+…+xn=1
2.如果x<=1e9,那么x最多只会由9种质数组成
那么每一个数都可以分解成几个质数相乘的结果,可以用状态压缩表示不包含哪个质数,dp[i]就表示,当前状态下从所以其他数在选择后存在分解的质数状态的最优解。最终选择如dp[(111)B]表示选择的数没有包含三个分解质数的最优解,通过修改k1,k2,k3数值可以组成一个和当前值互的一个数,然后就存在,多项式和为1了!
枚举单独匹配的那个数即可,即第一层循环
复杂度n^2*512
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
Input
The first line contains an integer n (1 ≤ n ≤ 300), number of cards.
The second line contains n numbers li (1 ≤ li ≤ 109), the jump lengths of cards.
The third line contains n numbers ci (1 ≤ ci ≤ 105), the costs of cards.
Output
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
Sample Input
Input
3
100 99 9900
1 1 1
Output
2
Input
5
10 20 30 40 50
1 1 1 1 1
Output
-1
Input
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
Output
6
Input
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
Output
7237
思路:
本来死活搞不懂这个是怎么实现的,花了一下午才看明白,数学真是差啊!参考的大牛见此。
大牛在此
说一下思路是什么,首先这个题目无疑,是选择任意个牌可以组成k1*x1+k2*x2+…+kn*xn=1最优情况,那么我们需要知道:
1.gcd(x1,x2,x3..,xn)=1,那么必有k1*x1+k2*x2+…+xn=1
2.如果x<=1e9,那么x最多只会由9种质数组成
那么每一个数都可以分解成几个质数相乘的结果,可以用状态压缩表示不包含哪个质数,dp[i]就表示,当前状态下从所以其他数在选择后存在分解的质数状态的最优解。最终选择如dp[(111)B]表示选择的数没有包含三个分解质数的最优解,通过修改k1,k2,k3数值可以组成一个和当前值互的一个数,然后就存在,多项式和为1了!
枚举单独匹配的那个数即可,即第一层循环
复杂度n^2*512
#include<bits/stdc++.h> #define input freopen("input.txt","r",stdin) using namespace std; const int inf = 0x3f3f3f3f; int use[12],value[330][2],n; //value左边存步长值,右边存花销 int dp[1<<10]; int main(){ int i,j,k; while(scanf("%d",&n)!=EOF){ for(i=1;i<=n;i++){ scanf("%d",&value[i][0]); } for(i=1;i<=n;i++){ scanf("%d",&value[i][1]); } int ans=inf; //初始化 for(i=1;i<=n;i++){ memset(dp,inf,sizeof(dp)); //每次更新一下 dp[0]=0; int pos=0,t=value[i][0]; for(j=2;j*j<=value[i][0];j++){ //提取素因数 if(t%j==0){ use[pos++]=j; while(t%j==0) t/=j; } } if(t!=1) use[pos++]=t; //存在较大的素因数 for(j=1;j<=n;j++){ int x=0; for(k=0;k<pos;k++){ if(value[j][0]%use[k]){ //标记不存在该因数为1 x^=(1<<k); } } for(int S=0;S<(1<<pos);S++){ if(dp[S] <inf)dp[S|x]=min(dp[S|x],dp[S]+value[j][1]); //如果开始dp[s]!=inf说明之前在当前状态可达 } } ans=min(ans,dp[(1<<pos)-1]+value[i][1]); //更新 } if(ans==inf) cout<<-1<<endl; else cout<<ans<<endl; } return 0; }
相关文章推荐
- Codeforces 453B B. Little Pony and Harmony Chest(dp+数论)
- codeforces 251C C. Number Transformation(数论+dp)
- codeforces 55D beautiful number [数学+数位DP]【动态规划+数论】
- 【bzoj4903/uoj300】[CTSC2017]吉夫特 数论+状压dp
- Codeforces 264B 数论+DP
- CodeForces 293 B.Distinct Paths(dfs+状压DP)
- Codeforces 454D Little Pony and Harmony Chest【思维+状压Dp+记录路径】好题!
- codeforces 453B B. Little Pony and Harmony Chest(dp+数论)
- Codeforces 417D Cunning Gena【排序+状压dp】
- CodeForces 71 C.Round Table Knights(数论+dp)
- codeforces 55D D. Beautiful numbers(数位dp+数论)
- Codeforces 743E(二分+状压dp)
- Codeforces 55D Beautiful numbers 数位dp 数论 优化技巧
- CodeForces 55D Beautiful numbers(数位dp+数论)
- HDU 6125 Free from square(数论+状压DP)
- Codeforces 624D Array Gcd(数论+dp)
- codeforces 698 C. LRU (概率与期望+状压DP)
- codeforces 16E 概率+状压dp
- CodeForces 623 B.Array GCD(数论+dp)
- codeforces 480C C. Riding in a Lift(dp)