hdu4340
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <cstdlib> #include <cmath> #include <stack> #include <map> #include <vector> #include <string> #include <algorithm> const double pi=cos(-1.); const double eps=10e-6; const double eps1=10e-9; const int inf=0x7fffffff; ///const int inf=0x3f3f3f3f; const long long infl=1ll<<62; ///******macro defination******/// #define cas(a) int a; scanf("%d", &a); while (a--) #define cas1(x, a) int a; scanf("%d", &a); for (int x=1; x<=a; ++x) #define int(a) int a; scanf("%d", &a) #define char(a) char a; scanf("%c", &a) #define strr(a, x) char a[x]; scanf("%s", &a) #define clean(a, x) memset (a, x, sizeof(a)); #define copy(a, b) memcpy(a, b, sizeof(a)); #define up(x,a) for(int x=0; x<a; ++x) #define down(x,a) for(int x=a-1; x>=0; --x) #define up1(x,a) for (int x=1; x<=a; ++x) #define debug(a) printf("here is %d!!!\n", a); ///*** mathmatics ***/// #define sqr(x) (x)*(x) #define abs(x) (x)>0?(x):(-(x)) #define zero(x) (x)<eps && (x)>eps ///****** by Geners ******/// typedef long long ll; typedef unsigned int UI; using namespace std; struct Edge{ int v, next; }edge[500]; double w[2][123]; int cnt, head[123]; void addedge(int u, int v) { edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; } void init() { clean(head, -1); cnt=0; } double dp[2][2][2][123]; /// 【父状态】 【当前状态】【子树中以有与当前状态相同的攻击起点】【当前点】 void dfs(int u, int fa) { //// debug(u); //// debug(fa); //printf("%d %d\n", u, fa); dp[0][0][u]=w[0][u]/2; dp[1][1][u]=w[1][u]/2; int son[123], scnt=0; for (int p=head[u]; ~p; p=edge[p].next) { if(edge[p].v==fa)continue; dfs(edge[p].v, u); son[scnt++]=edge[p].v; } double tmp0=0, tmp1=0; bool f0=false; bool f1=false; for (int i=0; i<scnt; ++i) { tmp0+=min(dp[0][1][son[i]], dp[0][0][son[i]]); if(dp[0][1][son[i]]>=dp[0][0][son[i]])f0=true; tmp1+=min(dp[1][0][son[i]], dp[1][0][son[i]]); if(dp[1][1][son[i]]<=dp[1][0][son[i]])f1=true; } dp[0][0][u]+=tmp0; dp[1][1][u]+=tmp1; if(f0)dp[1][0][u]=tmp0+w[0][u]/2; else { double mint=w[0][u]/2; for (int i=0; i<scnt; ++i) { mint=min(dp[0][0][son[i]]-dp[0][1][son[i]], mint); } dp[1][0][u]=tmp0+mint+w[0][u]/2; } if(f1)dp[0][1][u]=tmp1+w[1][u]/2; else { double mint=w[1][u]/2; for (int i=0; i<scnt; ++i) mint=min(dp[1][1][son[i]]-dp[1][0][son[i]], mint); dp[0][1][u]=tmp1+mint+w[1][u]/2; } } int main () { int n; while (~scanf("%d", &n)) { up(i, n)scanf("%lf", w[0]+i+1); up(i, n)scanf("%lf", w[1]+i+1); init(); for (int i=1; i<n; ++i) { int u, v; scanf("%d%d", &u, &v); addedge(u, v); addedge(v, u); } dfs(1, -1); for (int k=1; k<=n; ++k) { for (int i=0; i<2; ++i) { for (int j=0; j<2; ++j) { printf("dp[%d,%d,%d]=%lf ", i, j, k, dp[i][j][k]); } } puts(""); } double ans=dp[0][0][k] } return 0; } /* 3 1 2 5 3 8 1 1 2 1 3 4 1 2 5 1 3 8 1 3 1 4 4 2 4 3 */
hdu4341 有依赖的分组背包
int gcd(int x, int y) { return x?gcd(y%x, x):y; } struct node{ int x, y, t, v, d; }p[500]; bool cmp(node a, node b) { if(a.x==b.x && a.y==b.y)return a.d<b.d; if(a.x==b.x)return a.y<b.y; return a.x<b.x; } int dp[40000+123]; int main () { int n, ts; int cas=1; while (~scanf("%d%d", &n, &ts)) { for (int i=0; i<n; ++i) { scanf("%d%d%d%d", &p[i].x, &p[i].y, &p[i].t, &p[i].v); int d=gcd(abs(p[i].x), abs(p[i].y)); p[i].x/=d; p[i].y/=d; p[i].d=d; } sort(p, p+n, cmp); int r[222], l[222]; //debug(123); r[0]=0, l[0]=0; int cnt=0; for (int i=1; i<n; ++i) { if(p[i].x==p[i-1].x && p[i].y==p[i-1].y) { p[i].v+=p[i-1].v; p[i].t+=p[i-1].t; r[cnt]++; } else cnt++, r[cnt]=l[cnt]=i; } // for (int i=0; i<=cnt; ++i) // { // printf("%d %d\n", l[i], r[i]); // } clean(dp, 0); for (int i=0; i<=cnt; ++i) { for (int v=ts; v>=0; --v) { for (int k=l[i]; k<=r[i]; ++k) { if(v<p[k].t)continue; dp[v]=max(dp[v], dp[v-p[k].t]+p[k].v); } } } printf("Case %d: %d\n", cas++, dp[ts]); } return 0; }
hdu4342 History repeat itself 二分检索
const int maxn=70000; ll sqr[maxn]; void init() { for (int i=0; i<maxn; ++i)sqr[i]=(ll)i*i; } int main () { int cas; scanf("%d", &cas); init(); while (cas--) { ll n; scanf("%I64d", &n); ll l=1, r=2ll<<31; ll mid=-1; while (l<=r) { mid=(l+r)>>1; int p=lower_bound(sqr+1, sqr+maxn, mid)-sqr-1; //printf("mid=%I64d %d %I64d\n", mid, p, sqr[p]); if(mid-p==n) { if(sqr[p+1]==mid)mid++; break; } else if(mid-p>n)r=mid-1; else if(mid-p<n)l=mid+1; } ll ans=0; for (int i=1; sqr[i]<=mid; ++i) { ans+=i*(min(sqr[i+1], mid+1)-sqr[i]); } printf("%I64d %I64d\n", mid, ans); } return 0; }
hdu 4344 Mark the Rope pollard-rho 和MR素数检测模板题, 以前的模板出错了, 囧。
#include <stdio.h> #include <time.h> #include <string.h> #include <stdlib.h> #include <map> #define debug(a) printf("here is %lld!!!\n", a); typedef long long ll; typedef long long typec; using namespace std; typec gcd(typec a, typec b) { return b ? gcd(b, a % b) : a; } typec mul_mod(typec x, typec y, typec m) { typec res=0;x%=m; //printf("%lld %lld %lld\n", x, y, m); while (y) { if(y&1) res+=x, res%=m; x<<=1, y>>=1, x%=m; } return res; } typec power_mod(typec x, typec k, typec m) { typec res = 1; x%=m; while(k) { if(k&1) res=mul_mod(res, x, m);///res *= x, res %= m; x=mul_mod(x, x, m);///x *= x; k >>= 1; } return res; } bool MR_primality_test(typec x) { //puts("mr in"); bool flag = true; int k = 0, cnt = 30; ///cnt is test times typec a, q = x - 1, s; while(!(q&1)) k++, q >>= 1; while(cnt-- && flag) { a = rand() % (x - 1), a++; s = power_mod(a, q, x); if(s == 1) continue; for(int j = k; j && flag; j--) { if(s == x - 1) flag = false; s=mul_mod(s, s, x);///s *= s, s %= x; } flag = !flag; } return flag; } //bool MR_primality_test(typec n) //{ // int t = 0, cnt = 10; ///cnt is test times // typec a, u=n-1, x, y; // while(!(u&1)) t++, u >>= 1; // while(cnt--) // { // a=rand()%(n-1)+1; // x=power_mod(a, u, n); // for (int i=0; i<t; ++i) // { // y=mul_mod(x, x, n); // if(y==1 && x!=1 && x!=n-1)return false; // x=y; // } // if(y!=1)return false; // } // return true; //} inline bool is_prime(typec x) { if(x==2)return true; if(x<2 || !(x&1))return false; return MR_primality_test(x); } typec pollard_rho(typec n) { typec x, y, d, c = 3; while(true) { y = x = rand()%(n-1)+1; ll k=2, i=1; while(true) { i++; x=(mul_mod(x, x, n)+c)%n; //printf("%I64d %I64d %I64d\n", d, x, n); d = gcd((x - y + n)%n, n); if(d == n) break; if(d > 1 ) return d; if(i==k)k<<=1, y=x; } c++; } return 0; } map<ll, int>fac; map<ll, ll> facp; void find(ll n) { if(n==1)return ; ///printf("n===%I64d\n", n); if(is_prime(n)) { //debug(n); if(fac[n])facp[n]*=n; else facp[n]=n; fac[n]++; return; } ll t=pollard_rho(n); find(t); find(n/t); } int main() { freopen("1006.in", "r", stdin); freopen("1006a.out", "w", stdout); int cas; scanf("%d", &cas); //cas=5; while (cas--) { ll n; scanf("%I64d", &n); fac.clear(); facp.clear(); find(n); map<ll, ll>::iterator it; int cnt=0; ll ans=0; for (it=facp.begin(); it!=facp.end(); it++) { cnt++; ans+=it->second; } if(cnt>1) printf("%d %I64d\n", cnt, ans); else printf("%d %I64d\n", cnt, ans/facp.begin()->first); } return 0; } /* 5 180 198 199999991 99997973 19999593700018243 */