【AStar】初赛第一场
1. All X
1.1 基本思路
k和c的范围都不大,因此可以考虑迭代找循环节,然后求余数,判定是否相等。这题还是挺简单的。
1.2 代码
1 /* 5690 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <bitset> 12 #include <algorithm> 13 #include <cstdio> 14 #include <cmath> 15 #include <ctime> 16 #include <cstring> 17 #include <climits> 18 #include <cctype> 19 #include <cassert> 20 #include <functional> 21 #include <iterator> 22 #include <iomanip> 23 using namespace std; 24 //#pragma comment(linker,"/STACK:102400000,1024000") 25 26 #define sti set<int> 27 #define stpii set<pair<int, int> > 28 #define mpii map<int,int> 29 #define vi vector<int> 30 #define pii pair<int,int> 31 #define vpii vector<pair<int,int> > 32 #define rep(i, a, n) for (int i=a;i<n;++i) 33 #define per(i, a, n) for (int i=n-1;i>=a;--i) 34 #define clr clear 35 #define pb push_back 36 #define mp make_pair 37 #define fir first 38 #define sec second 39 #define all(x) (x).begin(),(x).end() 40 #define SZ(x) ((int)(x).size()) 41 #define lson l, mid, rt<<1 42 #define rson mid+1, r, rt<<1|1 43 44 typedef long long LL; 45 const int maxn = 10005; 46 int x; 47 LL m; 48 int c, k; 49 int visit[maxn]; 50 51 int main() { 52 ios::sync_with_stdio(false); 53 #ifndef ONLINE_JUDGE 54 freopen("data.in", "r", stdin); 55 freopen("data.out", "w", stdout); 56 #endif 57 58 int t; 59 int tmp, ntmp; 60 LL i, cycle; 61 62 scanf("%d", &t); 63 rep(tt, 1, t+1) { 64 scanf("%d%I64d%d%d", &x,&m,&k,&c); 65 printf("Case #%d: ", tt); 66 memset(visit, -1, sizeof(visit)); 67 ntmp = x % k; 68 cycle = -1; 69 for (i=1; i<=m; ++i) { 70 if (visit[tmp=ntmp] != -1) { 71 cycle = i - visit[tmp]; 72 break; 73 } 74 visit[tmp] = i; 75 ntmp = (10*tmp + x) % k; 76 } 77 78 if (cycle == -1) { 79 puts(tmp==c ? "Yes":"No"); 80 } else { 81 LL n = ((m - visit[tmp]) % cycle + cycle) % cycle; 82 for (i=0; i<n; ++i) 83 tmp = (10*tmp + x) % k; 84 puts(tmp==c ? "Yes":"No"); 85 } 86 } 87 88 #ifndef ONLINE_JUDGE 89 printf("time = %ldms. ", clock()); 90 #endif 91 92 return 0; 93 }
2. Sitting in Line
2.1 基本思路
N的范围很小,刚开始以为这是一个数学题,后来发现状态DP可解。
$dp[st][n]$st表示当前状态(即当前已经加入数字),n表示当前排列的最末端数字。显然有状态转移方程
[
dp[nst][k] = max(dp[nst][k], dp[st|(1<<k)][j]+a_j imes a_k), \
qquad ext{其中 } st&(1<<k) == 0 ext{ 并且 }pos_k==-1 || pos_k==getBits(st)
]
2.2 代码
1 /* 5691 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <bitset> 12 #include <algorithm> 13 #include <cstdio> 14 #include <cmath> 15 #include <ctime> 16 #include <cstring> 17 #include <climits> 18 #include <cctype> 19 #include <cassert> 20 #include <functional> 21 #include <iterator> 22 #include <iomanip> 23 using namespace std; 24 //#pragma comment(linker,"/STACK:102400000,1024000") 25 26 #define sti set<int> 27 #define stpii set<pair<int, int> > 28 #define mpii map<int,int> 29 #define vi vector<int> 30 #define pii pair<int,int> 31 #define vpii vector<pair<int,int> > 32 #define rep(i, a, n) for (int i=a;i<n;++i) 33 #define per(i, a, n) for (int i=n-1;i>=a;--i) 34 #define clr clear 35 #define pb push_back 36 #define mp make_pair 37 #define fir first 38 #define sec second 39 #define all(x) (x).begin(),(x).end() 40 #define SZ(x) ((int)(x).size()) 41 #define lson l, mid, rt<<1 42 #define rson mid+1, r, rt<<1|1 43 44 typedef long long LL; 45 LL INF = 0x3f3f3f3f3f3f3f3f; 46 LL NEG_INF = 0xc0c0c0c0c0c0c0c0; 47 const int maxn = 16; 48 LL dp[1<<maxn][maxn]; 49 int a[maxn], p[maxn]; 50 int Bits[1<<maxn]; 51 int n; 52 53 int getBits(int x) { 54 int ret = 0; 55 56 while (x) { 57 ret += x & 1; 58 x >>= 1; 59 } 60 61 return ret; 62 } 63 64 void init() { 65 const int mst = 1<<16; 66 rep(i, 0, mst) Bits[i] = getBits(i); 67 } 68 69 void solve() { 70 memset(dp, 0xC0, sizeof(dp)); 71 rep(i, 0, n) { 72 if (p[i]==-1 || p[i]==0) 73 dp[1<<i][i] = 0; 74 } 75 76 const int mst = 1<<n; 77 #ifndef ONLINE_JUDGE 78 printf("NEG_INF = %I64d ", NEG_INF); 79 #endif 80 rep(i, 0, mst) { 81 const int idx = Bits[i]; 82 rep(j, 0, n) { 83 if (dp[i][j] == NEG_INF) continue; 84 rep(k, 0, n) { 85 if (i & (1<<k)) continue; 86 if (p[k]==-1 || p[k]==idx) { 87 int nst = i | 1<<k; 88 dp[nst][k] = max(dp[nst][k], dp[i][j]+a[j]*a[k]); 89 } 90 } 91 } 92 } 93 94 LL ans = NEG_INF; 95 rep(j, 0, n) 96 ans = max(ans, dp[mst-1][j]); 97 printf("%I64d ", ans); 98 } 99 100 int main() { 101 ios::sync_with_stdio(false); 102 #ifndef ONLINE_JUDGE 103 freopen("data.in", "r", stdin); 104 freopen("data.out", "w", stdout); 105 #endif 106 107 int t; 108 109 init(); 110 scanf("%d", &t); 111 rep(tt, 1, t+1) { 112 scanf("%d", &n); 113 rep(i, 0, n) 114 scanf("%d%d", &a[i],&p[i]); 115 printf("Case #%d: ", tt); 116 solve(); 117 } 118 119 #ifndef ONLINE_JUDGE 120 printf("time = %ldms. ", clock()); 121 #endif 122 123 return 0; 124 }