数学简单专题
http://vjudge.net/vjudge/contest/view.action?cid=52710#overview
A - Cube HDU 1220
简单推导
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define MAX(a,b) (a > b ? a : b) 17 #define MIN(a,b) (a < b ? a : b) 18 #define mem0(a) memset(a,0,sizeof(a)) 19 20 //typedef long long LL; 21 typedef __int64 LL; 22 const double eps = 1e-12; 23 const int MAXN = 200005; 24 const int MAXM = 5005; 25 26 int N; 27 28 int calc() 29 { 30 if(N == 1) return 0; 31 if(N == 2) return 16; 32 int tot = (N*N*N) * (N*N*N-1) / 2; 33 int sub = 8 * 3 + 48 * (N-2) + 30 * (N-2) * (N-2) + (N-2)*(N-2)*(N-2) * 6; 34 return tot - sub / 2; 35 } 36 37 int main() 38 { 39 //freopen("in.txt", "r", stdin); 40 //freopen("out.txt", "w", stdout); 41 while(~scanf("%d", &N)) 42 { 43 printf("%d\n", calc()); 44 } 45 return 0; 46 }
B - Points on Cycle HDU 1700
计算下,然后考虑特殊情况(精度开太高居然WA。。。。)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define inf (-((LL)1<<40)) 17 #define lson k<<1, L, mid 18 #define rson k<<1|1, mid+1, R 19 #define mem0(a) memset(a,0,sizeof(a)) 20 #define mem1(a) memset(a,-1,sizeof(a)) 21 #define mem(a, b) memset(a, b, sizeof(a)) 22 #define FOPENIN(IN) freopen(IN, "r", stdin) 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 24 template<class T> T CMP_MIN ( T a, T b ) { return a < b; } 25 template<class T> T CMP_MAX ( T a, T b ) { return a > b; } 26 template<class T> T MAX ( T a, T b ) { return a > b ? a : b; } 27 template<class T> T MIN ( T a, T b ) { return a < b ? a : b; } 28 template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; } 29 template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b; } 30 template<class T> void SWAP( T& a, T& b ) { T t = a; a = b; b = t; } 31 32 typedef __int64 LL; 33 //typedef long long LL; 34 const int MAXN = 100010; 35 const int MAXM = 110000; 36 const double eps = 1e-7; 37 const double PI = 4.0 * (double)atan(1.0); 38 39 int main() 40 { 41 int T; 42 scanf("%d", &T); 43 while(T--) 44 { 45 double x, y; 46 scanf("%lf %lf", &x, &y); 47 double sita, r = sqrt(x * x + y * y); 48 if(fabs(x) < eps) { 49 if(y > 0) sita = PI / 2; 50 else sita = -PI / 2; 51 } 52 else sita = atan(y / x); 53 if(x < 0) sita += PI; 54 55 double x1, y1, x2, y2; 56 sita += 2.0 * PI / 3.0; 57 x1 = r * cos(sita); 58 y1 = r * sin(sita); 59 60 sita += 2.0 * PI / 3.0; 61 x2 = r * cos(sita); 62 y2 = r * sin(sita); 63 64 if(y1 - y2 > eps) SWAP(x1, x2), SWAP(y1,y2); 65 else if(fabs(y1 - y2) < eps && x1 - x2 > eps) SWAP(x1, x2), SWAP(y1,y2); 66 67 printf("%.3lf %.3lf %.3lf %.3lf\n", x1, y1, x2, y2); 68 } 69 return 0; 70 }
C - Eddy's 洗牌问题HDU 1210
模拟下洗牌过程
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define inf (-((LL)1<<40)) 17 #define lson k<<1, L, mid 18 #define rson k<<1|1, mid+1, R 19 #define mem0(a) memset(a,0,sizeof(a)) 20 #define mem1(a) memset(a,-1,sizeof(a)) 21 #define mem(a, b) memset(a, b, sizeof(a)) 22 #define FOPENIN(IN) freopen(IN, "r", stdin) 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 24 template<class T> T CMP_MIN ( T a, T b ) { return a < b; } 25 template<class T> T CMP_MAX ( T a, T b ) { return a > b; } 26 template<class T> T MAX ( T a, T b ) { return a > b ? a : b; } 27 template<class T> T MIN ( T a, T b ) { return a < b ? a : b; } 28 template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; } 29 template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b; } 30 template<class T> void SWAP( T& a, T& b ) { T t = a; a = b; b = t; } 31 32 typedef __int64 LL; 33 //typedef long long LL; 34 const int MAXN = 100010; 35 const int MAXM = 110000; 36 const double eps = 1e-14; 37 const double PI = 4.0 * (double)atan(1.0); 38 39 int main() 40 { 41 int N; 42 while(~scanf("%d", &N)) 43 { 44 int ans = 0, x = 1; 45 do 46 { 47 if(x <= N) x <<= 1; 48 else x = (x-N)*2-1; 49 ans ++; 50 }while(x != 1); 51 printf("%d\n", ans); 52 } 53 return 0; 54 }
D - Delta-wave HDU 1030
算出水平方向,左斜方向,右斜方向的所处的层数,相减取绝对值即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define inf (-((LL)1<<40)) 17 #define lson k<<1, L, mid 18 #define rson k<<1|1, mid+1, R 19 #define mem0(a) memset(a,0,sizeof(a)) 20 #define mem1(a) memset(a,-1,sizeof(a)) 21 #define mem(a, b) memset(a, b, sizeof(a)) 22 #define FOPENIN(IN) freopen(IN, "r", stdin) 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 24 template<class T> T ABS ( T a) { return a >= 0 ? a : -a; } 25 template<class T> T CMP_MIN ( T a, T b ) { return a < b; } 26 template<class T> T CMP_MAX ( T a, T b ) { return a > b; } 27 template<class T> T MAX ( T a, T b ) { return a > b ? a : b; } 28 template<class T> T MIN ( T a, T b ) { return a < b ? a : b; } 29 template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; } 30 template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b; } 31 template<class T> void SWAP( T& a, T& b ) { T t = a; a = b; b = t; } 32 33 typedef __int64 LL; 34 //typedef long long LL; 35 const int MAXN = 100010; 36 const int MAXM = 110000; 37 const double eps = 1e-14; 38 const double PI = 4.0 * (double)atan(1.0); 39 40 void get(LL &x, LL &y, LL &z, int n) 41 { 42 for(x=1;x*x<n;x++); 43 y = (n - (x-1)*(x-1) - 1) / 2 + 1; 44 z = (x*x - n) / 2 + 1; 45 } 46 47 int main() 48 { 49 int N, M; 50 while(~scanf("%d %d", &N, &M)) 51 { 52 LL ax, ay, az, bx, by, bz; 53 get(ax, ay, az, N); 54 get(bx, by, bz, M); 55 printf("%I64d\n", ABS(ax-bx) + ABS(ay-by) + ABS(az-bz)); 56 } 57 return 0; 58 }
F - 猜数字 HDU 2178
YY
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define MAX(a,b) (a > b ? a : b) 17 #define MIN(a,b) (a < b ? a : b) 18 #define mem0(a) memset(a,0,sizeof(a)) 19 20 //typedef long long LL; 21 typedef __int64 LL; 22 const double eps = 1e-12; 23 const int MAXN = 200005; 24 const int MAXM = 5005; 25 26 27 int main() 28 { 29 //freopen("in.txt", "r", stdin); 30 //freopen("out.txt", "w", stdout); 31 int t, N; 32 while(~scanf("%d", &t))while(t--) 33 { 34 scanf("%d", &N); 35 printf("%d\n", (1<<N)-1); 36 } 37 return 0; 38 }
G - Catching the ThiefHDU 3469
由于警察是足够聪明的,所以他可以选择从第二个城市开始一直到倒数第二个结束每个呆两天(可以保证左边绝对不会有小偷)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define inf (-((LL)1<<40)) 17 #define lson k<<1, L, mid 18 #define rson k<<1|1, mid+1, R 19 #define mem0(a) memset(a,0,sizeof(a)) 20 #define mem1(a) memset(a,-1,sizeof(a)) 21 #define mem(a, b) memset(a, b, sizeof(a)) 22 #define FOPENIN(IN) freopen(IN, "r", stdin) 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 24 template<class T> T ABS ( T a) { return a >= 0 ? a : -a; } 25 template<class T> T CMP_MIN ( T a, T b ) { return a < b; } 26 template<class T> T CMP_MAX ( T a, T b ) { return a > b; } 27 template<class T> T MAX ( T a, T b ) { return a > b ? a : b; } 28 template<class T> T MIN ( T a, T b ) { return a < b ? a : b; } 29 template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; } 30 template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b; } 31 template<class T> void SWAP( T& a, T& b ) { T t = a; a = b; b = t; } 32 33 typedef __int64 LL; 34 //typedef long long LL; 35 const int MAXN = 100010; 36 const int MAXM = 110000; 37 const double eps = 1e-14; 38 const double PI = 4.0 * (double)atan(1.0); 39 40 int main() 41 { 42 int N, t, T=0; 43 while(~scanf("%d", &t))while(t--) 44 { 45 scanf("%d", &N); 46 int ans; 47 if(N == 1) ans = 1; 48 else if(N == 2) ans = 2; 49 else ans = (N - 2) * 2; 50 printf("Case %d: %d\n", ++T, ans); 51 } 52 return 0; 53 }
H - Combinations, Once AgainPOJ 1285
建立状态F[i][j]表示第i个数选择j个的总方案数,可以枚举第i个数选了多少个,那么
F[i][j] = sum{DP[i-1][j-k] | k<=min(n[i], j) }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 //#include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define inf (-((LL)1<<40)) 17 #define lson k<<1, L, mid 18 #define rson k<<1|1, mid+1, R 19 #define mem0(a) memset(a,0,sizeof(a)) 20 #define mem1(a) memset(a,-1,sizeof(a)) 21 #define mem(a, b) memset(a, b, sizeof(a)) 22 #define FOPENIN(IN) freopen(IN, "r", stdin) 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 24 template<class T> T ABS ( T a) { return a >= 0 ? a : -a; } 25 template<class T> T CMP_MIN ( T a, T b ) { return a < b; } 26 template<class T> T CMP_MAX ( T a, T b ) { return a > b; } 27 template<class T> T MAX ( T a, T b ) { return a > b ? a : b; } 28 template<class T> T MIN ( T a, T b ) { return a < b ? a : b; } 29 template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; } 30 template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b; } 31 template<class T> void SWAP( T& a, T& b ) { T t = a; a = b; b = t; } 32 33 typedef __int64 LL; 34 //typedef long long LL; 35 const int MAXN = 100; 36 const int MAXM = 110000; 37 const double eps = 1e-14; 38 const double PI = 4.0 * atan(1.0); 39 40 LL DP[MAXN][MAXN]; 41 int n[MAXN]; 42 int N, M; 43 44 int main() 45 { 46 int cas = 0; 47 while(~scanf("%d%d", &N, &M) && (N || M)) 48 { 49 int x; 50 mem0(n); 51 for(int i = 0; i < N; i ++ ) 52 { 53 scanf("%d", &x); 54 n[x] ++; 55 } 56 mem0(DP); 57 for(int i = 0; i <= N; i ++) 58 DP[i][0] = 1; 59 for(int i = 1; i<= N; i ++) 60 { 61 for(int j = 1; j <= N; j ++) 62 { 63 for(int k = 0; k <= n[i] && k <= j; k ++) 64 DP[i][j] += DP[i-1][j-k]; 65 } 66 } 67 printf("Case %d:\n", ++cas); 68 while(M--) 69 { 70 scanf("%d", &x); 71 printf("%I64d\n", DP[N][x]); 72 } 73 } 74 return 0; 75 }
I - Self NumbersPOJ 1316
模拟一遍即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 //#include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define inf (-((LL)1<<40)) 17 #define lson k<<1, L, mid 18 #define rson k<<1|1, mid+1, R 19 #define mem0(a) memset(a,0,sizeof(a)) 20 #define mem1(a) memset(a,-1,sizeof(a)) 21 #define mem(a, b) memset(a, b, sizeof(a)) 22 #define FOPENIN(IN) freopen(IN, "r", stdin) 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 24 template<class T> T ABS ( T a) { return a >= 0 ? a : -a; } 25 template<class T> T CMP_MIN ( T a, T b ) { return a < b; } 26 template<class T> T CMP_MAX ( T a, T b ) { return a > b; } 27 template<class T> T MAX ( T a, T b ) { return a > b ? a : b; } 28 template<class T> T MIN ( T a, T b ) { return a < b ? a : b; } 29 template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; } 30 template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b; } 31 template<class T> void SWAP( T& a, T& b ) { T t = a; a = b; b = t; } 32 33 typedef __int64 LL; 34 //typedef long long LL; 35 const int MAXN = 10005; 36 const int MAXM = 110000; 37 const double eps = 1e-14; 38 const double PI = 4.0 * atan(1.0); 39 40 int vis[MAXN]; 41 42 int sum(int x) 43 { 44 int ans = 0; 45 while(x) ans += x%10, x /= 10; 46 return ans; 47 } 48 49 int main() 50 { 51 //FOPENIN("in.txt"); 52 for(int i = 1; i <= 10000; i ++) 53 { 54 int x = i; 55 do 56 { 57 x += sum(x); 58 vis[x] = 1; 59 }while(x <= 10000 && !vis[x]); 60 } 61 for(int i=1;i<=10000;i++)if(!vis[i]) 62 { 63 printf("%d\n", i); 64 } 65 return 0; 66 }
J - Prime Test POJ 1811
米勒拉宾素数测试
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 //#include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define inf ((LL)1<<40) 17 #define lson k<<1, L, mid 18 #define rson k<<1|1, mid+1, R 19 #define mem0(a) memset(a,0,sizeof(a)) 20 #define mem1(a) memset(a,-1,sizeof(a)) 21 #define mem(a, b) memset(a, b, sizeof(a)) 22 #define FOPENIN(IN) freopen(IN, "r", stdin) 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 24 template<class T> T ABS ( T a) { return a >= 0 ? a : -a; } 25 template<class T> T CMP_MIN ( T a, T b ) { return a < b; } 26 template<class T> T CMP_MAX ( T a, T b ) { return a > b; } 27 template<class T> T MAX ( T a, T b ) { return a > b ? a : b; } 28 template<class T> T MIN ( T a, T b ) { return a < b ? a : b; } 29 template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; } 30 template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b; } 31 template<class T> void SWAP( T& a, T& b ) { T t = a; a = b; b = t; } 32 33 typedef __int64 LL; 34 //typedef long long LL; 35 const int MAXN = 10005; 36 const int MAXM = 110000; 37 const double eps = 1e-14; 38 const double PI = 4.0 * atan(1.0); 39 40 41 42 43 LL n, ans; 44 int t; 45 46 47 48 49 #define Times 10 50 51 //产生[0, n-1]的一个随机数 52 LL random(LL n) 53 { 54 return ((double)rand() / RAND_MAX * n + 0.5); 55 } 56 //乘法,采用加法模拟,避免中间结果超出LL 57 LL multi(LL a,LL b,LL mod) 58 { 59 LL ans=0; 60 while(b) 61 { 62 if(b & 1) 63 { 64 b--; 65 ans=(ans+a) % mod; 66 } 67 else 68 { 69 b/=2; 70 a=(2*a) % mod; 71 } 72 } 73 return ans; 74 } 75 76 //快速幂,同样避免超出LL的做法 77 LL Pow(LL a, LL b, LL mod) 78 { 79 LL ans=1; 80 while(b) 81 { 82 if(b&1) 83 { 84 b--; 85 ans=multi(ans,a,mod); 86 } 87 else 88 { 89 b/=2; 90 a=multi(a,a,mod); 91 } 92 } 93 return ans; 94 } 95 96 //miller_rabin的一遍探测,返回false表示是合数 97 bool witness(LL a,LL n) 98 { 99 LL d=n-1; 100 while( !(d&1) ) 101 d >>= 1; 102 LL t=Pow(a, d, n); 103 while(d!=n-1 && t!=1 && t!=n-1) 104 { 105 t=multi(t,t,n); 106 d<<=1; 107 } 108 return t==n-1 || d&1; 109 } 110 111 //miller_rabin算法,返回false表示是合数,否则是素数 112 //返回素数出错的概率(最高)为 1 / (4 ^ times) 113 bool miller_rabin(LL n) 114 { 115 if(n == 2) 116 return true; 117 if( n<2 || !(n&1) ) 118 return false; 119 for(int i = 1; i <= Times ; i++ ) 120 { 121 LL a = random(n-2) + 1; 122 if( !witness(a, n) ) 123 return false; 124 } 125 return true; 126 } 127 128 129 //************************************************ 130 //pollard_rho 算法进行质因数分解 131 //************************************************ 132 LL factor[100];//质因数分解结果(刚返回时是无序的) 133 int tol;//质因数的个数。数组小标从0开始 134 135 LL gcd(LL a,LL b) 136 { 137 if(a==0)return 1;//??????? 138 if(a<0) return gcd(-a,b); 139 while(b) 140 { 141 LL t=a%b; 142 a=b; 143 b=t; 144 } 145 return a; 146 } 147 148 LL Pollard_rho(LL x, LL c) 149 { 150 LL i=1,k=2; 151 LL x0=rand()%x; 152 LL y=x0; 153 while(1) 154 { 155 i++; 156 x0=(multi(x0, x0, x) + c) % x; 157 LL d=gcd(y-x0,x); 158 if(d!=1&&d!=x) return d; 159 if(y==x0) return x; 160 if(i==k){y=x0;k+=k;} 161 } 162 } 163 164 //对n进行素因子分解 165 void findfac(LL n) 166 { 167 if(miller_rabin(n))//素数 168 { 169 factor[tol++]=n; 170 ans = MIN(ans, n); 171 return; 172 } 173 LL p=n; 174 while(p>=n) 175 p=Pollard_rho(p, rand()%(n-1)+1); 176 findfac(p); 177 findfac(n/p); 178 } 179 180 181 182 int main() 183 { 184 //FOPENIN("in.txt"); 185 while(~scanf("%d", &t))while(t--) 186 { 187 ans = inf; 188 scanf("%I64d", &n); 189 findfac(n); 190 if(miller_rabin(n)) 191 printf("Prime\n"); 192 else printf("%I64d\n", ans); 193 } 194 return 0; 195 }