XDOJ
1000.a+b。
#include<bits/stdc++.h>
using namespace std; int a,b; int main()
{
ios::sync_with_stdio(false);
while(~scanf("%d%d",&a,&b)) printf("%d\n",a+b);
return ;
}
1001.不知道n和m大小,可以用一维数组处理位置,或者直接使用vector。
#include<bits/stdc++.h>
using namespace std; int n,m,q,a[],r[],c[];
string s; int main()
{
ios::sync_with_stdio(false);
int z = ;
while(cin >> n >> m)
{
cout << "Case #" << ++z << ":" << endl;
for(int i = ;i <= n;i++) r[i] = i;
for(int i = ;i <= m;i++) c[i] = i;
int cnt = ;
for(int i = ;i <= n;i++)
{
cin >> s;
for(int j = ;j < m;j++)
{
if(s[j] == 'T') a[++cnt] = ;
else if(s[j] == 'i') a[++cnt] = ;
else a[++cnt] = ;
}
}
cin >> q;
while(q--)
{
int x,y,z;
cin >> x >> y >>z;
if(x == )
{
int t = (r[y]-)*m+c[z];
switch(a[t])
{
case : cout << "Empty" << endl;break;
case : cout << "Tree" << endl;break;
case : cout << "Phone" << endl;break;
}
}
else if(x == ) swap(r[y],r[z]);
else swap(c[y],c[z]);
}
} return ;
}
#include<bits/stdc++.h>
using namespace std; int n,m,q,r[],c[];
vector<int> v[];
string s; int main()
{
ios::sync_with_stdio(false);
int z = ;
while(cin >> n >> m)
{
cout << "Case #" << ++z << ":" << endl;
for(int i = ;i <= n;i++) r[i] = i;
for(int i = ;i <= m;i++) c[i] = i;
for(int i = ;i <= n;i++)
{
v[i].clear();
v[i].push_back();
}
for(int i = ;i <= n;i++)
{
cin >> s;
for(int j = ;j < m;j++)
{
if(s[j] == 'T') v[i].push_back();
else if(s[j] == 'i') v[i].push_back();
else v[i].push_back();
}
}
cin >> q;
while(q--)
{
int x,y,z;
cin >> x >> y >>z;
if(x == )
{
switch(v[r[y]][c[z]])
{
case : cout << "Empty" << endl;break;
case : cout << "Tree" << endl;break;
case : cout << "Phone" << endl;break;
}
}
else if(x == ) swap(r[y],r[z]);
else swap(c[y],c[z]);
}
} return ;
}
1002.dp[x][y][z]代表前x段,y个绿,z个蓝造成的最大伤害,复杂度O(n^3)。
可以优化空间,优化到二维状态。
继续优化时间,可以贪心发现红放最末为最优,于是一开始全放红,dp[x][y]表示x个绿,y个蓝的最大伤害,复杂度O(n^2)。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL n,x,y,z,t;
LL dp[][][]; int main()
{
while(cin >> n)
{
cin >> x >> y >> z >> t;
memset(dp,,sizeof(dp));
dp[][][] = x*t;
for(int i = ;i <= n;i++)
{
for(int g = ;g <= i;g++)
{
int b = ;
for(b = ;b+g < i;b++) dp[i][g][b] = dp[i-][g][b]+(g*y+x)*(b*z+t);
if(g > ) dp[i][g][b] = max(dp[i][g][b],dp[i-][g-][b]+y*(g-)*(b*z+t));
if(b > ) dp[i][g][b] = max(dp[i][g][b],dp[i-][g][b-]+g*y*((b-)*z+t));
}
}
LL ans = ;
for(int i = ;i <= n;i++)
{
for(int j = ;j+i <= n;j++) ans = max(ans,dp[n][i][j]);
}
cout << ans << endl;
}
return ;
}
#include<bits/stdc++.h>
using namespace std; long long n,x,y,z,t,dp[][]; int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
cin >> x >> y >> z >> t;
dp[][] = x*t;
dp[][] = ;
dp[][] = ;
for(int i = ;i <= n;i++)
{
for(int g = i;g >= ;g--)
{
for(int b = i-g;b >= ;b--)
{
if(b+g != i) dp[g][b] = dp[g][b]+(g*y+x)*(b*z+t);
else dp[g][b] = ;
if(g > ) dp[g][b] = max(dp[g][b],dp[g-][b]+y*(g-)*(b*z+t));
if(b > ) dp[g][b] = max(dp[g][b],dp[g][b-]+g*y*((b-)*z+t));
}
}
}
long long ans = ;
for(int i = ;i <= n;i++)
{
for(int j = ;j+i <= n;j++) ans = max(ans,dp[i][j]);
}
cout << ans << endl;
}
return ;
}
#include<bits/stdc++.h>
using namespace std; long long n,x,y,z,t,dp[][]; int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
cin >> x >> y >> z >> t;
memset(dp,,sizeof(dp));
dp[][] = x*t*n;
for(int g = ;g <= n;g++)
{
for(int b = ;b+g <= n;b++)
{
if(g == && b == ) continue;
if(g > ) dp[g][b] = max(dp[g][b],dp[g-][b]-x*(b*z+t)+y*(b*z+t)*(n-g-b));
if(b > ) dp[g][b] = max(dp[g][b],dp[g][b-]-x*((b-)*z+t)+(g*y+x)*z*(n-g-b));
}
}
long long ans = ;
for(int i = ;i <= n;i++)
{
for(int j = ;j+i <= n;j++) ans = max(ans,dp[i][j]);
}
cout << ans << endl;
}
return ;
}
1003.高精度模拟。
#include<bits/stdc++.h>
using namespace std; int change1(char a)
{
if(a <= '') return a-'';
return a-'A'+;
} char change2(int a)
{
if(a <= ) return a+'';
else return a-+'A';
} string s1,s2;
int n,ans[]; int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
cin >> s1 >> s2;
int p1 = s1.length()-,p2 = s2.length()-,cnt = ,temp = ;
while(p1 >= && p2 >= )
{
int now = change1(s1[p1--])+change1(s2[p2--])+temp;
ans[++cnt] = now%n;
temp = now/n;
}
while(p1 >= )
{
int now = change1(s1[p1--])+temp;
ans[++cnt] = now%n;
temp = now/n;
}
while(p2 >= )
{
int now = change1(s2[p2--])+temp;
ans[++cnt] = now%n;
temp = now/n;
}
if(temp) ans[++cnt] = temp;
for(int i = cnt;i > ;i--) cout << change2(ans[i]);
cout << endl;
}
return ;
}
1004.每次固定一个对应点,旋转5次,暴力比较。
#include<bits/stdc++.h>
using namespace std; int a[],b[],c[],d[];
int point[][] = {{,},{,},{,},{,},{,},{,},{,},{,},{,},{,},{,},{,}};
int x[][] = {{,,,,,,,,,},{,,,,,,,,,},{,,,,,,,,,},{,,,,,,,,,},
{,,,,,,,,,},{,,,,,,,,,},{,,,,,,,,,},{,,,,,,,,,},
{,,,,,,,,,},{,,,,,,,,,},{,,,,,,,,,},{,,,,,,,,,}}; int ok()
{
for(int i = ;i < ;i++)
{
if(c[i] != d[i]) return ;
}
return ;
} int main()
{
ios::sync_with_stdio();
int T;
cin >>T;
while(T--)
{
for(int i = ;i <= ;i++) cin >> a[i];
for(int i = ;i <= ;i++) cin >> b[i];
for(int i = ;i < ;i++) c[i] = b[x[][i]];
int flag = ;
for(int i = ;i < ;i++)
{
if(a[point[i][]] == b[] && a[point[i][]] == b[])
{
for(int j = ;j < ;j++) d[j] = a[x[i][j]];
for(int j = ;j < ;j++)
{
if(ok()) flag = ;
int t = d[];
for(int k = ;k > ;k--) d[k] = d[k-];
d[] = t;
t = d[];
for(int k = ;k > ;k--) d[k] = d[k-];
d[] = t;
}
}
}
if(flag) cout << "Identical" << endl;
else cout << "Different" << endl;
}
return ;
}
1005.每次除2向上取整,直到最后一根。
#include<bits/stdc++.h>
using namespace std; long long n; int main()
{
while(cin >> n)
{
int cnt = ;
while(n > )
{
n = n/+n%;
cnt++;
}
cout << cnt << endl;
}
return ;
}
1006.按位运算的dp,a[i],b[i],c[i]分别对应^,|,&,表示从头到i当前位的累和。
#include<bits/stdc++.h>
using namespace std; int bin[][],two[],n;
double a[],b[],c[]; int main()
{
two[] = ;
for(int i = ;i <= ;i++) two[i] = two[i-]*;
while(cin >> n)
{
memset(bin,,sizeof(bin));
int maxcnt = ;
double ans1 = ,ans2 = ,ans3 = ;
for(int i = ,x;i <= n;i++)
{
cin >> x;
ans1 -= x;
ans2 -= x;
ans3 -= x;
int cnt = ;
while(x)
{
bin[i][++cnt] = x%;
x /= ;
}
maxcnt = max(cnt,maxcnt);
}
for(int j = ;j <= maxcnt;j++)
{
int last0 = ,last1 = ;
for(int i = ;i <= n;i++)
{
if(bin[i][j] == )
{
if(last1 > ) a[i] = a[last1-]+i-last1;
else a[i] = i;
b[i] = i-last0;
c[i] = i;
last1 = i;
}
else
{
if(i > ) a[i] = a[i-];
else a[i] = ;
b[i] = ;
c[i] = last1;
last0 = i;
}
ans1 += a[i]*two[j];
ans2 += b[i]*two[j];
ans3 += c[i]*two[j];
}
}
cout << fixed << setprecision() << ans1/n/n << " " << fixed << setprecision() << ans2/n/n << " " << fixed << setprecision() << ans3/n/n << endl;
}
return ;
}
1007.dp[i][j]表示i个蛋,j层需要的最多次数,另外可以发现k层最多需要logn+1个蛋,复杂度O(n^2*min(logn,k))。
发现对于每一个dp[i][j]的状态,并不需要枚举每一层,因为这是一个先递减,再递增的函数,二分找最低点即可,复杂度O(nlogn*min(logn,k))。
还可以继续优化很多,具体请参考这篇论文https://pan.baidu.com/s/1htXipWsA9mYKaSzhSNYlqw
#include<bits/stdc++.h>
using namespace std; int dp[][]; int main()
{
int n,k;
while(cin >> n >> k)
{
memset(dp,0x3f,sizeof(dp));
for(int i=;i<=n;i++)
{
dp[i][]=;
dp[i][]=;
}
for(int i=;i<=k;i++) dp[][i]=i;
for(int j=;j<=k;j++)
{
for(int i=;i<=n;i++)
{
for(int t=;t<=j;t++) dp[i][j]=min(dp[i][j],max(dp[i-][t-],dp[i][j-t])+);
}
}
cout << dp[n][k] << endl;
}
return ;
}
#include<bits/stdc++.h>
using namespace std; int n,k,dp[][]; int main()
{
ios::sync_with_stdio();
while(cin >> n >> k)
{
for(int i = ;i <= n;i++)
{
dp[i][] = ;
dp[i][] = ;
}
n = min(n,int(log(k)/log()+));
for(int i = ;i <= k;i++) dp[][i] = i;
for(int j = ;j <= k;j++)
{
for(int i = ;i <= n;i++)
{
int l = ,r = j;
while(l < r)
{
int mid = (l+r+)/;
if(dp[i-][mid-] <= dp[i][j-mid]) l = mid;
else r = mid-;
}
dp[i][j] = min(dp[i][j-l],dp[i-][l])+;
}
}
cout << dp[n][k] << endl;
}
return ;
}
1008.约瑟夫环。
#include<bits/stdc++.h>
using namespace std; int n,k;
int nextt[],lastt[]; int main()
{
while(cin >> n >> k)
{
for(int i = ;i < n;i++) nextt[i] = i+;
for(int i = ;i <= n;i++) lastt[i] = i-;
nextt[n] = ;
lastt[] = n;
cout << k;;
nextt[lastt[k]] = nextt[k];
lastt[nextt[k]] = lastt[k];
int cnt = n-,now = nextt[k];
while(cnt)
{
int left = (k-)%cnt;
while(left--) now = nextt[now];
cout << " " << now;
nextt[lastt[now]] = nextt[now];
lastt[nextt[now]] = lastt[now];
now = nextt[now];
cnt--;
}
cout << endl;
}
return ;
}
1009.约瑟夫环线段树实现,查找和更新一起做了。
#include<bits/stdc++.h>
using namespace std; struct segtree
{
int left,right,sum;
}tree[]; void build(int pos,int l,int r)
{
tree[pos].left = l;
tree[pos].right = r;
if(l == r)
{
tree[pos].sum = ;
return;
}
int mid = (l+r)/;
build(pos*,l,mid);
build(pos*+,mid+,r);
tree[pos].sum = tree[pos*].sum+tree[pos*+].sum;
} int update(int pos,int num)
{
tree[pos].sum--;
if(tree[pos].left == tree[pos].right) return tree[pos].left;
if(num <= tree[pos*].sum) return update(pos*,num);
else return update(pos*+,num-tree[pos*].sum);
} int main()
{ int n,m;
while(cin >> n >> m)
{
build(,,n);
cout << m;
int now = m;
update(,now);
for(int i = ;i <= n;i++)
{
now = (now+m-)%tree[].sum;
if(now == ) now = tree[].sum;
cout << " " << update(,now);
}
cout << endl;
}
return ;
}
1010.分为两段ab和cd,预处理,枚举中点,求两段的和的最大值。
#include<bits/stdc++.h>
using namespace std; int lmax[],rmax[],a[],n; int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = ;i <= n;i++) cin >> a[i];
int minn = a[];
lmax[] = -1e9;
for(int i = ;i <= n;i++)
{
lmax[i] = max(lmax[i-],a[i]-minn);
minn = min(minn,a[i]);
}
int maxx = a[n];
rmax[n] = -1e9;
for(int i = n-;i >= ;i--)
{
rmax[i] = max(rmax[i+],maxx-a[i]);
maxx = max(maxx,a[i]);
}
int ans = -1e9;
for(int i = ;i < n-;i++) ans = max(lmax[i]+rmax[i+],ans);
cout << ans << endl;
}
return ;
}
1011.规律。
#include<bits/stdc++.h>
using namespace std; long long n; int main()
{
ios::sync_with_stdio(false);
while(cin >> n)
{
long long y = sqrt(n);
if(y*y <= n) y++;
if(y%) cout << "even" << endl;
else cout << "odd" << endl;
}
return ;
}
1012.KMP的运用,先将串反转。
#include<bits/stdc++.h>
using namespace std; int nextt[];
string s; void get_next(int len)
{
int i = ,j = -;
nextt[] = -;
while(i < len)
{
if(j == - || s[len-i-] == s[len-j-]) nextt[++i] = ++j;
else j = nextt[j];
}
} int main ()
{
while(cin >> s)
{
int endd = s.length();
get_next(endd);
int maxlen = ,maxcnt = ;
for(int i = ;i <= endd;i++)
{
int len = i-nextt[i];
if(len && i%len == )
{
int cnt = i/len;
if(cnt >= maxcnt)
{
maxcnt = cnt;
maxlen = len;
}
}
}
if(maxcnt > ) cout << s.substr(endd-maxlen) << " " << maxcnt << endl;
else cout << - << endl;
}
return ;
}
1013.求一个累和。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int n,m,a[],b[]; int main()
{
while(cin >> n)
{
long long sum = ;
memset(b,,sizeof(b));
for(int i = ;i <= n;i++) cin >> a[i];
cin >> m;
for(int i = ,l,r,k;i <= m;i++)
{
cin >> l >> r >> k;
b[l+] -= k;
b[r+] += k;
}
for(int i = ;i <= n;i++)
{
b[i] += b[i-];
a[i] += b[i];
sum += a[i];
}
int ave = sum/n;
for(int i = ;i <= n;i++) a[i] -= ave;
sum = ;
for(int i = ;i <= n;i++) sum += (long long)a[i]*a[i];
cout << sum << endl;
}
return ;
}
1014.求最大值期望。
先将l,r和210转化为1-n的数,问题就成了求k次取1-n中的数,求最大值的数学期望。
参考:http://www.zhihu.com/question/30463955?sort=created
#include<bits/stdc++.h>
using namespace std; int l,r;
double x[]; int main()
{
while(cin >> l >> r)
{
if(l < && r <= )
{
cout << "stupid" << endl;
continue;
}
if(l >= )
{
cout << << endl;
continue;
}
int m = r-l+,ok = -l+;
for(int i = ;i <= m;i++) x[i] = ;
double now = ;
int ans = ;
while(m - now <= ok-1e-)
{
now = ;
for(int i = ;i <= m-;i++)
{
x[i] *= (double)i/m;
now += x[i];
}
ans++;
}
cout << ans << endl;
}
return ;
}
1015.先确定最多的位数,然后从高位开始贪心。
#include<bits/stdc++.h>
using namespace std; int n,w[]; int main()
{
ios::sync_with_stdio();
while(~scanf("%d",&n))
{
int minn = 1e9;
for(int i = ;i <= ;i++)
{
scanf("%d",&w[i]);
minn = min(minn,w[i]);
}
if(n < minn)
{
printf("-1\n");
continue;
}
int cnt = n/minn,t = n%minn,now = ;
while(n >= minn)
{
while(t+minn < w[now]) now--;
printf("%d",now);
n -= w[now];
t += minn-w[now];
}
printf("\n");
}
return ;
}
1016.a+b。
#include<bits/stdc++.h>
using namespace std; int main()
{
ios::sync_with_stdio(false);
long long a,b;
while(cin >> a >> b) cout << a+b << endl;
return ;
}
1017.排序。
#include<bits/stdc++.h>
using namespace std; int n;
struct aa
{
string name;
int rating;
friend bool operator <(aa x,aa y)
{
if(x.rating != y.rating) return x.rating > y.rating;
return x.name < y.name;
}
}a[]; int main()
{
ios::sync_with_stdio(false);
while(cin >> n && n)
{
for(int i = ;i < n;i++) cin >> a[i].name >> a[i].rating;
sort(a,a+n);
for(int i = ;i < n;i++) cout << a[i].name << " " << a[i].rating << endl;
}
return ;
}
1018.约瑟夫环数学结论。
#include<bits/stdc++.h>
using namespace std; int n,k,t; int fun(int x)
{
int a = (n-x+k)%(n-x+);
for(int i = ;i <= x;)
{
int temp = min(x+-i,(n-x+i-a-)/k+);
a = (a+k*temp)%(n-x+i+temp-);
i += temp;
}
return a;
}
int main()
{
while(cin >> n >> k >> t)
{
int x;
cin >> x;
cout << fun(x)+;
while(--t)
{
cin >> x;
cout << " " << fun(x)+;
}
cout << endl;
}
return ;
}
1019.末尾0由2*5构成,2足够多,看5的数量就可以了。注意25是2个5,125是3个5...先用等比公式推出趋于极限时,n = m*4,但实际可能比这个数大一点,再向上找。
或者直接二分找5的个数大于等于n的最小值,再判断。
#include<bits/stdc++.h>
using namespace std; int n; int f(int x)
{
int now = ,ans = ;
while(now <= x)
{
ans += x/now;
now *= ;
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--)
{
cin >> n;
int ans = n*;
while(f(ans) < n) ans++;
if(f(ans) == n) cout << ans << endl;
else cout << "No solution" << endl;
}
return ;
}
1020.概率dp,dp[i][j]表示前i题对j题的概率。
#include<bits/stdc++.h>
using namespace std; int n,k;
double a[],dp[][]; int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--)
{
cin >> n >> k;
for(int i = ;i <= n;i++) cin >> a[i];
dp[][] = -a[];
dp[][] = a[];
for(int i = ;i <= n;i++)
{
dp[i][] = dp[i-][]*(-a[i]);
for(int k = ;k <= n;k++) dp[i][k] = dp[i-][k]*(-a[i])+dp[i-][k-]*a[i];
}
double ans = ;
for(int i = k;i <= n;i++) ans += dp[n][i];
cout << fixed << setprecision() << ans << endl;
}
return ;
}
1021.分割成三角形,求面积和。
#include<bits/stdc++.h>
#define PI atan(1)*4
using namespace std; int main()
{
int T;
cin >> T;
while(T--)
{
int n;
double x;
cin >> n >> x;
cout << fixed << setprecision() << 0.25*n*x*x/tan(PI/n) <<endl;
}
return ;
}
1022.统计每个数被加了多少次,两次前缀和。
#include<bits/stdc++.h>
using namespace std; int sum[];
int main()
{
ios::sync_with_stdio(false);
for(int i = ;i <= ;i++)
{
for(int j = i;j <= ;j += i) sum[j]++;
}
for(int i = ;i <= ;i++) sum[i] = (sum[i]+sum[i-])%;
for(int i = ;i <= ;i++) sum[i] = (sum[i]+sum[i-])%;
int a,b;
while(cin >> a >> b) cout << (sum[b]-sum[a-]+)% << endl;
return ;
}
1023.区间不交叉,直接排序,二分按r中找r满足最左段,判断是否在该段中。
#include<bits/stdc++.h>
using namespace std; int n,m;
struct city
{
int l,r,id;
city(){};
city(int ll,int rr,int idd):l(ll),r(rr),id(idd){};
friend bool operator <(city x,city y)
{
return x.r < y.r;
}
}a[]; int main()
{
ios::sync_with_stdio(false);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i <= n;i++) scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].id);
sort(a+,a+n+);
scanf("%d",&m);
while(m--)
{
int ip;
scanf("%d",&ip);
int ans = lower_bound(a+,a++n,city(,ip,))-a;
if(ans == n+ || ip < a[ans].l) printf("-1\n");
else printf("%d\n",a[ans].id);
}
}
return ;
}
1024.树状数组维护一下前缀和。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,tree[]; inline int lowbit(int x)
{
return x&-x;
} void add(int pos,int x)
{
while(pos <= )
{
tree[pos] += x;
pos += lowbit(pos);
}
} int getsum(int pos)
{
int sum = ;
while(pos > )
{
sum += tree[pos];
pos -= lowbit(pos);
}
return sum;
} int main()
{
ios::sync_with_stdio(false);
int T;
scanf("%d",&T);
while(T--)
{
memset(tree,,sizeof(tree));
scanf("%d",&n);
long long ans = ;
for(int i = ;i <= n;i++)
{
int x;
scanf("%d",&x);
add(x+,);
ans = (ans+i-getsum(x+))%MOD;
}
printf("%lld\n",ans);
}
return ;
}
1025.记忆化dp。
#include<bits/stdc++.h>
using namespace std; vector<int> make[];
int v[],dp[],n,m,w; int dfs(int x)
{
if(dp[x] != -) return dp[x];
dp[x] = v[x];
if(!make[x].empty())
{
int sum = w;
for(int i = ;i < make[x].size();i++) sum += dfs(make[x][i]);
dp[x] = min(dp[x],sum);
}
return dp[x];
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(dp,-,sizeof(dp));
scanf("%d%d%d",&n,&m,&w);
for(int i = ;i < n;i++) make[i].clear();
for(int i = ,cnt;i < n;i++)
{
scanf("%d%d",&v[i],&cnt);
for(int j = ;j <= cnt;j++)
{
int x;
scanf("%d",&x);
make[i].push_back(x);
}
}
int ans = ;
for(int i = ;i <= m;i++)
{
int x;
scanf("%d",&x);
ans += dfs(x);
}
printf("%d\n",ans);
}
return ;
}
1026.快速幂模版。
#include<bits/stdc++.h>
using namespace std; long long a,b,c; long long qpower(long long a, long long b, long long c)
{
long long ans = ;
a = a%c;
while(b)
{
if(b%) ans = ans*a%c;
a = a*a%c;
b /= ;
}
return ans;
} int main()
{
ios::sync_with_stdio();
while(cin >> a >> b >> c) cout << qpower(a,b,c) << endl;
return ;
}
1027.矩阵快速幂模版。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n;
struct matrix
{
long long m[][];
}; matrix one = { ,,
, };
matrix base = { ,,
, }; matrix mul(matrix a, matrix b)
{
matrix tmp;
for(int i = ; i < ;i++)
{
for(int j = ; j < ;j++)
{
tmp.m[i][j] = ;
for(int k = ; k < ;k++) tmp.m[i][j] = (tmp.m[i][j]+a.m[i][k]*b.m[k][j])%MOD;
}
}
return tmp;
} long long fast_mod(int n)
{
matrix ans = one,y = base;
while(n)
{
if(n&) ans = mul(ans,y);
y = mul(y,y);
n /= ;
}
return ans.m[][];
} int main()
{
while(cin >> n) cout << fast_mod(n-) << endl;
return ;
}
1028.素数筛,dp求最小花费。
#include<bits/stdc++.h>
using namespace std; int n,cnt = ,vis[] = {},prime[],dp[]; void getprime(int MAXN)
{
for(int i = ;i <= MAXN;i++)
{
if(!vis[i]) prime[++cnt] = i;
for(int j = ;j <= cnt && (long long)i*prime[j] <= MAXN;j++)
{
vis[prime[j]*i] = ;
if(!(i%prime[j])) break;
}
}
} int main()
{
getprime();
memset(dp,0x3f,sizeof(dp));
dp[] = ;
for(int i = ;i <= ;i++)
{
dp[i+] = min(dp[i]+,dp[i+]);
for(int j = ;j <= cnt && i*prime[j] <= ;j++)
{
int t = i*prime[j];
dp[t] = min(dp[i]+,dp[t]);
}
}
while(~scanf("%d",&n)) printf("%d\n",dp[n]);
return ;
}
1029.首位化log再化回来,末尾快速幂模10。
#include<bits/stdc++.h>
using namespace std; int n; long long PowerMod(int a, int b, int c)
{
long long ans = ;
a = a%c;
while(b > )
{
if(b % == ) ans = (ans*a)%c;
b = b/;
a = (a*a)%c;
}
return ans;
} int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
double x = log10()*n;
int ans = pow(,x-(int)x)+1e-;
cout << ans << " " << PowerMod(,n,) << endl;
}
return ;
}
1030.排序后枚举前两个,二分第三个。
#include<bits/stdc++.h>
using namespace std; int a[],n; int main()
{
ios::sync_with_stdio(false);
while(cin >> n)
{
int flag = ;
for(int i = ;i <= n;i++) cin >> a[i];
sort(a+,a++n);
for(int i = ;i <= n-;i++)
{
for(int j = i+;j <= n-;j++)
{
int sum = -a[i]-a[j];
int pos = lower_bound(a+j+,a+n+,sum)-a;
if(pos != n+ && a[pos] == sum)
{
flag = ;
cout << a[i] << " " << a[j] << " " << a[pos] << endl;
}
}
}
if(flag) cout << "Ren Chou Jiu Gai Duo Du Shu!" << endl;
}
return ;
}
1031.规律,a[i][j] = (i+j)/gcd(i,j)。
#include<bits/stdc++.h>
using namespace std; int n,m; int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m)
{
int ans = ;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++)
{
ans += (i+j)/__gcd(i,j);
}
}
cout << ans << endl;
}
return ;
}
1032.规律,组合数,lacus模版。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL n,m; LL PowerMod(LL a, LL b, LL c)
{
LL ans = ;
a = a % c;
while(b)
{
if(b&) ans = (ans*a)%c;
b = b>>;
a = (a*a)%c;
}
return ans;
} LL c(LL m,LL n)
{
if(m < n) return ;
if(m == n) return ;
if(n > m-n) n = m-n;
LL mm = ,nn = ;
for(LL i = ;i < n;i++)
{
mm = mm*(m-i)%;
nn = nn*(n-i)%;
}
return mm*PowerMod(nn,,)%;
} LL lucas(LL m,LL n)
{
LL ans = ;
while(m && n && ans)
{
ans = ans%*c(m%,n%)%;
n /= ;
m /= ;
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m) cout << lucas(m,n) << endl;
return ;
}
1033.直接比较。
#include<bits/stdc++.h>
using namespace std; int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--)
{
int a;
cin >> a;
if(a <= ) cout << "YES" << endl;
else cout << "NO" << endl;
}
return ;
}
1034.直接模拟。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
int n,T;
cin >> T;
while(T--)
{
cin >> n;
int sub = ,ans = ;
while(n > sub)
{
n -= sub;
ans++;
sub *= ;
}
if(n <= sub/) cout << ans- << " " << sub/ << endl;
else cout << ans << " " << n << endl;
}
return ;
}
1035.set去重判断。
#include<bits/stdc++.h>
using namespace std; int a[][];
int x[] = {,,,,,,,,},y[] = {,,,,,,,,}; set<int> s;
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--)
{
int flag = ;
for(int i = ;i <= ;i++)
{
s.clear();
for(int j = ;j <= ;j++)
{
cin >> a[i][j];
if(a[i][j] == || a[i][j] > ) flag = ;
s.insert(a[i][j]);
}
if(s.size() != ) flag = ;
}
for(int j = ;j <= ;j++)
{
s.clear();
for(int i = ;i <= ;i++) s.insert(a[i][j]);
if(s.size() != ) flag = ;
}
for(int k = ;k < ;k++)
{
s.clear();
for(int i = x[k],cnt1 = ;cnt1 < ;i++,cnt1++)
{
for(int j = y[k],cnt2 = ;cnt2 < ;j++,cnt2++) s.insert(a[i][j]);
}
if(s.size() != ) flag = ;
}
there:
if(flag) cout << "yes" << endl;
else cout << "no" << endl;
}
return ;
}
1036.容量为sum/2的01背包。
#include<bits/stdc++.h>
using namespace std; int n,w[],dp[]; int main()
{
int T;
cin >> T;
while(T--)
{
memset(dp,,sizeof(dp));
int sum = ;
cin >> n;
for(int i = ;i <= n;i++)
{
cin >> w[i];
sum += w[i];
}
int endd = sum/;
for(int i = ;i <= n;i++)
{
for(int j = endd;j >= w[i];j--)
{
dp[j] = max(dp[j],dp[j-w[i]]+w[i]);
}
}
cout << sum-dp[endd]* << endl;
}
return ;
}
1037.构造逆向逆数b,然后找a的后缀和b的前缀相同的部分。
#include<bits/stdc++.h>
using namespace std; int a[],b[];
string s; int main()
{
int T;
cin >> T;
while(T--)
{
cin >> s;
int n = s.length();
for(int i = ;i <= n;i++)
{
a[i] = s[i-]-'';
b[n-i+] = a[i]^;
}
int pos;
for(pos = ;pos <= n;pos++)
{
int flag = ;
for(int i = pos,j = ;i <= n;i++,j++)
{
if(a[i] != b[j])
{
flag = ;
break;
}
}
if(flag) break;
}
for(int i = ;i < pos;i++) cout << a[i];
for(int i = ;i <= n;i++) cout << b[i];
cout << endl;
}
return ;
}
1038.状压dp。
#include<bits/stdc++.h>
using namespace std; int a[][],sta[],dp[][],sum[][],n,m;
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--)
{
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
cin >> n >> m;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++) cin >> a[i][j];
}
int cnt = ,endd = <<(m-);
for(int i = ;i < endd;i++)
{
if(i & (i<<)) continue;
sta[++cnt] = i;
}
for(int i = ;i < n;i++)
{
for(int j = ;j <= cnt;j++)
{
for(int k = ;k <= m-;k++)
{
if(<<(k-) & sta[j])
{
if(a[i][k] && a[i][k+] && a[i+][k] && a[i+][k+]) sum[i][j]++;
}
}
}
}
for(int i = ;i <= cnt;i++) dp[][i] = sum[][i];
for(int i = ;i < n;i++)
{
for(int j = ;j <= cnt;j++)
{
for(int k = ;k <= cnt;k++)
{
dp[i+][k] = max(dp[i+][k],dp[i][j]+sum[i+][k]);
if(sta[j] & sta[k]) continue;
if(sta[j] & (sta[k]<<)) continue;
if(sta[j] & (sta[k]>>)) continue;
dp[i+][k] = max(dp[i+][k],dp[i][j]+sum[i+][k]);
}
}
}
int ans = ;
for(int i = ;i <= cnt;i++) ans = max(ans,dp[n-][i]);
cout << ans << endl;
}
return ;
}
1039.大模拟。
#include<bits/stdc++.h>
using namespace std; struct yy
{
int num,y;
friend bool operator<(yy A,yy B)
{
return A.y > B.y;
}
}y;
struct zz
{
int num,z;
friend bool operator<(zz A,zz B)
{
return A.z > B.z;
}
}z; struct xx
{
int x,last,next;
}a[];
int eat[],die[],n,m,live,turn;
priority_queue<yy> qy;
priority_queue<zz> qz; void fun(int i)
{
a[i].x--;
if(a[i].x == )
{
die[i] = ;
eat[i] = ;
live--;
a[a[i].last].next = a[i].next;
a[a[i].next].last = a[i].last;
}
} void eatt(int num)
{
if(!eat[num]) fun(num);
for(int i = ;i <= n;i++)
{
if(eat[i]) fun(i);
}
} int main()
{
int T;
cin >> T;
while(T--)
{
while(!qy.empty()) qy.pop();
while(!qz.empty()) qz.pop();
memset(die,,sizeof(die));
memset(eat,,sizeof(eat));
cin >> n >> m;
for(int i = ;i <= n;i++)
{
a[i].last = i-;
a[i].next = i+;
cin >> a[i].x;
}
a[].last = n;
a[n].next = ;
for(int i = ;i <= n;i++)
{
y.num = i;
cin >> y.y;
qy.push(y);
}
for(int i = ;i <= n;i++)
{
z.num = i;
cin >> z.z;
qz.push(z);
}
turn = ,live = n;
while(m--)
{
int card,now;
cin >> card;
if(n == ) break;
switch(card)
{
case :
now = qy.top().num;
while(die[now])
{
qy.pop();
now = qy.top().num;
}
eatt(now);
break;
case :
now = qz.top().num;
while(die[now])
{
qz.pop();
now = qz.top().num;
}
eatt(now);
break;
case :
eat[turn] = ;
break;
case :
eatt(a[turn].last);
break;
case :
eatt(a[turn].next);
break;
case :
eatt(turn);
break;
}
if(live == ) break;
turn = a[turn].next;
while(die[turn]) turn = a[turn].next;
if(live == ) break;
}
for(int i = ;i <= m;i++)
{
int card;
cin >> card;
}
switch(live)
{
case :
cout << << endl;
break;
case :
cout << turn << endl;
break;
default:
cout << - << endl;
break;
}
}
return ;
}
1040.解方程组判断。
#include<bits/stdc++.h>
using namespace std; int a,b,c; int main()
{
int T;
cin >> T;
while(T--)
{
cin >> a >> b >> c;
int x = (a+b-c)/,y = (a+c-b)/,z = (b+c-a)/;
if((x+y+z)*==(a+b+c)&&<=x&&x<=&&<=y&&y<=&&<=z&&z<=) cout << x << " " << y << " " << z << endl;
else cout << "Impossible" << endl;
}
return ;
}
1041.奇偶性规律。
#include<bits/stdc++.h>
using namespace std; int n,m; int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n >> m;
if(n% || m%) cout << << endl;
else cout << << endl;
}
return ;
}
1042.每次操作最小的两个数。
#include<bits/stdc++.h>
using namespace std; priority_queue<double,vector<double>,greater<double> > q;
int n; int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = ;i <= n;i++)
{
double temp;
cin >> temp;
q.push(temp);
}
for(int i = ;i < n;i++)
{
double a = q.top();
q.pop();
double b = q.top();
q.pop();
q.push((a+b)/);
}
cout << fixed << setprecision() << q.top()+0.00001 << endl;
q.pop();
}
return ;
}
1043.概率dp,dp[i][j]表示前i天上j节课的概率,等比求和求期望。
#include<bits/stdc++.h>
using namespace std; double p[],dp[][];
int n,k; int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n >> k;
for(int i = ;i <= n;i++) cin >> p[i];
dp[][] = ;
for(int i = ;i <= n;i++)
{
dp[i][] = dp[i-][]*(-p[i]);
for(int j = ;j <= n;j++) dp[i][j] = dp[i-][j-]*p[i]+dp[i-][j]*(-p[i]);
}
double x = ;
for(int i = k;i <= n;i++) x += dp[n][i];
cout << x/(-x) << endl;
}
return ;
}
1044.枚举所有情况就可以了,注意每种牌最多只有4张。
#include<bits/stdc++.h>
using namespace std; int dp[][][] = {},a[]; void init()
{
for(int i = ;i >= ;i--) dp[i][i][i] = dp[i+][i+][i+]+;
dp[][][] = ;
for(int i = ;i >= ;i--) dp[i][i+][i+] = dp[i+][i+][i+]+;
int x = ,y = ,z = ;
dp[][][] -= ;
for(int i = ;i >= ;i--)
{
int j;
for(j = ;j > i;j--)
{
dp[i][i][j] = dp[x][y][z]+;
x = i;
y = i;
z = j;
}
for(j--;j >= ;j--)
{
dp[j][i][i] = dp[x][y][z]+;
x = j;
y = i;
z = i;
}
}
dp[][][] += ;
dp[][][] += ;
for(int k = ;k >= ;k--)
{
for(int j = k-;j >= ;j--)
{
for(int i = j-;i >= ;i--)
{
if(dp[i][j][k]) continue;
dp[i][j][k] = dp[x][y][z]+;
x = i;
y = j;
z = k;
}
}
}
dp[][][] -= ;
dp[][][] += ;
} int main()
{
ios::sync_with_stdio(false);
init();
int T;
cin >> T;
while(T--)
{
cin >> a[] >> a[] >> a[];
sort(a,a+);
cout << dp[a[]][a[]][a[]] << endl;
}
return ;
}
1045.博弈dfs,注意dfs过程中还原原图。
#include<bits/stdc++.h>
using namespace std; string a[];
int dir[][] = {-,,,-,,,,}; bool dfs(int x,int y)
{
a[x][y] = '';
for(int i = ;i < ;i++)
{
int xx = x+dir[i][],yy = y+dir[i][];
if(xx > || xx < || yy > || yy < || a[xx][yy] == '') continue;
if(dfs(xx,yy))
{
a[x][y] = '';
return ;
}
}
a[x][y] = '';
return ;
} int main()
{
int T;
cin >> T;
while(T--)
{
for(int i = ;i <= ;i++)
{
cin >> a[i];
a[i] = " "+a[i];
}
int flag = ;
for(int i = ;i <= ;i++)
{
for(int j = ;j <= ;j++)
{
if(a[i][j] == '' && dfs(i,j))
{
flag = ;
break;
}
}
if(flag) break;
}
if(flag) cout << "win" << endl;
else cout << "lose" << endl;
}
return ;
}
1046.string类型的高精度。
#include<bits/stdc++.h>
using namespace std; int compare(string str1,string str2)
{
if(str1.length() > str2.length()) return ;
else if(str1.length() < str2.length()) return -;
else return str1.compare(str2);
} string add(string str1,string str2)
{
string str;
int len1 = str1.length(),len2 = str2.length();
if(len1 < len2)
{
for(int i = ;i <= len2-len1;i++) str1 = ""+str1;
}
else
{
for(int i = ;i <= len1-len2;i++) str2 = ""+str2;
}
int cf = ,temp;
for(int i = str1.length()-;i >= ;i--)
{
temp = str1[i]-''+str2[i]-''+cf;
cf = temp/;
temp %= ;
str = char(temp+'')+str;
}
if(cf != ) str = char(cf+'')+str;
return str;
} string sub(string str1,string str2)
{
string str;
int flag = ;
if(compare(str1,str2) < )
{
flag = ;
swap(str1,str2);
}
int tmp = str1.length()-str2.length(),cf = ;
for(int i = str2.length()-;i >= ;i--)
{
if(str1[tmp+i] < str2[i]+cf)
{
str = char(str1[tmp+i]-str2[i]-cf+''+)+str;
cf = ;
}
else
{
str = char(str1[tmp+i]-str2[i]-cf+'')+str;
cf = ;
}
}
for(int i = tmp-;i >= ;i--)
{
if(str1[i]-cf >= '')
{
str = char(str1[i]-cf)+str;
cf = ;
}
else
{
str = char(str1[i]-cf+)+str;
cf = ;
}
}
str.erase(,str.find_first_not_of(''));
if(str.empty()) str = "";
if(flag) str = "-"+str;
return str;
} string mul(string str1,string str2)
{
string str;
int len1 = str1.length();
int len2 = str2.length();
string tempstr;
for(int i = len2-;i >= ;i--)
{
tempstr = "";
int temp = str2[i]-'',t = ,cf = ;
if(temp != )
{
for(int j = ;j <= len2--i;j++) tempstr += "";
for(int j = len1-;j >= ;j--)
{
cf = (temp*(str1[j]-'')+cf);
t = cf%;
cf /= ;
tempstr = char(t+'')+tempstr;
}
if(cf != ) tempstr = char(cf+'')+tempstr;
str=add(str,tempstr);
}
}
str.erase(,str.find_first_not_of(''));
if(str.empty()) str = "";
return str;
} void div(string str1,string str2,string "ient,string &residue)
{
quotient = "";
residue = "";
if(str2 == "")
{
quotient = "ERROR";
residue = "ERROR";
return;
}
if(str1 == "")
{
quotient = "";
residue = "";
return;
}
int res = compare(str1,str2);
if(res < )
{
quotient = "";
residue = str1;
return;
}
else
{
int len1 = str1.length();
int len2 = str2.length();
string tempstr;
tempstr.append(str1,,len2-);
for(int i = len2-;i < len1;i++)
{
tempstr = tempstr+str1[i];
tempstr.erase(,tempstr.find_first_not_of(''));
if(tempstr.empty()) tempstr = "";
for(char ch = '';ch >= '';ch--)
{
string str,tmp;
str = str+ch;
tmp = mul(str2,str);
if(compare(tmp,tempstr) <= )
{
quotient = quotient+ch;
tempstr = sub(tempstr,tmp);
break;
}
}
}
residue = tempstr;
}
quotient.erase(,quotient.find_first_not_of(''));
if(quotient.empty()) quotient = "";
} int main()
{
string str1,str2;
string str3,str4;
while(cin >> str1 >> str2)
{
div(str1,str2,str3,str4);
cout << add(str1,str2) << ' ' << sub(str1,str2) << ' ' << mul(str1,str2) << ' ' << str3 << ' ' << str4 << endl;
}
return ;
}
1048.二分匹配模版。
#include<bits/stdc++.h>
using namespace std; int n,m,l;
int linker[];
bool used[];
vector<int> v[]; bool dfs(int u)
{
for(int i = ;i < v[u].size();i++)
{
int t = v[u][i];
if(used[t]) continue;
used[t] = ;
if(linker[t] == - || dfs(linker[t]))
{
linker[t] = u;
return ;
}
}
return ;
} int MaxMatch()
{
int ans = ;
memset(linker,-,sizeof(linker));
for(int i = ;i < n;i++)
{
memset(used,,sizeof(used));
if(dfs(i)) ans++;
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m)
{
for(int i = ;i < n;i++) v[i].clear();
cin >> l;
for(int i = ,a,b;i < l;i++)
{
cin >> a >> b;
v[a].push_back(b);
}
cout << MaxMatch() << endl;
}
return ;
}
1053.直接判断正负。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
while(cin >> n)
{
if(n > ) cout << "yes" << endl;
else if(n < ) cout << "no" << endl;
else cout << "light" << endl;
}
return ;
}
1054.权值累加。
#include<bits/stdc++.h>
using namespace std; int n,a[] = {,,,,,,,,,}; int main()
{
ios::sync_with_stdio(false);
while(cin >> n)
{
int ans = ;
while(n)
{
ans += a[n%];
n /= ;
}
cout << ans << endl;
}
return ;
}
1055.模拟遍历。
include<bits/stdc++.h>
using namespace std; int a[][],ans[],n,m; int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m)
{
int total = n*m;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++) cin >> a[i][j];
}
int now = ,x = ,y = ;
ans[] = a[][];
while(now < total)
{
if(x < n) ans[++now] = a[++x][y];
else ans[++now] = a[x][++y];
while(x > && y < m) ans[++now] = a[--x][++y];
if(y < m) ans[++now] = a[x][++y];
else ans[++now] = a[++x][y];
while(x < n && y > ) ans[++now] = a[++x][--y];
}
cout << ans[];
for(int i = ;i <= total;i++) cout << " " << ans[i];
cout << endl;
}
return ;
}
1056.dp。
#include<bits/stdc++.h>
using namespace std; int dp[][] = {},n; int main()
{
ios::sync_with_stdio(false);
dp[][] = ;
for(int i = ;i <= ;i++)
{
dp[i][] = ;
for(int j = ;j < i;j++) dp[i][j] = (dp[i-][j]+dp[i][j-])%;
dp[i][i] = dp[i][i-];
}
while(cin >> n) cout << dp[n][n] << endl;
return ;
}
1057.可重复组合,lucas模版。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL n,m; LL PowerMod(LL a, LL b, LL c)
{
LL ans = ;
a = a % c;
while(b)
{
if(b&) ans = (ans*a)%c;
b = b>>;
a = (a*a)%c;
}
return ans;
} LL c(LL m,LL n)
{
if(m < n) return ;
if(m == n) return ;
if(n > m-n) n = m-n;
LL mm = ,nn = ;
for(LL i = ;i < n;i++)
{
mm = mm*(m-i)%;
nn = nn*(n-i)%;
}
return mm*PowerMod(nn,,)%;
} LL lucas(LL m,LL n)
{
LL ans = ;
while(m && n && ans)
{
ans = ans%*c(m%,n%)%;
n /= ;
m /= ;
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m) cout << lucas(m+n-,m) << endl;
return ;
}
1058.把边的贡献算到点上,贡献大于0的点在子图内,小于0的点在子图外。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL a[];
int n,m,u,v,w; int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m)
{
memset(a,,sizeof(a));
for(int i = ;i <= m;i++)
{
cin >> u >> v >> w;
a[u] += w;
a[v] += w;
}
LL ans = ;
for(int i = ;i <= n;i++) ans += a[i] > ?a[i]:-a[i];
cout << ans/ << endl;
}
return ;
}
1059.按时间以ms为单位模拟,可以用优先队列优化,好像还可以用数学的方法快速算。
#include<bits/stdc++.h>
using namespace std; double ha,hb,maxha,maxhb,aa,ab,adda,addb,sub2a,sub2b,sub1a,sub1b,ara,arb;
int ta,tb,cool1a,cool1b,cool2a,cool2b; bool cmp(double x,double y)
{
return x-y > 1e-;
} int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> ha >> maxha >> aa >> adda >> sub2a >> sub1a >> ara >> ta >> cool1a >> cool2a;
cin >> hb >> maxhb >> ab >> addb >> sub2b >> sub1b >> arb >> tb >> cool1b >> cool2b;
cool2a *= ;
cool2b *= ;
int stopa = /*(-ta),stopb = /*(-tb);
double hita = aa*/(+arb*(-sub1a)/-sub2a),hitb = ab*/(+ara*(-sub1b)/-sub2b);
int ta_att = ,tb_att = ,ta_mag = ,tb_mag = ,buffa = ,buffb = ,ta_buff = ,tb_buff = ,ta_stop = -,tb_stop = -;
int t = ;
while()
{
if(ta_stop == t)
{
ta_att = max(ta_att,t+stopa);
ta_mag = max(ta_mag,t+stopa);
}
if(tb_stop == t)
{
tb_att = max(tb_att,t+stopb);
tb_mag = max(tb_mag,t+stopb);
}
if(--ta_buff <= ) buffa = ;
if(--tb_buff <= ) buffb = ;
if(ta_att == t && tb_att == t)
{
double hitatob = min(hita*(+0.125*buffa),hb);
double hitbtoa = min(hitb*(+0.125*buffb),ha);
hb -= hitatob;
ha -= hitbtoa;
if(!cmp(ha,) && !cmp(hb,)) break;
if(!cmp(ha,))
{
hb = min(maxhb,hb+hitbtoa*addb/);
break;
}
if(!cmp(hb,))
{
ha = min(maxha,ha+hitatob*adda/);
break;
}
hb = min(maxhb,hb+hitbtoa*addb/);
ha = min(maxha,ha+hitatob*adda/);
buffa = min(buffa+,);
buffb = min(buffb+,);
ta_buff = ;
tb_buff = ;
ta_att += cool1a;
tb_att += cool1b;
}
else if(ta_att == t)
{
double hit = min(hita*(+0.125*buffa),hb);
ha = min(maxha,ha+hit*adda/);
hb -= hit;
if(!cmp(hb,)) break;
buffa = min(buffa+,);
ta_buff = ;
ta_att += cool1a;
}
else if(tb_att == t)
{
double hit = min(hitb*(+0.125*buffb),ha);
hb = min(maxhb,hb+hit*addb/);
ha -= hit;
if(!cmp(ha,)) break;
buffb = min(buffb+,);
tb_buff = ;
tb_att += cool1b;
}
if(ta_mag == t)
{
ta_mag = t+cool2a;
tb_stop = t+;
}
if(tb_mag == t)
{
tb_mag = t+cool2b;
ta_stop = t+;
}
t++;
}
if(t% >= ) t = t/+;
else t = t/;
int t1 = t/,t2 = t%;
cout << t1 << "." << setw() << setfill('') << t2 << " ";
cout << fixed << setprecision() << ha+1e- << " " << hb+1e- << endl;
}
return ;
}
1060.不知道如何直接输出,手动处理整数。
#include<bits/stdc++.h>
using namespace std; int a[];
long long x; int main()
{
while(cin >> x)
{
int cnt = ;
if(x% >= ) x = x/+;
else x = x/;
if(x == )
{
cout << "0.00" << endl;
continue;
}
while(x)
{
a[++cnt] = x%;
x /= ;
}
if(cnt > )
{
for(;cnt > ;cnt--) cout << a[cnt];
}
else
{
cout << "";
}
cout << ".";
if(cnt == ) cout << "";
for(;cnt > ;cnt--) cout << a[cnt];
cout << endl;
}
return ;
}
1061.通分后计算,再约分。
#include<bits/stdc++.h>
using namespace std; int a1,a2,b1,b2;
char c; int main()
{ while(cin >> a1 >> c >> a2 >> b1 >> c >> b2)
{
int x = a1*b2+a2*b1,y = a2*b2,g = __gcd(x,y);;
cout << x/g << "/" << y/g << endl;
}
return ;
}
1062.最大连续k段和,注意k小于n的情况。
#include<bits/stdc++.h>
using namespace std; int n,t,a[]; int main()
{
while(cin >> n >> t)
{
for(int i = ;i <= n;i++) cin >> a[i];
int sum = ;
if(n <= t)
{
for(int i = ;i <= n;i++) sum += a[i];
cout << sum << endl;
continue;
}
for(int i = ;i <= t;i++) sum += a[i];
int ans = sum;
for(int l = ,r = t+;r <= n;l++,r++)
{
sum += a[r];
sum -= a[l];
ans = max(ans,sum);
}
cout << ans << endl;
}
return ;
}
1063.数学推导,参考https://pan.baidu.com/s/1i3jljoBZWL74nf6_3aVz-A
#include<bits/stdc++.h>
#define LL long long
#define MOD 1000000007
using namespace std; LL PowerMod(LL a, LL b, LL c)
{
LL ans = ;
a = a % c;
while(b)
{
if(b&) ans = (ans*a)%c;
b = b>>;
a = (a*a)%c;
}
return ans;
} int main()
{
int n;
while(cin >> n)
{
if(n == ) cout << << endl;
else if(n%) cout << PowerMod(,n-,MOD) << endl;
else cout << (PowerMod(,n-,MOD)+PowerMod(,n/-,MOD))%MOD << endl;
}
return ;
}
1064.将坐标旋转45度,注意长度的变化,然后处理一下重叠面积和方格数量的转化。
#include<bits/stdc++.h>
#define LL long long
using namespace std; int main()
{
LL x1,y1,x2,y2,r,X1,Y1,X2,Y2;
while(cin >> x1 >> y1 >> x2 >> y2 >> r)
{
X1 = y1+x1;
Y1 = y1-x1;
X2 = y2+x2;
Y2 = y2-x2;
LL a = max(0LL,*r+-abs(X1-X2)),b = max(0LL,*r+-abs(Y1-Y2));
LL sub = a*b?(a*b-a-b+)/:;
cout << *(*r*r+*r+)-sub << endl;
}
return ;
}
1065.动态开点线段树,维护连续最长段。
#include<bits/stdc++.h>
using namespace std; int n,q,cnt = ,a[];
struct segtree
{
int l,r,lazy,len,x,lx,rx;
segtree *lson,*rson;
}*root,tree[]; segtree *newnode(int l,int r)
{
segtree *t = &tree[++cnt];
t->l = l;
t->r = r;
t->lson = NULL;
t->rson = NULL;
t->lazy = -;
t->len = r-l+;
t->x = t->len;
t->lx = t->len;
t->rx = t->len;
return t;
} segtree *newlson(segtree *pos)
{
int mid = (pos->l+pos->r)/;
return newnode(pos->l,mid);
} segtree *newrson(segtree *pos)
{
int mid = (pos->l+pos->r)/;
return newnode(mid+,pos->r);
} void pushup(segtree *pos)
{
pos->lx = pos->lson->lx;
pos->rx = pos->rson->rx;
pos->x = max(pos->lson->x,pos->rson->x);
if(pos->lx == pos->lson->len) pos->lx += pos->rson->lx;
if(pos->rx == pos->rson->len) pos->rx += pos->lson->rx;
pos->x = max(pos->x,pos->lson->rx+pos->rson->lx);
} void pushdown(segtree *pos)
{
if(!pos->lson) pos->lson = newlson(pos);
if(!pos->rson) pos->rson = newrson(pos);
if(pos->lazy != -)
{
pos->lson->lazy = pos->lazy;
pos->rson->lazy = pos->lazy;
pos->lson->x = pos->lson->lx = pos->lson->rx = pos->lazy*pos->lson->len;
pos->rson->x = pos->rson->lx = pos->rson->rx = pos->lazy*pos->rson->len;
pos->lazy = -;
}
} void update(segtree *pos,int l,int r,int x)
{
if(r < pos->l || pos->r < l) return;
if(l <= pos->l && pos->r <= r)
{
pos->x = pos->lx = pos->rx = pos->len*x;
pos->lazy = x;
return;
}
pushdown(pos);
update(pos->lson,l,r,x);
update(pos->rson,l,r,x);
pushup(pos);
} int getl(segtree *pos,int x)
{
if(pos->x < x) return 2e9;;
if(pos->lx >= x) return pos->l;
pushdown(pos);
int minn = 2e9;
if(pos->lson->rx+pos->rson->lx >= x) minn = pos->lson->r-pos->lson->rx+;
minn = min(minn,getl(pos->lson,x));
minn = min(minn,getl(pos->rson,x));
return minn;
} int main()
{
while(~scanf("%d%d",&n,&q))
{
set<int> s;
map<int,int> mp;
cnt = ;
root = newnode(,n);
while(q--)
{
char ss[];
int x;
scanf("%s",ss);
if(ss[] == 'm')
{
sscanf(ss,"malloc(%d)",&x);
int t = getl(root,x);
if(t == 2e9) printf("0\n");
else
{
printf("%d\n",t);
update(root,t,t+x-,);
s.insert(t);
mp[t] = t+x-;
}
}
else
{
sscanf(ss,"free(%d)",&x);
auto it = s.lower_bound(x);
if(it == s.end() || *it != x) continue;
update(root,*it,mp[*it],);
s.erase(it);
}
}
}
return ;
}
1066.a^b%c = a^(b%phi[c]+phi[c])%c。
#include<bits/stdc++.h>
#define LL long long
#define MOD 1000000007
using namespace std; LL a,b,n;
LL PowerMod(LL a, LL b, LL c)
{
LL ans = ;
a = a % c;
while(b)
{
if(b&) ans = (ans*a)%c;
b = b>>;
a = (a*a)%c;
}
return ans;
} int main()
{
int T;
cin >> T;
while(T--)
{
cin >> a >> n >> b;
int ans = b;
for(int i = ;i <= n;i++)
{
b = (b*b+MOD-)%(MOD-);
ans = ans*b%(MOD-);
}
cout << PowerMod(a,ans,MOD) << endl;
}
return ;
}
1067.dp[i]表示i个人的方案数,更新每个dp[i]时,枚举i的所有约数x,对于每一个x,可以有A(n-1,x-1)的情况的环,再根据乘法原理,乘上dp[i-x]。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,k,cnt;
long long dp[],mul[],inv[],fac[]; long long qpower(long long a, long long b, long long c)
{
long long ans = ;
a = a%c;
while(b)
{
if(b%) ans = ans*a%c;
a = a*a%c;
b /= ;
}
return ans;
} int main()
{
ios::sync_with_stdio();
mul[] = ;
mul[] = ;
inv[] = ;
inv[] = ;
for(int i = ;i <= ;i++) mul[i] = mul[i-]*i%MOD;
for(int i = ;i <= ;i++) inv[i] = inv[i-]*qpower(i,MOD-,MOD)%MOD;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
cnt = ;
int endd = sqrt(k+0.1);
for(int i = ;i <= endd;i++)
{
if(k%i == )
{
fac[++cnt] = i;
if(k/i != i) fac[++cnt] = k/i;
}
}
sort(fac+,fac+cnt+);
memset(dp,,sizeof(dp));
dp[] = ;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= cnt && fac[j] <= i;j++) dp[i] = (dp[i]+dp[i-fac[j]]*mul[i-]%MOD*inv[i-fac[j]])%MOD;
}
printf("%lld\n",dp[n]);
}
return ;
}
1068.二分答案,根据单调性O(n)判断。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL a[],b[];
int n,m,k; int f(LL x)
{
int ans = ,now = m;
for(int i = ;i <= n;i++)
{
while(now)
{
if(a[i]*b[now] > x) now--;
else break;
}
ans += now;
}
return n*m-ans;
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n >> m >> k;
k--;
for(int i = ;i <= n;i++) cin >> a[i];
for(int i = ;i <= m;i++) cin >> b[i];
sort(a+,a++n);
sort(b+,b++m);
LL l = a[]*b[],r = a[n]*b[m];
while(l < r)
{
LL mid = (l+r)/;
int t = f(mid);
if(t <= k) r = mid;
else l = mid+;
}
cout << l << endl;
}
return ;
}
1069.建一个虚点连接所有人,再连接人和人之间的关系,求最小生成树。
#include<bits/stdc++.h>
using namespace std; struct line
{
int to,w;
line(int a,int b):to(a),w(b){}
friend bool operator<(line X,line Y)
{
return X.w > Y.w;
}
}; int n,m,k,x,dis[],vis[];
vector<line> v[]; int prim()
{
memset(vis,,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
int ans = ,cnt = ;
priority_queue<line> q;
q.push(line(,));
int endd = n+m;
while(cnt <= endd && !q.empty())
{
while(!q.empty() && vis[q.top().to]) q.pop();
ans += q.top().w;
int now = q.top().to;
vis[now] = ;
cnt++;
for(int i = ;i < v[now].size();i++)
{
if(vis[v[now][i].to]) continue;
if(dis[v[now][i].to] <= v[now][i].w) continue;
dis[v[now][i].to] = v[now][i].w;
q.push(line(v[now][i].to,v[now][i].w));
}
}
return ans;
} int main()
{
int T;
cin >> T;
while(T--)
{
cin >> m >> n >> k >> x;
int endd = n+m;
for(int i = ;i <= endd;i++) v[i].clear();
for(int i = ;i <= endd;i++) v[].push_back(line(i,k));
while(x--)
{
int a,b,c;
cin >> a >> b >> c;
a += ;
b += m+;
c = k-c;
v[a].push_back(line(b,c));
v[b].push_back(line(a,c));
}
cout << prim() << endl;
}
return ;
}
1070.树形dp。
#include<bits/stdc++.h>
using namespace std; int dp[][],n,m;
vector<int> v[]; void dfs(int pos,int pre)
{
for(int i = ;i < v[pos].size();i++)
{
if(v[pos][i] == pre) continue;
int x = v[pos][i];
dfs(x,pos);
for(int j = m;j > ;j--)
{
for(int k = ;k <= j;k++)
{
dp[pos][j] = max(dp[pos][j],dp[x][j-k]+dp[pos][k]);
}
}
}
} int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n >> m;
memset(dp,,sizeof(dp));
for(int i = ;i < n;i++) v[i].clear();
for(int i = ;i < n;i++) cin >> dp[i][];
for(int i = ;i < n;i++)
{
int a,b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(,-);
int ans = ;
for(int i = ;i < n;i++) ans = max(ans,dp[i][m]);
cout << ans << endl;
}
return ;
}
1071.枚举长宽。
#include<bits/stdc++.h>
using namespace std; int n,m; int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n >> m;
int ans =;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++) ans += i*j;
}
cout << ans << endl;
}
return ;
}
1072.求关键边的数量,即强连通分量-1,tarjan模版。
#include<bits/stdc++.h>
using namespace std; int n,m,dfn[],low[],num,cnt;
vector<int> v[]; void tarjan(int pos,int pre)
{
dfn[pos] = low[pos] = ++num;
for(int i = ;i < v[pos].size();i++)
{
int x = v[pos][i];
if(x == pre) continue;
if(dfn[x] == )
{
tarjan(x,pos);
low[pos] = min(low[pos],low[x]);
if(dfn[pos] < low[x]) cnt++;
}
else low[pos] = min(low[pos],dfn[x]);
}
} int main()
{
int T;
cin >> T;
while(T--)
{
num = ;
cnt = ;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
cin >> n >> m;
for(int i = ;i < n;i++) v[i].clear();
while(m--)
{
int x,y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
tarjan(,-);
cout << cnt << endl;
}
return ;
}
1073.容量为sum/2的01背包,先判断奇偶。
#include<bits/stdc++.h>
using namespace std; int n,w[],dp[]; int main()
{
int T;
cin >> T;
while(T--)
{
memset(dp,,sizeof(dp));
cin >> n;
int sum = ;
for(int i = ;i <= n;i++)
{
cin >> w[i];
sum += w[i];
}
if(sum&)
{
cout << "No" << endl;
continue;
}
int endd = sum/;
for(int i = ;i <= n;i++)
{
for(int j = endd;j >= w[i];j--) dp[j] = max(dp[j],dp[j-w[i]]+w[i]);
}
if(dp[endd] == endd) cout << "Yes" << endl;
else cout << "No" << endl;
}
return ;
}
*1074.待补。
*1075.待补。
1076.类似于三进制,输入数据中有空行,用gets会出错。
#include<bits/stdc++.h>
using namespace std; char s[]; int f(char x)
{
if(x == '') return ;
if(x == '') return ;
if(x == '') return ;
} int main()
{
while(~scanf("%s",s))
{
int len = strlen(s);
long long ans = ;
for(int i = ;i < len;i++) ans = ans*+f(s[i]);
printf("%lld\n",ans);
}
return ;
}
1077.循环节仅看分母,先把分母2,5除完后为m,求欧拉数,循环节长度为欧拉数约数中最小数x的使10^x%m == 1的值。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL PowerMod(LL a, LL b, LL c)
{
LL ans = ;
a = a % c;
while(b)
{
if(b&) ans = (ans*a)%c;
b = b>>;
a = (a*a)%c;
}
return ans;
} int euler(int n)
{
int res = n,a = n;
for(int i = ;i*i <= a;i++)
{
if(a%i == )
{
res = res/i*(i-);
while(a%i == ) a /= i;
}
}
if(a > ) res = res/a*(a-);
return res;
} int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
} int main()
{
int p,q;
while(cin >> p >> q)
{
q /= gcd(p,q);
while(q% == ) q /= ;
while(q% == ) q /= ;
if(q == )
{
cout << << endl;
continue;
}
int x = euler(q),now = ;
int xx = sqrt(x),flag = ;
for(int i = ;i <= xx;i++)
{
if(x%i) continue;
if(PowerMod(,i,q) == )
{
cout << i << endl;
flag = ;
break;
}
}
if(flag) continue;
for(int i = xx;i >= ;i--)
{
if(x%i) continue;
if(PowerMod(,x/i,q) == )
{
cout << x/i << endl;
break;
}
}
}
return ;
}
1078.分层图最短路模版,建k+1层图。
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std; struct edge
{
int to,cost;
friend bool operator>(edge x,edge y)
{
return x.cost > y.cost;
}
}e;
vector<edge> v[];
int n,m,k,a[][],dis[],vis[];
priority_queue< edge,vector<edge>,greater<edge> > q; int dij()
{
while(!q.empty()) q.pop();
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof(vis));
dis[] = ;
e.to = ;
e.cost = ;
q.push(e);
while(!q.empty())
{
int now = q.top().to,cost = q.top().cost;
if(now == (k+)*n) return dis[(k+)*n];
q.pop();
if(vis[now]) continue;
vis[now] = ;
for(int i = ;i < v[now].size();i++)
{
e = v[now][i];
if(e.cost+dis[now] < dis[e.to])
{
dis[e.to] = e.cost+dis[now];
e.cost = dis[e.to];
q.push(e);
}
} }
} int main()
{
while(cin >> n >> m >> k)
{
for(int i = ;i <= ;i++) v[i].clear();
memset(a,0x3f,sizeof(a));
for(int i = ;i <= m;i++)
{
int u,v,w;
cin >> u >> v >> w;
a[u][v] = min(a[u][v],w);
a[v][u] = min(a[v][u],w);
}
for(int i = ;i <= n;i++)
{
for(int j = ;j <= n;j++)
{
if(i == j || a[i][j] == INF) continue;
for(int l = ;l <= k;l++)
{
e.to = l*n+j;
e.cost = a[i][j];
v[l*n+i].push_back(e);
if(l != k)
{
e.to = (l+)*n+j;
e.cost = ;
v[l*n+i].push_back(e);
}
}
}
}
cout << dij() << endl;
}
return ;
}
1079.枚举两个数,之后的处理类似于最大连续子序列和。
#include<bits/stdc++.h>
using namespace std; int a[],n,m,has[]; int main()
{
int T;
cin >> T;
while(T--)
{
memset(has,,sizeof(has));
cin >> n >> m;
int cnt = ;
for(int i = ;i <= n;i++)
{
cin >> a[i];
if(!has[a[i]]) has[a[i]] = ;
}
int ans = ;
for(int i = ;i <= m;i++)
{
for(int j = ;j <= m;j++)
{
if(i == j) continue;
if(!has[i] || !has[j]) continue;
int sum = ,flag = ;
for(int k = ;k <= n;k++)
{
if(a[k] == i) sum++;
else if(a[k] == j)
{
sum--;
flag = ;
}
if(sum < )
{
sum = ;
flag = ;
}
if(flag) ans = max(ans,sum);
else ans = max(ans,sum-);
}
}
}
cout << ans << endl;
}
return ;
}
1080.规律。
#include<bits/stdc++.h>
using namespace std; int main()
{
int n;
while(cin >> n) cout << n*n* << endl;
return ;
}
1081.判断两个范围是否有交集。
#include<bits/stdc++.h>
using namespace std; long long a1,a2,b1,b2,c1,c2,d1,d2; int main()
{
while(cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2 >> d1 >> d2)
{
long long x1 = a1*d1,x2 = a2*d2,y1 = b1*c1,y2 = b2*c2;
if(x1 > y2 || x2 < y1) cout << "NO" << endl;
else cout << "YES" << endl;
}
return ;
}
1082.若场数最多的人比其余的和要大,则ans为其余的和,否则ans为sum/2。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
int T;
cin >> T;
while(T--)
{
long long maxx = ,sum = ,temp;
cin >> n;
while(n--)
{
cin >> temp;
maxx = max(maxx,temp);
sum += temp;
}
cout << min(sum/,sum-maxx) << endl;
}
return ;
}
1083.从左到右扫一遍,记录左括号个数,有右括号时更新答案就可以了。
#include<bits/stdc++.h>
using namespace std; int main()
{
int T;
string s;
cin >> T;
while(T--)
{
cin >> s;
int len = s.length(),cnt = ,ans = ;
for(int i = ;i < len;i++)
{
if(s[i] == '(') cnt++;
else if(cnt)
{
cnt--;
ans += ;
}
}
cout << ans << endl;
}
return ;
}
1084.暴力搜索,不会超时。
#include<bits/stdc++.h>
using namespace std; int n,u,v,vis[],sco[],ans,k;
vector<int> pre[]; void dfs(int x,int sum)
{
if(x == )
{
ans = max(ans,sum);
return;
}
for(int i = ;i <= ;i++)
{
if(vis[i]) continue;
int flag = ;
for(int j = ;j < pre[i].size();j++)
{
if(!vis[pre[i][j]])
{
flag = ;
break;
}
}
if(!flag) continue;
vis[i] = ;
dfs(x-,sum+sco[i]);
vis[i] = ;
}
}
int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
memset(vis,,sizeof(vis));
for(int i = ;i <= ;i++) pre[i].clear();
while(n--)
{
cin >> u >> v;
pre[v].push_back(u);
}
for(int i = ;i <= ;i++) cin >> sco[i];
cin >> k;
ans = ;
dfs(k/,);
if(ans >= ) cout << ans << endl;
else cout << "I chose to die" << endl;
}
return ;
}
*1085.待补。
1086.简单模拟。
#include<bits/stdc++.h>
using namespace std; int n,m,h,op[],a[],hh[]; int main()
{
int T;
cin >> T;
while(T--)
{
memset(a,-,sizeof(a));
cin >> n;
for(int i = ;i < n;i++) cin >> op[i];
cin >> m;
for(int i = ;i <= m;i++) cin >> hh[i];
int pos;
for(int i = ;i <= m;i++)
{
cin >> pos;
if(pos <= n) a[pos] = hh[i];
}
cin >> h;
int flag = ,now = ;
while(now < n)
{
if(op[now] == ) h++;
else if(op[now] == ) h--;
if(h == || a[now] != - && h >= a[now])
{
flag = ;
break;
}
now++;
}
if(flag) cout << "V8Orz" << endl;
else cout << now << endl;
}
return ;
}
1087.贪心单位体积的价值。
#include<bits/stdc++.h>
using namespace std; int n,w,V;
struct aa
{
int v;
double a;
}a[]; bool cmp(aa x,aa y)
{
return x.a < y.a;
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = ;i <= n;i++)
{
cin >> a[i].v >> w;
a[i].a = (double)w/a[i].v;
}
cin >> V;
sort(a+,a++n,cmp);
int now = n;
double ans = ;
while(V && now >= )
{
if(V > a[now].v)
{
ans += a[now].a*a[now].v;
V -= a[now].v;
now--;
}
else
{
ans += V*a[now].a;
break;
}
}
cout << fixed << setprecision() << ans << endl;
}
return ;
}
1088.按字典序从大到小排序。
#include<bits/stdc++.h>
using namespace std; string s[];
int n; bool cmp(string x,string y)
{
string X = x+y,Y = y+x;
return X > Y;
} int main()
{
while(cin >> n)
{
for(int i = ;i <= n;i++) cin >> s[i];
sort(s+,s++n,cmp);
for(int i = ;i <= n;i++) cout << s[i];
cout << endl;
}
return ;
}
1089.时间顺序的bfs,注意多个水珠同时到一点的情况。
#include<bits/stdc++.h>
using namespace std; int n,m,nn,tt,a[][],sta[][],t[][],dir[][] = {-,,,-,,,,};
struct water
{
int x,y,d,t;
water(int _x,int _y,int _d,int _t):x(_x),y(_y),d(_d),t(_t){};
}; void bfs(int x,int y)
{
queue<water> q;
q.push(water(x,y,,));
q.push(water(x,y,,));
q.push(water(x,y,,));
q.push(water(x,y,,));
while(!q.empty())
{
water temp = q.front();
q.pop();
if(temp.t > tt) return;
int xx = temp.x+dir[temp.d][],yy = temp.y+dir[temp.d][];
if(xx < || xx > n || yy < || yy > m) continue;
if(t[xx][yy] == temp.t) continue;
if(sta[xx][yy] == ) q.push(water(xx,yy,temp.d,temp.t+));
else if(sta[xx][yy] == )
{
sta[xx][yy] = ;
t[xx][yy] = temp.t;
q.push(water(xx,yy,,temp.t+));
q.push(water(xx,yy,,temp.t+));
q.push(water(xx,yy,,temp.t+));
q.push(water(xx,yy,,temp.t+));
}
else sta[xx][yy]++;
}
} int main()
{
while(cin >> n >> m >> nn >> tt)
{
memset(sta,,sizeof(sta));
memset(t,,sizeof(t));
for(int i = ;i <= nn;i++)
{
int x;
cin >> a[i][] >> a[i][] >> x;
sta[a[i][]][a[i][]] = x;
}
int x,y;
cin >> x >> y;
bfs(x,y);
for(int i = ;i <= nn;i++)
{
if(t[a[i][]][a[i][]] == ) cout << << " " << sta[a[i][]][a[i][]] << endl;
else cout << << " " << t[a[i][]][a[i][]] << endl;
}
}
return ;
}
1090.树形dp。
#include<bits/stdc++.h>
using namespace std; struct line
{
int next,w;
}l;
vector<line> v[];
int n; int dfs(int x)
{
int sum = ;
for(int i = ;i < v[x].size();i++) sum = max(sum,dfs(v[x][i].next)+v[x][i].w);
return sum;
} int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = ;i <= ;i++) v[i].clear();
for(int i = ;i <= n;i++)
{
int u;
cin >> u >> l.next >> l.w;
v[u].push_back(l);
}
cout << dfs() << endl;
}
return ;
}
1091.按结束时间排序,贪心。
#include<bits/stdc++.h>
using namespace std; struct vedio
{
int l,r;
}a[];
int n; bool cmp(vedio x,vedio y)
{
return x.r < y.r;
} int main()
{
while(cin >> n && n)
{
for(int i = ;i <= n;i++) cin >> a[i].l >> a[i].r;
sort(a+,a++n,cmp);
int ans = ,time = a[].r,now = ;
for(;now <= n;now++)
{
if(time > a[now].l) continue;
time = a[now].r;
ans++;
}
cout << ans << endl;
}
return ;
}
1092.模拟,每次移动后,不断下落,消去,直到不变化为止。
#include<bits/stdc++.h>
using namespace std; int n,a[][],ans[][]; int fmove()
{
int flag = ;
for(int i = ;i < ;i++)
{
for(int j = ;j < ;j++)
{
if(!a[i][j]) continue;
int t = j;
while(t > && a[i][t-] == )
{
flag = ;
swap(a[i][t],a[i][t-]);
t--;
}
}
}
return flag;
} void fdel()
{
memset(ans,,sizeof(ans));
for(int i = ;i < ;i++)
{
int l = ,cnt = ;
for(int j = ;j <= ;j++)
{
if(a[i][j] == a[i][j-]) cnt++;
else
{
if(cnt >= )
{
for(int k = l;k < j;k++) ans[i][k] = ;
}
l = j;
cnt = ;
}
}
}
for(int j = ;j < ;j++)
{
int l = ,cnt = ;
for(int i = ;i <= ;i++)
{
if(a[i][j] == a[i-][j]) cnt++;
else
{
if(cnt >= )
{
for(int k = l;k < i;k++) ans[k][j] = ;
}
l = i;
cnt = ;
}
}
}
for(int i = ;i < ;i++)
{
for(int j = ;j < ;j++)
{
if(ans[i][j]) a[i][j] = ;
}
}
}
int main()
{
while(~scanf("%d",&n))
{
int flag = ;
memset(a,,sizeof(a));
for(int i = ;i < ;i++)
{
for(int j = ;j < ;j++)
{
scanf("%d",&a[i][j]);
if(!a[i][j]) break;
}
}
while(n--)
{
int x,y,d;
scanf("%d%d%d",&x,&y,&d);
if(!a[x][y] || d+x > || d+x < )
{
printf("Runtime Error\n");
flag = ;
}
else
{
swap(a[x][y],a[x+d][y]);
fmove();
fdel();
while(fmove()) fdel();
}
}
if(flag) continue;
for(int i = ;i < ;i++)
{
for(int j = ;j < ;j++)
{
if(a[i][j]) flag = ;
}
}
if(!flag) printf("Accepted\n");
else printf("Wrong Answer\n");
}
return ;
}
1093.确定两个区间,二分答案。
#include<bits/stdc++.h>
using namespace std;
const double eps=0.0001;
double a,b,c,d; double ans[];
int sum; inline double calc(double x)
{
return a*x*x*x+b*x*x+c*x+d;
} void find(double l,double r)
{
while()
{
double mid = (l+r)/;
if(abs(calc(mid)) < eps)
{
ans[++sum] = mid;
return;
}
if(calc(mid)*calc(l) < ) r = mid;
else l = mid;
}
} int main()
{
while(cin >> a >> b >> c >> d)
{
sum = ;
for(int i=-;i<=;i++)
{
double tmp1 = calc(i),tmp2 = calc(i+);
if(abs(tmp1) < eps) ans[++sum] = i;
else if(abs(tmp2) < eps)
{
ans[++sum] = i+;
i++;
}
else if(tmp1*tmp2 < ) find(i,i+);
if(sum == ) break;
}
sort(ans+,ans+);
cout << fixed << setprecision() << ans[] << " " << ans[] << " " << ans[] << endl;
}
return ;
}
1094.递归输出。
#include<bits/stdc++.h>
using namespace std; int n; void printff(int x)
{
if(x == )
{
cout <<"2(0)";
return;
}
if(x == )
{
cout << "";
return;
}
int sum = ,b = -;
while(sum <= x)
{
sum *= ;
b++;
}
if(sum/ < x)
{
if(sum/ == )
{
cout << "2+";
printff(x-sum/);
}
else
{
cout << "2(";
printff(b);
cout << ")+";
printff(x-sum/);
}
}
else
{
cout << "2(";
printff(b);
cout << ")";
}
} int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
printff(n);
cout << endl;
}
return ;
}
1095.暴力搜索,不会超时。
#include<bits/stdc++.h>
using namespace std; int vis[],a[],n,k,ans; void dfs(int num,int v)
{
if(num == n)
{
if(abs(v-a[]) <= k) ans++;
return;
}
for(int i = ;i <= n;i++)
{
if(vis[i]) continue;
if(abs(v-a[i]) > k) continue;
vis[i] = ;
dfs(num+,a[i]);
vis[i] = ;
}
} int main()
{
ios::sync_with_stdio();
while(cin >> n >> k)
{
memset(vis,,sizeof(vis));
ans = ;
for(int i = ;i <= n;i++) cin >> a[i];
dfs(,a[]);
cout << ans << endl;
}
return ;
}
1096.dp[i][j]表示i拆分成最大为j的数。
#include<bits/stdc++.h>
using namespace std; int dp[][],n; int main()
{
ios::sync_with_stdio();
for(int i = ;i <= ;i++)
{
dp[i][] = ;
dp[][i] = ;
}
for(int i = ;i <= ;i++)
{
for(int j = ;j <= ;j++)
{
if(i == j) dp[i][j] = dp[i][j-]+;
else if(i < j) dp[i][j] = dp[i][j-];
else dp[i][j] = dp[i-j][j]+dp[i][j-];
}
}
while(cin >> n) cout << dp[n][n]- << endl;
return ;
}
1097.dp[i] = dp[i-1]+dp[i-2]+dp[i-3]。
#include<bits/stdc++.h>
using namespace std; int n;
long long dp[]; int main()
{
dp[] = ;
dp[] = ;
dp[] = ;
for(int i = ;i <= ;i++) dp[i] = dp[i-]+dp[i-]+dp[i-];
while(cin >> n) cout << dp[n] << endl;
return ;
}
1098.求欧拉函数模版。
#include<bits/stdc++.h>
using namespace std; int n; int euler(int n)
{
int res = n,a = n;
for(int i = ;i*i <= a;i++)
{
if(a%i == )
{
res = res/i*(i-);
while(a%i == ) a /= i;
}
}
if(a > ) res = res/a*(a-);
return res;
} int main()
{
ios::sync_with_stdio();
while(cin >> n) cout << euler(n) << endl;
return ;
}
1099.双指针,线性扫一遍。
#include<bits/stdc++.h>
using namespace std; int a[],n,k; int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> n >> k;
for(int i = ;i <= n;i++) cin >> a[i];
int l = ,now = ;
long long ans = ;
for(int i = ;i <= n;i++)
{
now += a[i];
while(now > k) now -= a[++l];
ans += i-l;
}
cout << ans << endl;
}
return ;
}
1100.即求最大矩阵面积,单调栈模版。
#include<bits/stdc++.h>
using namespace std; int n;
stack<int> s;
long long a[],b[]; int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
while(!s.empty()) s.pop();
long long ans = ;
b[] = ;
for(int i = ;i <= n;i++) cin >> a[i];
for(int i = ;i <= n;i++)
{
cin >> b[i];
b[i] += b[i-];
}
a[n+] = ;
for(int i = ;i <= n+;i++)
{
while(!s.empty() && a[s.top()] > a[i])
{
int temp = s.top();
s.pop();
long long len = s.empty()?b[i-]:b[i-]-b[s.top()+];
ans = max(ans,len*a[temp]);
}
s.push(i);
}
cout << ans << endl;
}
return ;
}
1101.模拟进制加法。
#include<bits/stdc++.h>
using namespace std; int k,n; int main()
{
ios::sync_with_stdio();
while(cin >> k >> n)
{
long long ans = ,temp = ;
while(n)
{
ans += temp*(n%);
n /= ;
temp *= k;
}
cout << ans << endl;
}
return ;
}
1102.dp[i][j]表示i个钩子,j个灯笼的方案数,先求一个钩子的情况,再O(n^3)求多个钩子的情况。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,m;
long long dp[][]; int main()
{
ios::sync_with_stdio();
for(int i = ;i <= ;i++) dp[i][] = ;
for(int i = ;i <= ;i++)
{
for(int j = ;j < i;j++) dp[][i] = (dp[][i]+dp[][j]*dp[][i-j-])%MOD;
}
for(int i = ;i <= ;i++)
{
for(int j = ;j <= ;j++)
{
for(int k = ;k <= j;k++) dp[i][j] = (dp[i][j]+dp[i-][k]*dp[][j-k])%MOD;
}
}
while(cin >> n >> m) cout << dp[n][m] << endl;
return ;
}
*1103.待补。
1104.dfs两侧点的个数,相乘。
#include<bits/stdc++.h>
using namespace std; int n,m,from[],to[];
vector<int> v[]; int dfs(int s,int t)
{
int ans = ;
for(int i = ;i < v[s].size();i++)
{
if(v[s][i] == t) continue;
ans += dfs(v[s][i],s);
}
return ans;
} int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> n >> m;
for(int i = ;i <= n;i++) v[i].clear();
for(int i = ;i < n;i++)
{
int a,b;
cin >> a >> b;
from[i] = a;
to[i] = b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i = ;i <= m;i++)
{
int x;
cin >> x;
int t = dfs(to[x],from[x]);
cout << t*(n-t) << endl;
}
}
return ;
}
1105.约数个数。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
int ans = ;
for(int i = ;i <= sqrt(n+0.1);i++)
{
if(n%i == )
{
if(i == n/i) ans++;
else ans += ;
}
}
cout << ans << endl;
}
return ;
}
1106.求n!/(每个数字个数)!,先统计个数,然后对于每种个数分解质因数,得到最后剩余的所有质因数和其个数,再计算答案。
#include<bits/stdc++.h>
#define MAXN 1000000
using namespace std; int n,a[];
int cnt,prime[MAXN+] = {},mi[MAXN+],vis[MAXN+] = {};
long long b[]; void getprime()
{
cnt = ;
for(int i = ;i <= MAXN;i++)
{
if(!vis[i])
{
prime[++cnt] = i;
mi[i] = i;
}
for(int j = ;j <= cnt && (long long)i*prime[j] <= MAXN;j++)
{
vis[prime[j]*i] = ;
mi[prime[j]*i] = prime[j];
if(!(i%prime[j])) break;
}
}
}
int main()
{
ios::sync_with_stdio();
getprime();
while(~scanf("%d",&n))
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i = ;i <= n;i++)
{
int x;
scanf("%d",&x);
a[x]++;
}
for(int i = ;i <= n;i++) b[i]++;
for(int i = ;i <= ;i++)
{
for(int j = ;j <= a[i];j++) b[j]--;
}
for(int i = ;i >= ;i--)
{
if(b[i] == ) continue;
int cnt = b[i],t = i;
b[i] = ;
while(t != )
{
b[mi[t]] += cnt;
t /= mi[t];
}
}
long long now = ;
int ok = ;
for(int i = ;i <= ;i++)
{
while(b[i]--)
{
if(5e18/i > now && now*i <= 1e18) now *= i;
else ok = ;
}
}
if(ok) cout << now << endl;
else cout << "Look, shability!" << endl;
}
return ;
}
1107.按x排序好后,按y求最长上升子序列。
#include<bits/stdc++.h>
using namespace std; struct point
{
int x,y;
}a[];
int b[],n; bool cmp(point X,point Y)
{
if(X.x == Y.x) return X.y > Y.y;
return X.x < Y.x;
} int LIS()
{
int len = ,j;
b[] = a[].y;
for(int i = ;i <= n;i++)
{
if(a[i].y > b[len]) j = ++len;
else j = lower_bound(b+,b++len,a[i].y)-b;
b[j] = a[i].y;
}
return len;
}
int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
for(int i = ;i <= n;i++) cin >> a[i].x;
for(int i = ;i <= n;i++) cin >> a[i].y;
sort(a+,a++n,cmp);
cout << LIS() << endl;
}
return ;
}
1108.打表,前缀和。
#include<bits/stdc++.h>
using namespace std; int x,y,a[] = {}; int main()
{
a[] = a[] = ;
for(int i = ;i <= ;i++)
{
if(i%) a[i] = a[i-]+;
else a[i] = a[i/]+;
}
for(int i = ;i <= ;i++) a[i] += a[i-];
while(cin >> x >> y) cout << a[y]-a[x-] << endl;
return ;
}
1109.dp[i] = dp[i-1]+dp[i-2]+dp[i-3]。
#include<bits/stdc++.h>
using namespace std; int n,a[]; int main()
{
ios::sync_with_stdio();
a[] = ;
a[] = ;
a[] = ;
for(int i = ;i <= ;i++) a[i] = (a[i-]+a[i-]+a[i-])%;
while(cin >> n) cout << a[n] << endl;
return ;
}
1111.f(n)=f(n-1)+f(n-2)+n-2推出f(n)=3*f(n-1)-2*f(n-2)-f(n-3)+f(n-4),矩阵快速幂。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,anss[] = {,,,,,};
struct matrix
{
long long m[][];
}; matrix ans = { ,,,,
,,,,
,,,,
,,,,};
matrix base = { ,,,,
MOD-,,,,
MOD-,,,,
,,,,}; matrix mul(matrix a, matrix b)
{
matrix tmp;
for(int i = ; i < ;i++)
{
for(int j = ; j < ;j++)
{
tmp.m[i][j] = ;
for(int k = ; k < ;k++) tmp.m[i][j] = (tmp.m[i][j]+a.m[i][k]*b.m[k][j])%MOD;
}
}
return tmp;
} long long fast_mod(int n)
{
matrix x = ans,y = base;
while(n)
{
if(n & )
{
x = mul(x,y);
}
y = mul(y,y);
n >>= ;
}
return (anss[]*x.m[][]+anss[]*x.m[][]+anss[]*x.m[][]+anss[]*x.m[][])%MOD;
} int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
if(n <= ) cout << anss[n] << endl;
else cout << fast_mod(n-) << endl;
}
}
1112.dp[i][j]表示i个人j派的方案数。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; long long dp[][] = {},ans[] = {};
int n; int main()
{
ios::sync_with_stdio();
dp[][] = ;
for(int i = ;i <= ;i++)
{
for(int j = ;j <= i;j++) dp[i][j] = (dp[i-][j-]+dp[i-][j]*j)%MOD;
}
for(int i = ;i <= ;i++)
{
for(int j = ;j <= i;j++) ans[i] = (ans[i]+dp[i][j])%MOD;
}
while(cin >> n) cout << ans[n] << endl;
return ;
}
1113.dp[i][j]表示前i位起余数为j的个数。
#include<bits/stdc++.h>
using namespace std; string s;
int k;
long long dp[][]; int main()
{
ios::sync_with_stdio();
while(cin >> s >> k)
{
memset(dp,,sizeof(dp));
dp[][(s[]-'')%k]++;
for(int i = ;i < s.length();i++)
{
for(int j = ;j < k;j++) dp[i][(j*+s[i]-'')%k] += dp[i-][j];
dp[i][(s[i]-'')%k]++;
}
long long ans = ;
for(int i = ;i < s.length();i++) ans += dp[i][];
cout << ans << endl;
}
return ;
}
1114.先离线,按所有点的和k1,k2的大小排序,再树状数组维护一下。
#include<bits/stdc++.h>
using namespace std; struct arr
{
int o,x,pos,l,r;
}a[];
int n,m,tree[] = {},ans1[],ans2[]; bool cmp(arr X,arr Y)
{
if(X.x == Y.x) return X.o < Y.o;
return X.x < Y.x;
} inline int lowbit(int x)
{
return x & (-x);
} void update(int pos,int x)
{
while(pos <= n)
{
tree[pos] += x;
pos += lowbit(pos);
}
} int getsum(int pos)
{
int sum = ;
while(pos > )
{
sum += tree[pos];
pos -= lowbit(pos);
}
return sum;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(tree,,sizeof(tree));
for(int i = ;i <= n;i++)
{
scanf("%d",&a[i].x);
a[i].pos = i;
a[i].o = ;
}
int cnt = n;
for(int i = ;i <= m;i++)
{
scanf("%d%d%d%d",&a[cnt+].l,&a[cnt+].r,&a[cnt+].x,&a[cnt+].x);
a[cnt+].o = ;
a[cnt+].o = ;
a[cnt+].pos = a[cnt+].pos = i;
a[cnt+].l = a[cnt+].l;
a[cnt+].r = a[cnt+].r;
cnt += ;
}
sort(a+,a++cnt,cmp);
for(int i = ;i <= cnt;i++)
{
if(a[i].o == ) update(a[i].pos,);
else if(a[i].o == ) ans1[a[i].pos] = getsum(a[i].r)-getsum(a[i].l-);
else ans2[a[i].pos] = getsum(a[i].r)-getsum(a[i].l-);
}
for(int i = ;i <= m;i++) printf("%d\n",ans2[i]-ans1[i]);
}
return ;
}
1115.x^n+y^n = (x^(n-1)+y^(n-1))*(x+y)-(x^(n-2)+y^(n-2))*x*y。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int a,b,n;
long long ans[]; int main()
{
ios::sync_with_stdio();
ans[] = ;
while(cin >> a >> b >> n)
{
ans[] = a;
for(int i = ;i <= n;i++) ans[i] = ((ans[i-]*a-ans[i-]*b%MOD)+MOD)%MOD;
cout << ans[n] << endl;
}
return ;
}
1116.求n!/(每一个重要城市子树种的节点数)。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,m,ok[],a[];
vector<int> v[]; long long qpower(long long a,long long b,long long c)
{
long long ans = ;
a %= c;
while(b)
{
if(b%) ans = ans*a%c;
a = a*a%c;
b /= ;
}
return ans;
} void dfs(int now,int pre)
{
a[now] = ;
for(int i = ;i < v[now].size();i++)
{
int t = v[now][i];
if(t == pre) continue;
dfs(t,now);
a[now] += a[t];
}
} int main()
{
ios::sync_with_stdio();
while(cin >> n >> m)
{
memset(ok,,sizeof(ok));
for(int i = ;i <= n;i++) v[i].clear();
for(int i = ;i < n;i++)
{
int x,y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(,-);
long long ans = ;
for(int i = ;i <= n;i++) ans = (ans*i)%MOD;
for(int i = ;i <= m;i++)
{
int x;
cin >> x;
if(ok[x]) continue;
ans = (ans*qpower(a[x],MOD-,MOD))%MOD;
ok[x] = ;
}
cout << ans << endl;
}
return ;
}
1117.状压dp。
#include<bits/stdc++.h>
#define LL long long
#define MOD 1000000007
using namespace std; int n,m;
LL dp[][<<];
vector<int> v[]; LL dfs(int left,int sta)
{
if(dp[left][sta] != -) return dp[left][sta];
if(left == ) return !sta;
dp[left][sta] = ;
for(int i = ;i < v[left].size();i++)
{
dp[left][sta] = (dp[left][sta]+dfs(left-,sta^v[left][i]))%MOD;
}
return dp[left][sta];
} int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(dp,-,sizeof(dp));
for(int i = ;i <= m;i++) v[i].clear();
for(int i = ;i <= m;i++)
{
int t,endd = <<n;
scanf("%d",&t);
for(int j = ;j < endd;j++)
{
int sum = ;
for(int k = ;k < n;k++)
{
if(j&(<<k)) sum++;
}
if(sum == t) v[i].push_back(j);
}
}
int a = ;
for(int i = ;i < n;i++)
{
int t;
scanf("%d",&t);
a |= (<<i)*t;
}
printf("%lld\n",dfs(m,a));
}
return ;
}
1118.暴力搜索边的组成顺序,每次判断只要延长边,判断是否正三角形。
#include<bits/stdc++.h>
#define PI (atan(1)*4)
using namespace std; int a[],b[],vis[] = {};
double ans; void dfs(int cnt)
{
if(cnt == )
{
if(b[]+b[]+b[] == b[]+b[]+b[] && b[]+b[]+b[] == b[]+b[]+b[])
{
ans = sqrt()/*((b[]+b[]+b[])*(b[]+b[]+b[])-b[]*b[]-b[]*b[]-b[]*b[]);
}
}
for(int i = ;i < ;i++)
{
if(vis[i]) continue;
b[cnt] = a[i];
vis[i] = ;
dfs(cnt+);
vis[i] = ;
}
}
int main()
{
ios::sync_with_stdio(false);
while(cin >> a[] >> a[] >> a[] >> a[] >> a[] >> a[])
{
ans = ;
dfs();
if(ans > 1e-) cout << fixed << setprecision() << ans << endl;
else cout << << endl;
}
return ;
}
1119.递增枚举最小公倍数。
#include<bits/stdc++.h>
using namespace std; int a[],n,k; bool ok(int x)
{
int cnt = ;
for(int i = ;i <= n;i++)
{
if(x%a[i] == ) cnt++;
}
if(cnt >= k) return ;
return ;
} int main()
{
ios::sync_with_stdio();
while(cin >> n >> k)
{
for(int i = ;i <= n;i++) cin >> a[i];
int ans = ;
for(;ans <=;ans++)
{
if(ok(ans)) break;
}
if(ans <= ) cout << ans << endl;
else cout << "Orz" << endl;
}
return ;
}
1120.贪心,时间后往前,大的先考虑,可以用优先队列优化一下。
#include<bits/stdc++.h>
using namespace std; struct task
{
int t,v;
}a[];
int n,vis[] = {}; bool cmp(task x,task y)
{
return x.v > y.v;
}
int main()
{
ios::sync_with_stdio();
cin >> n;
int t = ,sum = ,ans = ;
for(int i = ;i <= n;i++)
{
cin >> a[i].t;
t = max(a[i].t,t);
}
for(int i = ;i <= n;i++)
{
cin >> a[i].v;
sum += a[i].v;
}
sort(a+,a++n,cmp);
for(int i = t;i >= ;i--)
{
for(int j = ;j <= n;j++)
{
if(!vis[j] && i <= a[j].t)
{
ans += a[j].v;
vis[j] = ;
break;
}
}
}
cout << sum-ans << endl;
return ;
}
1121.60个刚好超1e18,60之前直接递归算,60之后的用60的来算。
#include<bits/stdc++.h>
using namespace std; long long a[],n,k; int f(long long x,long long y)
{
if(x == ) return ;
if(y <= a[x-]) return f(x-,y)^;
if(y == a[x-]+) return ;
return f(x-,a[x]-y+);
} int main()
{
ios::sync_with_stdio();
a[] = ;
for(int i = ;i <= ;i++) a[i] = a[i-]*+;
while(cin >> n >> k)
{
int ans;
if(n <= ) ans = f(n,k);
else
{
ans = f(,k);
if(n&) ans ^= ;
}
if(ans) cout << "srO" << endl;
else cout << "Orz\n" << endl;
}
return ;
}
1123.二分查找。
#include<bits/stdc++.h>
using namespace std; struct orz
{
int id,x;
friend bool operator<(struct orz X,struct orz Y)
{
if(X.x == Y.x) return X.id < Y.id;
return X.x < Y.x;
}
}a[],t;
int n,m; int main()
{
ios::sync_with_stdio();
t.id = ;
while(cin >> n >> m)
{
for(int i = ;i <= n;i++)
{
cin >> a[i].x;
a[i].id = i;
}
sort(a+,a++n);
int flag = ;
while(m--)
{
cin >> t.x;
int ans = lower_bound(a+,a++n,t)-a;
if(flag)
{
if(ans > n) cout << -;
else cout << a[ans].id;
flag = ;
}
else
{
if(ans > n) cout << " -1";
else cout << " " << a[ans].id;
}
}
cout << endl;
}
return ;
}
1124.找第一个可以替代的数,用后面最大且最后的数替代。
#include<bits/stdc++.h>
using namespace std; char a[]; int main()
{
while(cin >> a)
{
int len = strlen(a),first = ,last = ,now = ;
while(now < len)
{
first = last = now;
char maxx = a[now];
for(int i = len-;i > now;i--)
{
if(a[i] > maxx)
{
maxx = a[i];
last = i;
}
}
if(first != last) break;
now++;
}
swap(a[first],a[last]);
cout << a << endl;
}
return ;
}
1125.分成三个三角形,由面积判断。
#include<bits/stdc++.h>
using namespace std; double f(double x1,double y1,double x2,double y2,double x3,double y3)
{
double a,b,c,p;
a = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b = sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c = sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
p = (a+b+c)/;
return sqrt(p*(p-a)*(p-b)*(p-c));
} int main()
{
double x1,x2,x3,y1,y2,y3,x,y;
while(cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x >> y)
{
if(abs(f(x1,y1,x2,y2,x3,y3)-f(x,y,x2,y2,x3,y3)-f(x1,y1,x,y,x3,y3)-f(x1,y1,x2,y2,x,y)) < 1e-) cout << "Orz" << endl;
else cout << "stO" << endl;
}
return ;
}
*1128.待补。
1129.记录交换后的映射。
#include<bits/stdc++.h>
using namespace std; int a[][],x[],y[],n,m,k; int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
for(int i = ;i <= n;i++) x[i] = i;
for(int i = ;i <= m;i++) y[i] = i;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++) scanf("%d",&a[i][j]);
}
int o,xx,yy;
while(k--)
{
scanf("%d%d%d",&o,&xx,&yy);
if(o == ) swap(x[xx],x[yy]);
else swap(y[xx],y[yy]);
}
for(int i = ;i <= n;i++)
{
printf("%d",a[x[i]][y[]]);
for(int j = ;j <= m;j++) printf(" %d",a[x[i]][y[j]]);
printf("\n");
}
}
return ;
}
1130.C(n,0)+C(n,1)+...+C(n,n) =2^n,0*C(n,0)+1*C(n,1)+2*C(n,2)+...n*C(n,n) = n*2^(n-1)。
#include<bits/stdc++.h>
#define LL long long
#define MOD 542
using namespace std; int p,d,n; LL qmod(LL a, LL b, LL c)
{
LL ans = ;
a = a%c;
while(b)
{
if(b&) ans = ans*a%c;
b >>= ;
a = a*a%c;
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&p,&d,&n);
LL ans = qmod(,n,MOD)*p%MOD;
ans = (qmod(,n-,MOD)*d*n+ans)%MOD;
printf("%lld\n",ans);
}
return ;
}
1131.dp[i][j]表示i,j两个下面为首的最序列的个数,从后往前更新,用map记录是否出现过某个值。
#include<bits/stdc++.h>
using namespace std; int n,dp[][];
long long a[]; int main()
{
//freopen("1.txt","r",stdin);
while(~scanf("%d",&n))
{
memset(dp,,sizeof(dp));
for(int i = ;i <= n;i++) scanf("%lld",&a[i]);
map<long long,int> mp;
long long ans1 = 2e18,ans2 = 2e18,maxx = ;
for(int j = n;j >= ;j--)
{
for(int i = j-;i >= ;i--)
{
if(mp.count(a[i]+a[j])) dp[i][j] = max(dp[i][j],dp[j][mp[a[i]+a[j]]]+);
dp[i][j] = max(dp[i][j],);
if(dp[i][j] > maxx || dp[i][j] == maxx && (a[i] < ans1 || a[i] == ans1 && a[j] < ans2))
{
maxx = dp[i][j];
ans1 = a[i];
ans2 = a[j];
}
}
mp[a[j]] = j;
}
printf("%d\n",maxx);
int now1 = mp[ans1],now2 = mp[ans2];
printf("%lld %lld",ans1,ans2);
for(int i = ;i <= maxx;i++)
{
long long t = a[now1]+a[now2];
now1 = now2;
while(a[now2] != t) now2++;
printf(" %lld",a[now2]);
}
printf("\n");
}
return ;
}
*1132.待补。
1138.a+b。
#include<bits/stdc++.h>
using namespace std; char s;
int a,b,c,d; int main()
{
ios::sync_with_stdio();
while(cin >> a >> s >> b >> s >> c >> s >> d >> s) cout << a+c << "+" << b+d << "i" << endl;
return ;
}
1139.打表二分,注意前面几个特例。
#include<bits/stdc++.h>
using namespace std; long long n,f[],sum[]; int main()
{
ios::sync_with_stdio();
f[] = ;
f[] = ;
sum[] = ;
for(int i = ;i <= ;i++)
{
f[i] = f[i-]+f[i-];
sum[i] = sum[i-]+f[i];
}
while(cin >> n)
{
if(n == || n == || n == ) cout << "1 1" << endl;
else
{
int x = lower_bound(sum,sum+,n)-sum;
long long y = n-sum[x-];
if(y > f[x-]) cout << x << " " << y << endl;
else cout << x- << " " << f[x-] << endl;
}
}
return ;
}
1140.kmp,c中的strstr比kmp快10来倍。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
while(~scanf("%d",&n))
{
getchar();
int s = ;
char a[],b[] = " ";
while(n--)
{
gets(a);
strcat(b,a);
char *p = b,*o;
while(o = strstr(p,"wanshen"))
{
p = o+;
s++;
}
strcpy(b,p);
}
printf("%d\n",s);
}
return ;
}
1141.取余后模拟。
#include<bits/stdc++.h>
using namespace std; long long x,y,a; int main()
{
while(cin >> x >> y >> a)
{
a %= x+y;
if(a == )
{
cout << "light" << endl;
continue;
}
while(a)
{
a -= x;
if(a <= )
{
cout << "wanshen" << endl;
break;
}
a -= y;
if(a <= )
{
cout << "light" << endl;
break;
}
}
}
return ;
}
1142.记录字符串中出现的字符,然后删除。
#include<bits/stdc++.h>
using namespace std; char a[],b[];
int s[]; int main()
{
while(cin >> a >> b)
{
memset(s,,sizeof(s));
int flag=,lena = strlen(a),lenb = strlen(b);
for(int i = ;i < lenb;i++) s[b[i]] = ;
for(int i = ;i < lena;i++)
{
if(!s[a[i]])
{
putchar(a[i]);
flag = ;
}
}
if(!flag) cout << "EMPTY";
cout << endl;
}
return ;
}
1143.暴力dfs,注意过程中还原。
#include<bits/stdc++.h>
using namespace std; int a[],vis[],num[],ans; bool ok(int x,int z)
{
if(x == ) return ;
if(x > && __gcd(z,num[x-]) != ) return ;
if(x% != && __gcd(z,num[x-]) != ) return ;
return ;
} void dfs(int x)
{
if(x == )
{
ans++;
return;
}
for(int i = ;i <= ;i++)
{
if(vis[i]) continue;
if(ok(x+,a[i]))
{
vis[i] = ;
num[x+] = a[i];
dfs(x+);
vis[i] = ;
}
}
}
int main()
{
while(cin >> a[])
{
for(int i = ;i <= ;i++) cin >> a[i];
ans = ;
dfs();
cout << ans << endl;
}
return ;
}
1144.合并小的,不一定每一次都是合并k个,第一次先合并不足k的次数,后面都合并k个。
#include<bits/stdc++.h>
using namespace std; priority_queue< long long,vector<long long>,greater<long long> > q;
int n,k; int main()
{
ios::sync_with_stdio();
while(cin >> n >> k)
{
while(!q.empty()) q.pop();
long long x,ans = ;
for(int i = ;i <= n;i++)
{
cin >> x;
q.push(x);
}
while((n-)%(k-))
{
q.push();
n++;
}
while(q.size() != )
{
long long t = ,sum = ;
while(t++ < k)
{
sum += q.top();
q.pop();
}
ans += sum;
q.push(sum);
}
cout << ans << endl;
}
return ;
}
1145.矩阵快速幂。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; long long l,r,anss[] = {,,,,};
struct matrix
{
long long m[][];
};
matrix ans = { ,,,,
,,,,
,,,,
,,,,};
matrix base = { ,,,,
,,,,
,,,,
,,,,}; matrix mul(matrix a, matrix b)
{
matrix tmp;
for(int i = ; i < ;i++)
{
for(int j = ; j < ;j++)
{
tmp.m[i][j] = ;
for(int k = ; k < ;k++) tmp.m[i][j] = (tmp.m[i][j]+a.m[i][k]*b.m[k][j])%MOD;
}
}
return tmp;
} long long fast_mod(long long n)
{
matrix x = ans,y = base;
while(n)
{
if(n & )
{
x = mul(x,y);
}
y = mul(y,y);
n >>= ;
}
return (x.m[][]+x.m[][]+x.m[][]+*x.m[][])%MOD;
} int main()
{
ios::sync_with_stdio();
while(cin >> l >> r)
{
long long x,y;
if(l == ) x = ;
else x = l < ?anss[l-]:fast_mod(l-);
y = r < ?anss[r]:fast_mod(r-);
cout << (y-x+MOD)%MOD << endl;
}
return ;
}
1146.01背包的变形,达成每种V所需的最小体力。
#include<bits/stdc++.h>
using namespace std; int dp[],v[],w[],n,W; int main()
{
ios::sync_with_stdio();
while(cin >> n >> W)
{
memset(dp, 0x3f, sizeof(dp));
dp[] = ;
int sum = ;
for(int i = ;i <= n;i++)
{
cin >> w[i] >> v[i];
sum += v[i];
}
for(int i = ;i <= n;i++)
{
for(int j = sum;j >= v[i];j--) dp[j] = min(dp[j-v[i]]+w[i],dp[j]);
}
for(int i = sum;i >= ;i--)
{
if(dp[i] <= W)
{
cout << i << endl;
break;
}
}
}
return ;
}
1147.先判断和在不在最大和最小的区间内。若在,从后往前贪心。
#include<bits/stdc++.h>
using namespace std; int n,l[],r[],ans[],s; int main()
{
while(~scanf("%d%d",&n,&s))
{
int sum1 = ,sum2 = ;
for(int i = ;i <= n;i++)
{
scanf("%d%d",&l[i],&r[i]);
sum1 += l[i];
sum2 += r[i];
}
if(s < sum1 || s > sum2)
{
printf("Xue Beng\n");
continue;
}
int x = s-sum1;
for(int i = n;i >= ;i--)
{
ans[i] = x >= r[i]-l[i]?r[i]:l[i]+x;
x -= ans[i]-l[i];
}
printf("%d",ans[]);
for(int i = ;i <= n;i++) printf(" %d",ans[i]);
printf("\n");
}
return ;
}
1148.b ≥ phi[c]时,a^b%c = a^(b%phi[c]+phi[c])%c,否则直接算。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL a,b,c,y; LL gcd(LL a,LL b)
{
return b?gcd(b,a%b):a;
} LL PowerMod(LL a, LL b, LL c)
{
LL ans = ;
a = a % c;
while(b)
{
if(b&) ans = (ans*a)%c;
b = b>>;
a = (a*a)%c;
}
return ans;
} LL euler(LL n)
{
LL res = n,a = n;
for(LL i = ;i*i <= a;i++)
{
if(a%i == )
{
res = res/i*(i-);
while(a%i == ) a /= i;
}
}
if(a > ) res = res/a*(a-);
return res;
} int main()
{
while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&y))
{
LL t = euler(y);
if(log(b) < /c)
{
printf("%lld\n",PowerMod(a,PowerMod(b,c,1e9+),y));
}
else printf("%lld\n",PowerMod(a,PowerMod(b,c,t)+t,y));
}
return ;
}
1149.打表预处理,容斥。
#include<bits/stdc++.h>
#define MOD 1000000007
#define LL long long
using namespace std; LL fac[],inv[],n,m,k; LL PowerMod(LL a, LL b, LL c)
{
LL ans = ;
a = a % c;
while(b)
{
if(b&) ans = (ans*a)%c;
b = b>>;
a = (a*a)%c;
}
return ans;
} inline LL c(LL n,LL m)
{
if(m > n) return ;
return fac[n]*inv[m]%MOD*inv[n-m]%MOD;
} int main()
{
ios::sync_with_stdio();
fac[] = ;
for(int i = ;i <= ;i++) fac[i] = fac[i-]*i%MOD;
inv[] = PowerMod(fac[],MOD-,MOD);
for(int i = ;i >= ;i--) inv[i] = inv[i+]*(i+)%MOD;
while(cin >> n >> m >> k)
{
LL ans = c(n+m-,m),x = -;
for(int i = ;i <= n;i++,x = -x)
{
LL t = m-i*(k+);
if(t < ) break;
ans = (ans+x*(c(n,i)*c(n+t-,t)%MOD)+MOD)%MOD;
}
cout << ans << endl;
}
return ;
}
1150.减法。
#include<bits/stdc++.h>
using namespace std; int a; int main()
{
while(cin >> a) cout << a- << endl;
return ;
}
1151.分情况讨论。
#include<bits/stdc++.h>
using namespace std; long long a,b,c; int main()
{
while(cin >> a >> b >> c)
{
if(a==&&b==&&c==)
{
cout << "inf" << endl;
continue;
}
if(a==&&b!=)
{
cout << << endl;
continue;
}
if(a==&&b==&&c!=)
{
cout << << endl;
continue;
}
long long x=b*b-*a*c;
if(x==) cout << << endl;
else if(x>) cout << << endl;
else cout << << endl;
}
return ;
}
1152.map记录。
#include<bits/stdc++.h>
using namespace std; struct st
{
string name,course;
};
map<int,st> mp;
map<int,st>::iterator it; int main()
{
int n,m;
while(cin >> n >> m)
{
mp.clear();
string a,c,d;
int b;
cin >> a >> c;
for(int i = ;i <= n;i++)
{
cin >> a >> b;
mp[b].name = a;
mp[b].course = "NULL";
}
cin >> a >> c >> d;
for(int i = ;i <= m;i++)
{
cin >> a >> b >> c;
mp[b].course = c;
}
cout << "Name StuNum CourseName" << endl;
for(it = mp.begin();it != mp.end();it++) cout << it->second.name << ' ' << it->first << ' ' << it->second.course << endl;
}
return ;
}
1153.比较斜率排序。
#include<bits/stdc++.h>
#define LL long long
using namespace std; struct line
{
LL x,y;
}a[];
int n; bool cmp(line X,line Y)
{
return X.y*Y.x < X.x*Y.y;
} int main()
{
while(~scanf("%d",&n))
{
int cnt = ;
LL x,y,xx,yy,temp = ;
for(int i = ;i <= n;i++)
{
scanf("%lld%lld%lld%lld",&x,&y,&xx,&yy);
if(x == xx) temp++;
else if(x < xx)
{
a[cnt].x = xx-x;
a[cnt].y = yy-y;
cnt++;
}
else
{
a[cnt].x = x-xx;
a[cnt].y = y-yy;
cnt++;
}
}
sort(a,a+cnt,cmp);
LL ans = temp*(temp-)/;
LL num = ;
for(int i = ;i < cnt;i++)
{
if(a[i].x*a[i-].y == a[i].y*a[i-].x) num++;
else
{
ans += num*(num-)/;
num = ;
}
}
ans += num*(num-)/;
printf("%lld\n",ans);
}
return ;
}
1154.统计连续0串的个数。
#include<bits/stdc++.h>
using namespace std; string s; int main()
{
ios::sync_with_stdio();
while(cin >> s)
{
int len = s.length();
int ans = ,flag = ;
for(int now = ;now < len;now++)
{
if(flag)
{
while(now < len)
{
if(s[now] == '')
{
ans++;
flag = ;
break;
}
else now++;
}
}
else if(s[now] == '') flag = ;
}
if(flag) ans++;
cout << ans << endl;
}
return ;
}
1155.先确定在往里数的第几个圈上,然后根据位置确定数字。
#include<bits/stdc++.h>
using namespace std; long long k,x,y,ans; int main()
{
ios::sync_with_stdio();
while(cin >> k >> x >> y)
{
long long p = min(x,y);
p = min(p,k-x+);
p = min(p,k-y+);
long long dis = x-p+y-p;
if(x <= y) ans = *(p-)*(k-p+)++dis;
else ans = *p*(k-p)+-dis;
cout << ans << endl;
}
return ;
}
1156.递减的单调队列,保存人的编号和入队耐心-入队时间。
#include<bits/stdc++.h>
using namespace std; struct st
{
int num,t;
}s;
deque<st> q;
int n,x; int main()
{
while(~scanf("%d",&n))
{
while(!q.empty()) q.pop_back();
int out = ,cnt = ;
for(int i = ;i <= n;i++)
{
scanf("%d",&x);
switch(x)
{
case :
int tt;
scanf("%d",&tt);
s.t = tt-i;
s.num = cnt++;
while(!q.empty() && q.back().t <= s.t) q.pop_back();
q.push_back(s);
break;
case :
if(q.front().num == out++) q.pop_front();
break;
case :
printf("%d\n",q.front().t+i);
}
}
}
return ;
}
1157.一条一条加边,求max和从小边往大边加,求min和从大边往小边加,用并查集计点数。
#include<bits/stdc++.h>
using namespace std; int n,pre[],point[];
struct line
{
int v,u,w;
friend bool operator<(line x,line y)
{
return x.w < y.w;
}
}l[]; int findd(int x)
{
return x == pre[x]?x:pre[x] = findd(pre[x]);
} int main()
{
while(~scanf("%d",&n))
{
for(int i = ;i < n;i++) scanf("%d%d%d",&l[i].u,&l[i].v,&l[i].w);
sort(l+,l+n);
long long ans = ;
for(int i = ;i <= n;i++)
{
pre[i] = i;
point[i] = ;
}
for(int i = ;i < n;i++)
{
int x = findd(l[i].u),y = findd(l[i].v);
ans += (long long)point[x]*point[y]*l[i].w;
pre[x] = y;
point[y] += point[x];
}
for(int i = ;i <= n;i++)
{
pre[i] = i;
point[i] = ;
}
for(int i = n-;i >= ;i--)
{
int x = findd(l[i].u),y = findd(l[i].v);
ans -= (long long)point[x]*point[y]*l[i].w;
pre[x] = y;
point[y] += point[x];
}
printf("%lld\n",ans);
}
return ;
}
1158.规律C(x+k,x+y),打表预处理。
#include<bits/stdc++.h>
#define LL long long
#define MOD (long long)(1e9+7)
using namespace std; int x,y,k;
LL fac[]; LL PowerMod(LL a, LL b, LL c)
{
LL ans = ;
a = a % c;
while(b)
{
if(b&) ans = (ans*a)%c;
b = b>>;
a = (a*a)%c;
}
return ans;
} LL inv(LL x)
{
return PowerMod(x,MOD-,MOD);
} int main()
{
fac[] = ;
for(int i = ;i <= ;i++) fac[i] = fac[i-]*i%MOD;
while(~scanf("%d%d%d",&x,&y,&k))
{
if(y == k) printf("1\n");
else if(y < k) printf("0\n");
else printf("%lld\n",fac[x+y]*inv(fac[x+k])%MOD*inv(fac[y-k])%MOD);
}
return ;
}
1159.floyd动态更新,首要要考虑重边和环,然后开放一个点就要更新相关最短路,而且更新的顺序有要求。
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std; int n,m,t,a[][],ok[]; int main()
{
while(~scanf("%d%d%d",&n,&m,&t))
{
memset(a,0x3f,sizeof(a));
memset(ok,,sizeof(ok));
for(int i = ;i < n;i++) a[i][i] = ;
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(u == v) continue;
a[u][v] = min(a[u][v],w);
}
while(t--)
{
int o;
scanf("%d",&o);
if(o == )
{
int x;
scanf("%d",&x);
if(ok[x]) printf("lab %d has been repaired!\n",x);
else
{
ok[x] = ;
for(int i = ;i < n;i++)
{
for(int j = ;j < n;j++)
{
if(!ok[i] || !ok[j]) continue;
a[j][x] = min(a[j][x],a[j][i]+a[i][x]);
}
}
for(int i = ;i < n;i++)
{
for(int j = ;j < n;j++)
{
if(!ok[i] || !ok[j]) continue;
a[x][j] = min(a[x][j],a[x][i]+a[i][j]);
}
}
for(int i = ;i < n;i++)
{
for(int j = ;j < n;j++)
{
if(!ok[i] || !ok[j]) continue;
a[i][j] = min(a[i][j],a[i][x]+a[x][j]);
}
}
}
}
else
{
int u,v;
scanf("%d%d",&u,&v);
if(!ok[u] || !ok[v]) printf("help %d %d\n",u,v);
else if(a[u][v] == INF) printf("no path\n");
else printf("%d\n",a[u][v]);
}
}
}
return ;
}
1160.set模拟,貌似代码有点问题,直接过了,好像是询问都是按时间顺序来的。
#include<bits/stdc++.h>
using namespace std; int n,m;
struct ss
{
int num,t,o;
}a[];
set<int> s; bool cmp(ss x,ss y)
{
return x.t < y.t;
} int main()
{
scanf("%d%d",&n,&m);
for(int i = ;i <= n;i++) scanf("%d%d%d",&a[i].o,&a[i].t,&a[i].num);
sort(a+,a++n,cmp);
int cnt = ;
for(int i = ;i <= m;i++)
{
int p,q;
scanf("%d%d",&p,&q);
while(cnt <= n&& a[cnt].t < p)
{
if(a[cnt].o == ) s.insert(a[cnt].num);
else s.erase(a[cnt].num);
cnt++;
}
if(s.count(q)) printf("YES\n");
else printf("NO\n");
}
return ;
}
1161.规律,列方程推一下,其中某一个村庄假设不动,动其他n-1个,最后转化成求绝对值最小问题。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL a[] = {};
int n; int main()
{
while(~scanf("%d",&n) && n > )
{
LL sum1 = ,sum2 = ;
for(int i = ;i <= n;i++)
{
scanf("%lld",&a[i]);
sum1 += a[i];
}
for(int i = ;i <= n;i++)
{
LL t;
scanf("%lld",&t);
sum2 += t;
a[i] = a[i-]+t-a[i];
}
if(sum1 != sum2)
{
printf("No way\n");
continue;
}
sort(a,a+n);
LL t = a[n/],ans = ;
for(int i = ;i < n;i++) ans += abs(t-a[i]);
printf("%lld\n",ans);
}
return ;
}
1162.二分补课次数,树形dp判断。
#include<bits/stdc++.h>
using namespace std; int n,m,k,ok,a[],b[],minn[],cnt[];
bool vis[];
vector<int> v[]; void dfs(int now)
{
minn[now] = a[now];
for(int i = ;i < v[now].size();i++)
{
int t = v[now][i];
if(!vis[t])
{
vis[t] = ;
dfs(t);
}
minn[now] = min(minn[now],minn[t]);
}
minn[now] += k*cnt[now];
} bool pan(int x)
{
memset(cnt,,sizeof(cnt));
memset(vis,,sizeof(vis));
memset(minn,,sizeof(minn));
for(int i = ;i <= x;i++) cnt[b[i]]++;
ok = ;
for(int i = ;i <= n;i++)
{
if(vis[i]) continue;
dfs(i);
if(minn[i] < ) return ;
}
ok = ;
return ;
} int main()
{
ios::sync_with_stdio();
scanf("%d%d%d",&n,&m,&k);
for(int i = ;i < n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
v[x].push_back(y);
}
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
for(int i = ;i <= m;i++) scanf("%d",&b[i]);
int l = ,r = m;
while(l < r)
{
int mid = (l+r)/;
if(pan(mid)) r = mid;
else l = mid+;
}
pan(l);
if(ok) printf("%d\n",l);
else printf("mdzz\n");
return ;
}
1163.扫一边,动态维护每个质因数对应的最大最小值,树状数组位数累和。
#include<bits/stdc++.h>
using namespace std; int n,a[],c[],minn[],maxx[] = {};
long long tree[] = {},ans[]; inline int lowbit(int x)
{
return x&-x;
} void add(int pos,int x)
{
while(pos <= )
{
tree[pos] += x;
pos += lowbit(pos);
}
} long long getsum(int pos)
{
long long sum = ;
while(pos > )
{
sum += tree[pos];
pos -= lowbit(pos);
}
return sum;
} int main()
{
while(~scanf("%d",&n) && n)
{
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
for(int i = ;i <= n;i++) scanf("%d",&c[i]);
memset(maxx,,sizeof(maxx));
memset(minn,0x3f,sizeof(minn));
memset(tree,,sizeof(tree));
for(int i = ;i <= n;i++)
{
int x = a[i];
int mi = 0x3f3f3f3f,ma = ;
for(int j = ;j*j <= x;j++)
{
if(x%j == )
{
while(x%j == ) x /= j;
mi = min(mi,minn[j]);
ma = max(ma,maxx[j]);
minn[j] = min(minn[j],a[i]);
maxx[j] = max(maxx[j],a[i]);
}
}
if(x > )
{
mi = min(mi,minn[x]);
ma = max(ma,maxx[x]);
minn[x] = min(minn[x],a[i]);
maxx[x] = max(maxx[x],a[i]);
}
if(mi == 0x3f3f3f3f) mi = ;
ans[i] = getsum(ma)-getsum(mi-);
add(a[i],c[i]);
}
printf("%lld",ans[]);
for(int i = ;i <= n;i++) printf(" %lld",ans[i]);
printf("\n");
}
return ;
}
1164.因为存孩子会爆,只能用孩子存父节点,然后逆向的dfs,期间需要一个数组保存累加值。
#include<bits/stdc++.h>
using namespace std; int a[],pre[] = {},sum[] = {},n,u,v;
bool vis[] = {}; long long dfs(int pos)
{
if(vis[pos]) return ;
vis[pos] = ;
if(!pre[pos])
{
sum[pos] -= a[pos];
return abs(sum[pos]);
}
long long t = dfs(pre[pos]);
a[pos] += sum[pre[pos]];
t += abs(a[pos]);
sum[pos] += sum[pre[pos]]-a[pos];
return t;
} int main()
{
scanf("%d",&n);
for(int i = ;i < n;i++)
{
scanf("%d%d",&u,&v);
pre[v] = u;
}
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
long long ans = ;
for(int i = ;i <= n;i++) ans += dfs(i);
printf("%lld\n",ans);
return ;
}
1165.题目https://pan.baidu.com/s/1Ro3vR-L_7mnDfbIKjRj2sg
旋转坐标,求二维前缀和。
#include<bits/stdc++.h>
using namespace std; int n,m,k,a[][],b[][],mp[][],sum[][],cnt[][]; int main()
{
while(~scanf("%d%d%d",&m,&n,&k))
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
int maxx = max(n,m),nn = *maxx;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++)
{
int ii = i-j+maxx,jj = i+j;
scanf("%d",&a[ii][jj]);
b[ii][jj] = ;
mp[i][j] = a[ii][jj];
}
}
for(int i = ;i <= nn;i++)
{
for(int j = ;j <= nn;j++)
{
a[i][j] += a[i][j-]+a[i-][j]-a[i-][j-];
b[i][j] += b[i][j-]+b[i-][j]-b[i-][j-];
}
}
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++)
{
int ii = i-j+maxx,jj = i+j;
int x1 = max(,ii-k),y1 = max(,jj-k);
int x2 = min(nn,ii+k),y2 = min(nn,jj+k);
sum[i][j] = a[x2][y2]-a[x1-][y2]-a[x2][y1-]+a[x1-][y1-];
cnt[i][j] = b[x2][y2]-b[x1-][y2]-b[x2][y1-]+b[x1-][y1-];
}
}
for(int i = ;i <= n;i++)
{
printf("%.0f",1.0*(sum[i][]-mp[i][])/(cnt[i][]-)+1e-);
for(int j = ;j <= m;j++) printf(" %.0f",1.0*(sum[i][j]-mp[i][j])/(cnt[i][j]-)+1e-);
printf("\n");
}
}
return ;
}
1166.题目https://pan.baidu.com/s/1Ro3vR-L_7mnDfbIKjRj2sg
将一个串扩充至两倍,另一个串变化几种形态在里面找是否存在相同字串,strstr方便又快速。
#include<bits/stdc++.h>
using namespace std; char a[],b[];
int n; void change()
{
for(int i = ;i < n;i++)
{
if(b[i] == '') b[i] = '';
else b[i] += ;
}
} int main()
{
while(~scanf("%d",&n))
{
getchar();
gets(a);
gets(b);
for(int i = ;i < n;i++) a[i+n] = a[i];
a[*n] = ;
char *p = NULL;
for(int i = ;i < ;i++)
{
p = strstr(a,b);
if(p) break;
if(i != ) change();
}
if(p) printf("yes\n");
else printf("no\n");
}
return ;
}
*1167.待补。
1168.都取出现次数最少的那个字母就可以了。
#include<bits/stdc++.h>
using namespace std; char a[];
int x[],y[]; int main()
{
int T;
scanf("%d",&T);
for(int c = ;c <= T;c++)
{
memset(x,,sizeof(x));
memset(y,,sizeof(y));
scanf("%s",a);
int len = strlen(a);
for(int i = ;i < len;i++)
{
if('a' <= a[i] && a[i] <= 'z') x[a[i]-'a']++;
else y[a[i]-'A']++;
}
sort(x,x+);
sort(y,y+);
printf("Case %d: %d\n",c,min(x[],y[]));
}
return ;
}
1169.不断除以2,直到奇数,操作次数的奇偶性。
#include<bits/stdc++.h>
using namespace std; int f(long long x)
{
if(x&) return ;
else return f(x/)^;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
long long x;
scanf("%d",&x);
if(f(x) == ) printf("First Box\n");
else printf("Second Box\n");
}
}
1170.优化一下暴力的过程,将数量一样的放一起算。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL n,m; LL gcd(LL x,LL y)
{
return y?gcd(y,x%y):x;
} LL c(LL x,LL y)
{
return (y*(y+)*(*y+))/-((x-)*x*(*x-))/;
} int main()
{
while(~scanf("%lld%lld",&n,&m) && n && m)
{
LL ans = ;
int now = ;
while(now <= n)
{
int t = n/now,endd = min(m,n/t);
ans += t*c(now,endd);
if(endd == m) break;
now = endd +;
}
LL x = n*m;
LL g = gcd(ans,x);
printf("%lld/%lld\n",ans/g,x/g);
}
}
1171.dp[i][j][k]表示前i行里有j列1个,k列两个的方案数,自底向上dp可以省略一维空间,记忆化应该快一点。
#include<bits/stdc++.h>
#define MOD 14020130063
using namespace std; int n,m;
long long dp[][][]; long long dfs(int x,int y,int z)
{
if(x < || y < || z < || y+z > m) return ;
if(dp[x][y][z] != -) return dp[x][y][z];
dp[x][y][z] = (dfs(x-,y,z)+(m-(y-)-z)*dfs(x-,y-,z)+(y+)*dfs(x-,y+,z-)+(m-(y-)-z)*(m-(y-)-z-)/*dfs(x-,y-,z)+(y+)*(y+-)/*dfs(x-,y+,z-)+y*(m-y-(z-))*dfs(x-,y,z-))%MOD;
return dp[x][y][z];
} int main()
{
ios::sync_with_stdio();
while(cin >> n >> m)
{
if(m > n) swap(n,m);
memset(dp,-,sizeof(dp));
dp[][][] = ;
dp[][][] = m;
dp[][][] = m*(m-)/;
long long ans = ;
for(int i = ;i <= m;i++)
{
for(int j = ;i+j <= m;j++) ans = (ans+dfs(n,i,j))%MOD;
}
cout << ans << endl;
}
return ;
}
1172.容量为sum/2的01背包。
#include<bits/stdc++.h>
using namespace std; int n,w[],dp[]; int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
memset(dp,,sizeof(dp));
int sum = ;
for(int i = ;i <= n;i++)
{
cin >> w[i];
sum += w[i];
}
int endd = sum/;
for(int i = ;i <= n;i++)
{
for(int j = endd;j >= w[i];j--)
{
dp[j] = max(dp[j],dp[j-w[i]]+w[i]);
}
}
cout << sum-dp[endd]* << endl;
}
return ;
}
1173.是否为3的倍数。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
if(n%) cout << "Yes" << endl;
else cout << "No" << endl;
}
return ;
}
*1174.待补。
1175.拿k长度的线段扫一遍,map维护数量。
#include<bits/stdc++.h>
using namespace std; int n,k,a[] = {}; int main()
{
while(~scanf("%d%d",&n,&k))
{
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
map<int,int> mp;
for(int i = ;i < k;i++) mp[a[i]]++;
int ans = ;
for(int i = k;i <= n;i++)
{
mp[a[i]]++;
if(mp.size() == k) ans++;
if(--mp[a[i-k+]] == ) mp.erase(a[i-k+]);
}
printf("%d\n",ans);
}
return ;
}
1176.渡河dp,每次分两批送走最大的两个人。
#include<bits/stdc++.h>
using namespace std; int a[],n; int main()
{
while(cin >> n)
{
for(int i = ;i <= n;i++) cin >> a[i];
sort(a+,a++n);
long long ans = ;
while(n)
{
if(n == )
{
ans += a[];
break;
}
if(n == )
{
ans += a[];
break;
}
if(n == )
{
ans += a[]+a[]+a[];
break;
}
else
{
ans += min(*a[]+a[n]+a[n-],a[]+*a[]+a[n]);
n -= ;
}
}
cout << ans << endl;
}
return ;
}
1177.排序后树状数组维护。
#include<bits/stdc++.h>
using namespace std; int tree[],n,m,cnt = ,ans[]; struct star
{
int num,x,y,o;
}a[]; bool cmp(star X,star Y)
{
if(X.x == Y.x) return X.y < Y.y;
return X.x < Y.x;
} inline int lowbit(int x)
{
return x & (-x);
} void update(int pos,int x)
{
while(pos <= )
{
tree[pos] += x;
pos += lowbit(pos);
}
} int getsum(int pos)
{
int sum = ;
while(pos > )
{
sum += tree[pos];
pos -= lowbit(pos);
}
return sum;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
printf("Case #%d:\n",cnt++);
memset(tree,,sizeof(tree));
for(int i = ;i <= n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].o = ;
}
for(int i = n+;i <= n+m;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].num = i-n;
a[i].o = ;
}
sort(a+,a++n+m,cmp);
for(int i = ;i <= n+m;i++)
{
if(a[i].o) update(a[i].y+,);
else ans[a[i].num] = getsum(a[i].y+);
}
for(int i = ;i <= m;i++) printf("%d\n",ans[i]);
}
return ;
}
1179.先打表单个硬币能凑成的数的最小硬币个数,枚举每种硬币及个数,更新答案。
#include<bits/stdc++.h>
using namespace std; int a[],ans[] = {},n,s,q;
int main()
{
memset(ans,0x3f,sizeof(ans));
cin >> n >> s;
for(int i = ;i <= n;i++)
{
cin >> a[i];
for(int j = ;j <= s;j++) ans[a[i]*j] = min(ans[a[i]*j],j);
} cin >> q;
while(q--)
{
int t,x = 2e9;
cin >> t;
for(int i = ;i <= n;i++)
{
int endd = min(s,t/a[i]);
for(int j = ;j <= endd;j++)
{
int left = t-j*a[i];
if(left == ) x = min(x,j);
else if(ans[left] && ans[left]+j <= s) x = min(x,ans[left]+j);
}
}
if(x == 2e9) cout << - << endl;
else cout << x << endl;
}
return ;
}
1180.dp[i][j][k]表示取第i个珠子颜色为j且末尾有k个连续的珠子的总数。
k = 1时,dp[i][j][k] = sumx≠j且1≤y≤k(dp[i-1][x][y])
k ≠ 1时,dp[i][j][k] = dp[i-1][j][k-1]
由于对称性,j不影响dp[i][j][k]结果,则忽略第二维,dp[i][1] = sum1≤y≤k(dp[i-1][y])*(m-1)
利用矩阵快速幂,计算sum1≤x≤k-1(dp[n][x]),再乘以m种颜色即为结果。
#include<bits/stdc++.h>
#define MOD 23333
using namespace std; long long n,m,k;
struct matrix
{
long long m[][];
}; matrix one = {},base = {}; matrix mul(matrix a, matrix b)
{
matrix tmp;
for(int i = ; i < k-;i++)
{
for(int j = ; j < k-;j++)
{
tmp.m[i][j] = ;
for(int K = ;K < k-;K++) tmp.m[i][j] = (tmp.m[i][j]+a.m[i][K]*b.m[K][j])%MOD;
}
}
return tmp;
} long long fast_mod(long long n)
{
matrix ans = one,y = base;
while(n)
{
if(n&) ans = mul(ans,y);
y = mul(y,y);
n /= ;
}
long long res = ;
for(int i = ;i < k-;i++) res = (res+ans.m[i][])%MOD;
return res*m%MOD;
} int main()
{
ios::sync_with_stdio(false);
for(int i = ;i < ;i++) one.m[i][i] = ;
for(int i = ;i < ;i++) base.m[i][i-] = ;
while(cin >> n >> m >> k)
{
for(int i = ;i < k-;i++) base.m[][i] = m-;
cout << fast_mod(n-) << endl;
}
return ;
}
1181.因为只有一组数据,直接开vector存每一次的状态,把树状数组改一下就可以了。
#include<bits/stdc++.h>
using namespace std; int a[] = {},n,q,o,t = ,c = ;
vector< pair<int,int> > v[];
vector< pair<int,int> >::iterator it; inline int lowbit(int x)
{
return x&(-x);
} void update(int pos,int x)
{
while(pos <= n)
{
int temp = ;
if(!v[pos].empty()) temp = v[pos].back().second;
v[pos].push_back(make_pair(t,temp+x));
pos += lowbit(pos);
}
} int getsum(int pos,int tt)
{
int sum = ;
while(pos > )
{
it = upper_bound(v[pos].begin(),v[pos].end(),make_pair(tt,));
if(it != v[pos].end() && it->first == tt) sum += it->second;
else if(it != v[pos].begin())
{
it--;
sum += it->second;
}
pos -= lowbit(pos);
}
return sum;
} int main()
{
ios::sync_with_stdio(false);
cin >> n >> q;
while(q--)
{
cin >> o;
if(o == )
{
t++;
int i,x;
cin >> i >> x;
c = i^c;
update(c,x);
a[c] += x;
c = a[c];
}
else
{
int l,r,tt;
cin >> l >> r >> tt;
c = getsum(r^c,tt)-getsum((l^c)-,tt);
cout << c << endl;
}
}
return ;
}
1182.快速乘。
#include<bits/stdc++.h>
#define LL long long
#define MOD 23333333333
using namespace std; LL n,m; LL qmul(LL a, LL b, LL c)
{
LL ans = ;
a = a%c;
while(b)
{
if(b&) ans = (ans+a)%c;
b >>= ;
a = (a+a)%c;
}
return ans;
} int main()
{
while(cin >> n >> m)
{
LL x = m/,y = m%,ans = ;
if(x%) ans = qmul(x*,(*n+x+)/,MOD);
else ans = qmul(x*/,*n+x+,MOD);
for(int i = ;i < y;i++) ans = (ans+n+x+i)%MOD;
cout << ans << endl;
}
return ;
}
1183.划分问题,dp[i][j] = dp[i-1][j-1]+dp[i-j][j]。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; long long dp[][] = {};
int n,m; int main()
{
for(int i = ;i <= ;i++) dp[i][] = ;
for(int i = ;i <= ;i++)
{
for(int j = ;j <= i;j++) dp[i][j] = (dp[i-][j-]+dp[i-j][j])%MOD;
}
for(int i = ;i <= ;i++)
{
for(int j = ;j <= ;j++) dp[i][j] = (dp[i][j-]+dp[i][j])%MOD;
}
while(scanf("%d%d",&n,&m) && n && m) printf("%lld\n",dp[n][m]);
return ;
}
1184.Sprague-Grundy定理。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
while(~scanf("%d",&n))
{
int ans = ,t;
for(int i = ;i <= n;i++)
{
scanf("%d",&t);
ans ^= t%;
}
if(ans) printf("Yes\n");
else printf("No\n");
}
return ;
}
1185.二分时间。
#include<bits/stdc++.h>
using namespace std; int m,k,t[]; bool ok(int x)
{
int cnt = ,sum = ;
for(int i = ;i <= m;i++)
{
if(sum+t[i] < x) sum += t[i];
else if(sum+t[i] == x)
{
cnt++;
sum = ;
}
else
{
cnt++;
sum = t[i];
}
}
if(sum) cnt++;
if(cnt > k) return ;
return ;
} int main()
{
ios::sync_with_stdio(false);
while(cin >> m >> k)
{
for(int i = ;i <= m;i++) cin >> t[i];
int l = ,r = ;
while(l < r)
{
int mid = (l+r)/;
if(ok(mid)) l = mid+;
else r = mid;
}
cout << l << endl;
}
return ;
}
1186.dfs。
#include<bits/stdc++.h>
using namespace std; int n = ,m,len[],vis[][] = {},sum,ok;
int dx[] = {-,,,};
int dy[] = {,-,,};
string a[]; void dfs(int x,int y)
{
vis[x][y] = ;
sum++;
for(int i = ;i < ;i++)
{
int xx = x+dx[i],yy = y+dy[i];
if(xx < || xx > n || yy < || yy > len[x])
{
ok = ;
continue;
}
if(vis[xx][yy]) continue;
if(a[xx][yy] == '#') continue;
dfs(xx,yy);
}
} int main()
{
ios::sync_with_stdio(false);
while(cin >> a[++n])
{
len[n] = a[n].length();
a[n] = ' '+a[n];
}
int ans = ;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= len[i];j++)
{
if(vis[i][j] || a[i][j] == '#') continue;
sum = ;
ok = ;
dfs(i,j);
if(!ok) ans += sum;
}
}
cout << ans << endl;
return ;
}
1187.每个点按度排序,从度小的点开始,把每个点邻接表中在该点前面的点都删掉。然后对于每一条边,统计公共点的个数和。
#include<bits/stdc++.h>
using namespace std; int n,m,vis[],x[],y[];
vector<int> v[];
struct xx
{
int cnt,id;
friend bool operator<(xx a,xx b)
{
return a.cnt < b.cnt;
}
}a[]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(vis,,sizeof(vis));
for(int i = ;i <= n;i++) v[i].clear();
for(int i = ;i <= n;i++) a[i].id = i,a[i].cnt = ;
for(int i = ;i <= m;i++)
{
scanf("%d%d",&x[i],&y[i]);
v[x[i]].push_back(y[i]);
v[y[i]].push_back(x[i]);
a[x[i]].cnt++;
a[y[i]].cnt++;
}
sort(a+,a++n);
for(int i = ;i <= n;i++)
{
int tt = a[i].id;
for(int j = ;j < v[tt].size();j++)
{
int t = v[tt][j];
if(vis[t]) v[tt].erase(v[tt].begin()+j),j--;
}
sort(v[tt].begin(),v[tt].end());
vis[tt] = ;
}
long long ans = ;
for(int i = ;i <= m;i++)
{
int t = x[i],tt = y[i];
int now1 = ,now2 = ;
while(now1 < v[t].size() && now2 < v[tt].size())
{
if(v[t][now1] < v[tt][now2]) now1++;
else if(v[t][now1] > v[tt][now2]) now2++;
else
{
ans++;
now1++;
now2++;
}
}
}
printf("%lld\n",ans);
}
return ;
}
1188.递推,找公式。
#include<bits/stdc++.h>
using namespace std; long long n; int main()
{
ios::sync_with_stdio(false);
while(cin >> n) cout << +n*(n+)/ << endl;
return ;
}
1189.输出注意空格。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
ios::sync_with_stdio(false);
while(cin >> n)
{
for(int i = ;i <= n;i++)
{
int t = -i;
while(t--) cout << " ";
for(int j = ;j < i;j++) cout << (char)('A'+j);
for(int j = i-;j >= ;j--) cout << (char)('A'+j);
cout << endl;
}
}
return ;
}
1190.连续平方数求和,公式,逆元。
#include<bits/stdc++.h>
#define LL long long
using namespace std; long long n,p; LL qmod(LL a, LL b, LL c)
{
LL ans = ;
a = a%c;
while(b)
{
if(b&) ans = ans*a%c;
b >>= ;
a = a*a%c;
}
return ans;
} LL inv(LL a,LL b)
{
return qmod(a,b-,b);
} int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> p)
{
cout << n%p*(n+)%p*(*n+)%p*inv(,p)%p << endl;
} return ;
}
1191.给每一类记录一个标准。
#include<bits/stdc++.h>
#define LL long long
using namespace std; long long n;
vector<string> v[]; int main()
{
ios::sync_with_stdio(false);
while(cin >> n)
{
int cnt= ;
for(int i = ;i <= n;i++) v[i].clear();
for(int i = ;i <= n;i++)
{
string s;
cin >> s;
string ss = s;
sort(ss.begin(),ss.end());
int p = ,flag = ;
for(;p <= cnt;p++)
{ if(v[p][] == ss)
{
v[p].push_back(s);
flag = ;
break;
}
}
if(!flag)
{
v[++cnt].push_back(ss);
v[cnt].push_back(s);
}
}
for(int i = ;i <= cnt;i++)
{
cout << v[i][];
for(int j = ;j <v[i].size();j++) cout << " " << v[i][j];
cout << endl;
}
} return ;
}
1192.线段扫一遍比较每一段。
#include<bits/stdc++.h>
#define LL long long
using namespace std; int n,k,a[]; int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> k)
{
long long ans = ,now = ;
for(int i = ;i <= n;i++) cin >> a[i];
for(int i = ;i <= k;i++) now += a[i];
ans = now;
for(int i = k+;i <= n;i++)
{
now = now+a[i]-a[i-k];
ans = min(now,ans);
}
cout << ans << endl;
}
return ;
}
1193.Sprague-Grundy定理。
#include<bits/stdc++.h>
#define LL long long
using namespace std; int a,b,c,d; int main()
{
ios::sync_with_stdio(false);
while(cin >> a >> b >> c >> d)
{
int t = a%(c+),tt = b%(d+);
if(t^tt) cout << "NUO!" << endl;
else cout << "NO!" << endl;
}
return ;
}
1194.求解模方程a^x=b(mod n),n为素数。大步小步模版。
#include<bits/stdc++.h>
#define LL long long
using namespace std; LL pow_mod(LL a,LL b,LL n)
{
LL s=;
while(b)
{
if(b&)
s=(s*a)%n;
a=(a*a)%n;
b=b>>;
}
return s;
} LL log_mod (LL a,LL b,LL n)
{
LL m,v,e=,i;
m=ceil(sqrt(n+0.5)); //x=i*m+j
//v=inv(pow_mod(a,m,n),n); //a^m*v=1(mod n)
v=pow_mod(a,n-m-,n);
map<LL,LL>x;
x[]=m;
for(i=;i<m;i++)
{
e=(e*a)%n;
if(!x[e])x[e]=i;
}
for(i=;i<m;i++)
{
if(x[b])
{
LL num=x[b];
x.clear();
return i*m+(num==m?:num);
}
b=(b*v)%n; //b=b/(a^m)
} return -;
} int main()
{
LL a,b;
while(scanf("%lld%lld",&a,&b)!=EOF)
{
LL ans1=log_mod(,a,),ans2 = log_mod(,b,); if(ans1==- || ans2 == -) printf("No Solution\n");
else printf("%lld\n",pow_mod(,ans1*ans2,));
}
return ;
}
1195.最短路模版。
#include<bits/stdc++.h>
using namespace std; int n,m,a[][],vis[],dis[]; int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--)
{
cin >> m;
n = ;
memset(a,0x3f,sizeof(a));
while(m--)
{
int x,y,z;
cin >> x >> y >> z;
a[x][y] = a[y][x] = min(a[x][y],z);
n = max(n,x);
n = max(n,y);
}
int s,d;
cin >> s >> d;
memset(vis,,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
dis[s] = ;
for(int i = ;i <= n;i++)
{
int k = -,minn = 0x3f3f3f3f;
for(int j = ;j <= n;j++)
{
if(vis[j]) continue;
if(minn > dis[j])
{
minn = dis[j];
k = j;
}
}
if(k == -) break;
vis[k] = ;
for(int j = ;j <= n;j++)
{
if(vis[j]) continue;
if(dis[j] > dis[k]+a[j][k]) dis[j] = dis[k]+a[j][k];
}
}
cout << *dis[d] << endl;
}
return ;
}
1196.直接判断。
#include <bits/stdc++.h>
using namespace std; int n; int main()
{
ios::sync_with_stdio(false);
while(cin >> n)
{
if(n <= ) cout << "XiaoZhen" << endl;
else if(n < ) cout << "Zh0ngshen" << endl;
else cout << "DaNuo" << endl;
}
return ;
}
1197.直接判断。
#include <bits/stdc++.h>
using namespace std; string s; int main()
{
ios::sync_with_stdio(false);
while(cin >> s)
{
if(s == "v8") cout << "SingleDog&YangRouHuoGuo" << endl;
else if(s == "qsqx") cout << "Couple&Program" << endl;
else cout << "SingleDog&GoodGoodStud" << endl;
}
return ;
}
1198.递推,找公式。
#include <bits/stdc++.h>
using namespace std; long long a[];
int n; int main()
{
ios::sync_with_stdio(false);
a[] = ;
a[] = ;
for(int i = ;i <= ;i++) a[i] = a[i-]+*(i-);
while(cin >> n) cout << a[n] << endl;
return ;
}
1199.圆排列,除数取模不好处理,只要用在乘的时候抵消就可以了。
#include <bits/stdc++.h>
using namespace std; long long n,m,p; int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m >> p)
{
long long ans = ;
int flag = ;
for(int i = n-m+;i <= n;i++)
{
if(!flag && i%m == )
{
ans = ans*i/m%p;
flag = ;
}
else ans = ans*i%p;
}
cout << ans << endl;
}
return ;
}
1200.递推,找公式。
#include <bits/stdc++.h>
using namespace std; long long n; long long qpower(long long a,long long b,long long c)
{
a %= c;
long long ans = ;
while(b)
{
if(b%) ans = ans*a%c;
a = a*a%c;
b /= ;
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
while(cin >> n) cout << ((qpower(,n,)-)*+)% << endl;
return ;
}
1201.素数筛。
#include <bits/stdc++.h>
using namespace std; int vis[] = {},prime[],num[] = {};
long long n; int main()
{
int cnt = ;
for(int i = ;i < ;i++)
{
if(!vis[i]) prime[++cnt] = i;
for(int j = ;i*prime[j] < && j <= cnt;j++)
{
vis[prime[j]*i] = ;
if(!(i%prime[j])) break;
}
}
for(int i = ;i <= ;i++)
{
for(int j = ;j <= i;j++) num[i] += (j+)*(i-j+);
}
while(~scanf("%lld",&n))
{
long long ans = ;
for(int i = ;(long long)prime[i]*prime[i] <= n;i++)
{
if(n%prime[i] == )
{
int t = ;
while(n%prime[i] == )
{
t++;
n /= prime[i];
}
ans *= num[t];
}
}
if(n != ) ans *= ;
printf("%lld\n",ans);
}
return ;
}
1202.最短路构造虚点。
#include<bits/stdc++.h>
using namespace std; struct xx
{
int to;
long long w;
xx(int a,long long b):to(a),w(b){};
friend bool operator <(xx X,xx Y)
{
return X.w > Y.w;
}
}; vector<xx> v[];
int n,m,r,h[][];
long long dis[];
bool vis[],has[]; void add(int a,int b,int x)
{
v[a].push_back(xx(b,x));
v[b].push_back(xx(a,x));
} void dij(int beg)
{
priority_queue<xx> q;
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof(vis));
dis[beg] = ;
q.push(xx(beg,));
while(!q.empty())
{
int now = q.top().to;
q.pop();
if(vis[now]) continue;
vis[now] = ;
for(int i = ;i < v[now].size();i++)
{
int tt = v[now][i].to,ww = v[now][i].w;
if(!vis[tt] && dis[now]+ww < dis[tt])
{
dis[tt] = dis[now]+ww;
q.push(xx(tt,dis[tt]));
}
}
}
} int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--)
{
for(int i = ;i <= ;i++) v[i].clear();
cin >> n >> m >> r;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++) cin >> h[i][j];
}
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++)
{
if(i+ <= n) add(*i+j,*i+j+,*(h[i][j]+h[i+][j]));
if(j+ <= m) add(*i+j,*i+j+,*(h[i][j]+h[i][j+]));
}
}
for(int k = ;k <= r;k++)
{
memset(has,,sizeof(has));
int ax,ay,bx,by,t,kk;
cin >> ax >> ay >> bx >> by >> t >> kk;
for(int i = ax;i <= bx;i++)
{
for(int j = ay;j <= by;j++)
{
add(+*k+h[i][j],*i+j,t);
v[+*k+h[i][j]].push_back(xx(*i+j,));
v[*i+j].push_back(xx(+*k+h[i][j],));
has[h[i][j]] = ;
}
}
for(int i = ;i <= ;i++)
{
if(!has[i]) continue;
for(int j = i+;j <= min(i+kk,);j++)
{
if(!has[j]) continue;
add(+*k+i,+*k+j,*t);
add(+*k+i,+*k+j,*t);
}
}
}
int sx,sy,ox,oy;
cin >> sx >> sy >> ox >> oy;
dij(*sx+sy);
cout << dis[*ox+oy]/ << endl;
}
}
1203.状压dp。
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007 int n,m,k,cnt,sta[<<],a[][<<]; void dfs(int l,int now,int pre1,int pre2)
{
if(l == m)
{
sta[now] = ;
return;
}
for(int i = ;i < k;i++)
{
if(i == pre1 && i == pre2) continue;
dfs(l+,(now<<)|i,pre2,i);
dfs(l+,(now<<)|(i+),pre2,i);
}
} bool ok(int x)
{
int t = x%;
x >>= ;
int tt = x%;
if(t != tt) return ;
x >>= ;
int ttt = x%;
if(t != ttt) return ;
return ;
} int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> m >> k)
{
if(n < m) swap(n,m);
memset(sta,,sizeof(sta));
memset(a,,sizeof(a));
cnt = ;
dfs(,,-,-);
int endd = <<(*m);
for(int i = ;i < endd;i++)
{
if(!sta[i]) continue;
int flag = ;
for(int j = ;j < m;j++)
{
if(i&(<<(j*))) flag = ;
}
if(!flag) a[m][i] = ;
}
for(int i = ;i < n;i++)
{
for(int j = ;j < m;j++)
{
int now = i*m+j+;
for(int l = ;l < endd;l++)
{
if(a[now-][l] == ) continue;
if((l&(<<(j*))) == && (j < || ok(l>>(*j-)))) a[now][l|(<<(j*))] = (a[now][l|(<<(j*))]+a[now-][l])%MOD;
for(int t = ;t < k;t++)
{
if(t == (l>>(j*))%) continue;
int tt = (l-(l&(<<(j*))))|(t<<(j*));
if(j > && !ok(tt>>(*j-))) continue;
a[now][tt] = (a[now][tt]+a[now-][l])%MOD;
}
}
}
}
int t = n*m;
int ans = ;
for(int i = ;i < endd;i++) ans = (ans+a[t][i])%MOD;
cout << ans << endl;
}
return ;
}
1204.大模拟的恐惧。
#include<bits/stdc++.h>
using namespace std; int n,x1,x2,a[][] = {},ok[],vis[]; double calcF()
{
double kin = ,kout = ;
for(int i = ;i <= n;i++)
{
for(int j = i+;j <= n;j++)
{
if(ok[i] && ok[j]) kin += *a[i][j];
else if(ok[i] || ok[j]) kout += a[i][j];
}
}
return kin/(kin+kout);
} double calcf(int x)
{
int t = ok[x];
ok[x] = ;
double t1 = calcF();
ok[x] = ;
double t2 = calcF();
ok[x] = t;
return t1-t2;
} void op1()
{
double maxx = -1e18;
int pos = -;
for(int i = ;i <= n;i++)
{
if(ok[i]) continue;
int flag = ;
for(int j = ;j <= n;j++)
{
if(ok[j] && a[i][j]) flag = ;
}
if(!flag) continue;
double t = calcf(i);
if(t-maxx > 1e- || abs(t-maxx) <= 1e-)
{
maxx = t;
pos = max(pos,i);
}
}
ok[pos] = ;
} void op2()
{
double minn = 1e18;
int pos = -;
for(int i = ;i <= n;i++)
{
if(!ok[i]) continue;
double t = calcf(i);
if(minn-t > 1e- || abs(minn-t) <= 1e-)
{
minn = t;
pos = max(pos,i);
}
}
if(minn < ) ok[pos] = ;
} bool isok()
{
for(int i = ;i <= n;i++)
{
if(ok[i] && calcf(i) <= ) return ;
if(ok[i]) continue;
int flag = ;
for(int j = ;j <= n;j++)
{
if(ok[j] && a[i][j]) flag = ;
}
if(flag && calcf(i) >= ) return ;
}
return ;
} void op(int x)
{
ok[x] = ;
while()
{
op1();
op2();
if(isok()) return;
}
} int main()
{
//freopen("1.txt","r",stdin);
ios::sync_with_stdio();
cin >> x1 >> x2;
int x,y,z;
while(cin >> x >> y >> z)
{
n = max(n,x);
n = max(n,y);
a[x][y] += z;
a[y][x] += z;
}
memset(ok,,sizeof(ok));
op(x1);
if(ok[x1] && ok[x2])
{
cout << "YES" << endl;
return ;
}
memset(ok,,sizeof(ok));
op(x2);
if(ok[x1] && ok[x2])
{
cout << "YES" << endl;
return ;
}
cout << "NO" << endl;
return ;
}
1205.构造x为10。
#include<bits/stdc++.h>
using namespace std; string s;
stack<char> ss; int main()
{
ios::sync_with_stdio(false);
while(cin >> s)
{
if(s.length() == )
{
cout << "1 10" << endl;
cout << << " " << s[] << endl;
continue;
}
for(int i = ;i < s.length();i++) ss.push(s[i]);
cout << s.length()- << " " << << endl;
cout << ss.top();
ss.pop();
while(!ss.empty())
{
cout << " " << ss.top();;
ss.pop();
}
cout << endl;
}
return ;
}
1206.等比求和推。
#include<bits/stdc++.h>
#define LL long long
using namespace std; string s; int main()
{
ios::sync_with_stdio(false);
while(cin >> s)
{
LL t = ,x = ,y = ;
int i = ,len = s.length();
int a = ,b = ;
for(;i < len && s[i] != '.';i++) t = t*+s[i]-'';
i++;
for(;i < len && s[i] != '_';i++)
{
x = x*+s[i]-'';
a++;
}
i++;
y = x;
for(;i < len && s[i] != '.';i++)
{
y = y*+s[i]-'';
b++;
}
LL sub,mom,z = ;
if(a == && b != )
{
while(b--) z = z*+;
LL g = __gcd(y,z);
sub = y/g,mom = z/g;
}
else if(a == && b == )
{
sub = ;
mom = ;
}
else if(b == )
{
z = ;
while(a--) z *= ;
LL g = __gcd(x,z);
sub = x/g,mom = z/g;
}
else
{
y -= x;
while(b--) z = z*+;
while(a--) z *= ;
LL g = __gcd(y,z);
sub = y/g,mom = z/g;
}
sub += t*mom;
cout << sub << "/" << mom << endl;
}
return ;
}
1207.dp,过程中有个累和优化。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,l,r,sum[],t[]; int main()
{
ios::sync_with_stdio(false);
while(cin >> n >> l >> r)
{
memset(sum,,sizeof(sum));
memset(t,,sizeof(t));
t[] = ;
sum[] = ;
while(n--)
{
int c,a;
cin >> c >> a;
for(int i = ;i+c <= r;i++)
{
t[i+c] = (t[i]+t[i+c])%MOD;
int tt = i+(a+)*c;
if(tt <= r) t[tt] = (t[tt]-sum[i]+MOD)%MOD;
}
memcpy(sum,t,(r+)*sizeof(int));
}
for(int i = l+;i <= r;i++) sum[i] = (sum[i-]+sum[i])%MOD;
cout << sum[r] << endl;
}
return ;
}
1208.每次少k-1个瓶盖,推公式。
#include<bits/stdc++.h>
#define MOD 1000000007
#define LL long long
using namespace std; int a,b,k; LL qmod(LL a, LL b, LL c)
{
LL ans = ;
a = a%c;
while(b)
{
if(b&) ans = ans*a%c;
b >>= ;
a = a*a%c;
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
while(cin >> a >> b >> k)
{
LL x = qmod(a,b,k-);
if(x == ) x = k-;
cout << (qmod(a,b,MOD)-x+MOD)%MOD*qmod(k-,MOD-,MOD)%MOD << endl;
}
return ;
}
1209.dp,f[i] = sum[i-1]*2/i+i。
#include<bits/stdc++.h>
using namespace std; int n;
double f[],sum[]; int main()
{
ios::sync_with_stdio();
f[] = ;
sum[] = ;
for(int i = ;i <= ;i++)
{
f[i] = *sum[i-]/i+i;
sum[i] = sum[i-]+f[i];
}
int T;
cin >> T;
while(T--)
{
cin >> n;
cout << f[n] << endl;
}
return ;
}
1210.
#include<bits/stdc++.h>
#define MOD 19260817
using namespace std; int n,q,a[];
long long euler(long long n)
{
long long ans = ;
for(int i = ;(long long)i*i <= n;i++)
{
if(n%i == )
{
n /= i;
ans *= i-;
while(n%i == )
{
n /= i;
ans *= i;
}
}
}
if(n > ) ans *= n-;
return ans;
} int main()
{
while(~scanf("%d",&n))
{
for(int i = ;i <= n;i++)
{
int x;
scanf("%d",&x);
a[i] = euler(x);
}
scanf("%d",&q);
while(q--)
{
long long ans = ;
int x;
scanf("%d",&x);
for(int i = ;i <= n;i++) ans = ans*(__gcd(x-,a[i])+)%MOD;
printf("%lld\n",ans);
}
}
return ;
}
1211.首项系数为1,根肯定为整数,且为an因子,找个大素数取模防止溢出。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,a[]; long long qpower(long long a, long long b, long long c)
{
long long ans = ;
a = a%c;
while(b)
{
if(b%) ans = ans*a%c;
a = a*a%c;
b /= ;
}
return ans;
} bool ok(long long x)
{
long long t = ;
for(int i = ;i <= n;i++) t = (t+a[i]*qpower(x,n-i,MOD)+MOD)%MOD;
return t == ;
} int main()
{
ios::sync_with_stdio();
a[] = ;
while(cin >> n)
{
for(int i = ;i <= n;i++) cin >> a[i];
long long x = abs(a[n]),ans = ;
for(long long i = ;i*i <= x;i++)
{
if(x%i) continue;
if(ok(i)) ans += i;
if(ok(-i)) ans -= i;
if(i*i == x) continue;
if(ok(x/i)) ans += x/i;
if(ok(-x/i)) ans -= x/i;
}
cout << fixed << setprecision() << (double)ans << endl;
}
return ;
}
1213.对m排序,双指针维护m符合的序列,树状数组维护w。
#include<bits/stdc++.h>
using namespace std; int n,X,Y,tree[]; inline int lowbitt(int x)
{
return x&-x;
} void add(int pos,int x)
{
while(pos <= )
{
tree[pos] += x;
pos += lowbitt(pos);
}
} int getsum(int pos)
{
int ans = ;
while(pos > )
{
ans += tree[pos];
pos -= lowbitt(pos);
}
return ans;
} struct xx
{
int x,y;
friend bool operator<(xx a,xx b)
{
return a.x < b.x;
}
}a[]; int main()
{
ios::sync_with_stdio(false);
while(~scanf("%d%d%d",&n,&X,&Y))
{
memset(tree,,sizeof(tree));
for(int i = ;i <= n;i++) scanf("%d",&a[i].x);
for(int i = ;i <= n;i++) scanf("%d",&a[i].y);
sort(a+,a++n);
int now = ,r = ;
long long ans = ;
for(int i = n;i >= ;i--)
{
while(a[r].x <= X-a[i].x) r++;
while(now > i)
{
--now;
add(a[now].y,-);
}
while(now < i && now < r)
{
add(a[now].y,);
now++;
}
ans += getsum(Y-a[i].y);
}
printf("%lld\n",ans);
}
return ;
}
1216.dfs序+划分树,或dfs序+主席树。
#include<bits/stdc++.h>
#define N 100005
using namespace std; int n,m,cnt,l[N],r[N],pos[N];
int a[N];
vector<int> v[N]; void dfs(int now,int pre)
{
l[now] = ++cnt;
pos[cnt] = now;
for(int i = ;i < v[now].size();i++)
{
int t = v[now][i];
if(t == pre) continue;
dfs(t,now);
}
r[now] = cnt;
} int tree[][N],sorted[N],toleft[][N]; void build(int l,int r,int dep)
{
if(l == r)return;
int mid = (l+r)/,same = mid-l+;
for(int i = l;i <= r;i++)
{
if(tree[dep][i] < sorted[mid]) same--;
}
int lpos = l,rpos = mid+;
for(int i = l;i <= r;i++)
{
if(tree[dep][i] < sorted[mid]) tree[dep+][lpos++] = tree[dep][i];
else if(tree[dep][i] == sorted[mid] && same > )
{
tree[dep+][lpos++] = tree[dep][i];
same--;
}
else tree[dep+][rpos++] = tree[dep][i];
toleft[dep][i] = toleft[dep][l-]+lpos-l;
}
build(l,mid,dep+);
build(mid+,r,dep+);
} int query(int l,int r,int ql,int qr,int dep,int k)
{
if(ql == qr) return tree[dep][ql];
int mid = (l+r)/,cnt = toleft[dep][qr]-toleft[dep][ql-];
if(cnt >= k)
{
int ll = l+toleft[dep][ql-]-toleft[dep][l-],rr = ll+cnt-;
return query(l,mid,ll,rr,dep+,k);
}
else
{
int rr = qr+toleft[dep][r]-toleft[dep][qr],ll = rr-(qr-ql-cnt);
return query(mid+,r,ll,rr,dep+,k-cnt);
}
} int main()
{
while(~scanf("%d",&n))
{
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
for(int i = ;i <= n;i++) v[i].clear();
cnt = ;
for(int i = ;i < n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
dfs(,-);
for(int i = ;i <= n;i++) sorted[i] = tree[][i] = a[pos[i]];
sort(sorted+,sorted+n+);
build(,n,);
scanf("%d",&m);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",query(,n,l[x],r[x],,y));
}
}
return ;
}
1226.待补。
1127.lucas+中国剩余。
#include<bits/stdc++.h>
using namespace std; int n;
long long a[]; long long qmod(long long a,long long b,long long c)
{
long long ans = ;
a = a%c;
while(b)
{
if(b%) ans = (ans*a)%c;
a = (a*a)%c;
b /= ;
}
return ans;
} long long c(long long m,long long n,long long MOD)
{
if(m < n) return ;
if(m == n) return ;
if(n > m-n) n = m-n;
long long mm = ,nn = ;
for(long long i = ;i < n;i++)
{
mm = mm*(m-i)%MOD;
nn = nn*(n-i)%MOD;
}
return mm*qmod(nn,MOD-,MOD)%MOD;
} long long lucas(long long m,long long n,long long MOD)
{
long long ans = ;
while(m && n && ans)
{
ans = ans%MOD*c(m%MOD,n%MOD,MOD)%MOD;
n /= MOD;
m /= MOD;
}
return ans;
} long long e_gcd(long long a,long long b,long long &x,long long &y)
{
if(!b)
{
x = ;
y = ;
return a;
}
long long d = e_gcd(b,a%b,y,x);
y -= a/b*x;
return d;
} long long solve(long long *m,long long *a,long long n)
{
long long M = m[],A = a[],x,y;
for(int i = ;i <= n;i++)
{
long long d = e_gcd(M,m[i],x,y);
if((a[i]-A)%d) return -;
x = (a[i]-A)/d*x%(m[i]/d);
A += x*M;
M = M/d*m[i];
A %= M;
}
if(A < ) A += M;
return A;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i <= n;i++) scanf("%lld",&a[i]);
long long t1 = ,t2 = ,t3 = ;
for(int i = ;i <= n;i++)
{
t1 = (t1+lucas(n-,i-,)*a[i])%;
t2 = (t2+lucas(n-,i-,)*a[i])%;
t3 = (t3+lucas(n-,i-,)*a[i])%;
}
long long a[],m[];
a[] = t1;
a[] = t2;
a[] = t3;
m[] = ;
m[] = ;
m[] = ;
printf("%lld\n",solve(m,a,));
}
return ;
}
1129.len-串和反串的LCS。
#include<bits/stdc++.h>
using namespace std; int dp[][];
char a[],b[]; int main()
{
ios::sync_with_stdio(false);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",&a);
int len = strlen(a);
for(int i = ;i < len;i++) b[i] = a[len-i-];
memset(dp,,sizeof(dp));
for(int i = ;i <= len;i++)
{
for(int j = ;j <= len;j++)
{
if(a[i-] == b[j-]) dp[i][j] = dp[i-][j-]+;
else dp[i][j] = max(dp[i-][j],dp[i][j-]);
}
}
printf("%d\n",len-dp[len][len]);
}
return ;
}
1233.转化为LIS,nlogn。
#include<bits/stdc++.h>
using namespace std; int n,a[],b[],mp[]; int main()
{
ios::sync_with_stdio();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
for(int i = ;i <= n;i++) scanf("%d",&b[i]);
for(int i = ;i <= n;i++) mp[b[i]] = i;
for(int i = ;i <= n;i++) a[i] = mp[a[i]];
b[] = a[];
int len = ;
for(int i = ;i <= n;i++)
{
int t = lower_bound(b+,b+len+,a[i])-b;
b[t] = a[i];
if(t == len+) len++;
}
printf("%d\n",len); }
return ;
}
1234.打表会发现规律。
#include<bits/stdc++.h>
using namespace std; int n;
double a[],b[],R; int main()
{
ios::sync_with_stdio();
cin >> n;
if(n == )
{
cout << "0.00" << endl;
return ;
}
for(int i = ;i <= n;i++) cin >> a[i];
cin >> R;
sort(a+,a++n);
reverse(a+,a++n);
int l = ,r = n,cnt = ;
while(l <= r)
{
if(cnt%) b[r--] = a[cnt++];
else b[l++] = a[cnt++];
}
double ans = b[]+b[n];
for(int i = ;i < n;i++) ans += *sqrt(R*(b[i]+b[i+]-R));
cout << fixed << setprecision() << ans << endl;
return ;
}
1237.二分轮数。
#include<bits/stdc++.h>
using namespace std; int n,maxx = ,cnt[] = {}; bool ok(int x)
{
if(x < maxx) return ;
if(x-maxx > ) return ;
int now = ;
for(int i = ;i <= x;i++)
{
if(now > 1e6) break;
if(cnt[x-i] > now) return ;
now -= cnt[x-i];
now *= ;
}
return ;
} int main()
{
scanf("%d",&n);
for(int i = ;i <= n;i++)
{
int x;
scanf("%d",&x);
cnt[x]++;
maxx = max(maxx,x);
}
int l = ,r = 1e6+;
while(l < r)
{
int mid = (l+r)/;
if(ok(mid)) r = mid;
else l = mid+;
}
printf("%d\n",l);
return ;
}
1239.从高位往低位贪心,尽可能将1和并成偶数,维护每个可以划分的位置。
#include<bits/stdc++.h>
using namespace std; int n,k,a[][],ok[][]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
for(int i = ;i <= n;i++)
{
int x;
scanf("%d",&x);
for(int j = ;j <= ;j++)
{
a[i][j] = x%;
x /= ;
}
}
for(int i = ;i <= n;i++) ok[i][] = ;
int ans = ;
for(int j = ;j >= ;j--)
{
int cnt = ,now = ;
for(int i = ;i <= n;i++)
{
now ^= a[i][j];
if(now == && ok[i][j+])
{
ok[i][j] = ;
cnt++;
}
else ok[i][j] = ;
}
if(cnt < k || now == )
{
ans += (<<(j-));
for(int i = ;i <= n;i++) ok[i][j] = ok[i][j+];
}
}
cout << ans << endl;
}
return ;
}
1240.贪心找大于等于且最小的。
#include<bits/stdc++.h>
using namespace std; int n,a[]; int main()
{
while(~scanf("%d",&n))
{
multiset<int> s;
for(int i = ;i <= n;i++)
{
int x;
scanf("%d",&x);
s.insert(x);
}
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
int ans = ;
for(int i = ;i <= n;i++)
{
auto it = s.lower_bound(a[i]);
if(it != s.begin())
{
it--;
ans++;
s.erase(it);
}
}
if(ans) printf("%d\n",ans);
else printf("Godv too strong\n");
}
return ;
}
1241.dp[i][j]表示前i个时刻,j个芯片最大的rp。
#include<bits/stdc++.h>
using namespace std; int n,m,a[],dp[][],x[][]; int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> n >> m;
for(int i = ;i <= n;i++) cin >> a[i];
for(int i = ;i <= n;i++)
{
int minn = ;
for(int j = i;j <= n;j++)
{
minn = min(minn,a[j]);
x[i][j] = minn;
}
}
memset(dp,,sizeof(dp));
for(int j = ;j <= m;j++)
{
for(int i = n;i >= ;i--)
{
for(int k = i;k >= ;k--) dp[i][j] = max(dp[i][j],dp[k-][j-]+(i-k+)*x[k][i]);
}
for(int i = ;i <= n;i++) dp[i][j] = max(dp[i][j],dp[i-][j]);
}
cout << dp[n][m] << endl;
}
return ;
}
*1242.待补。
1243.移项,开根号。
#include<bits/stdc++.h>
using namespace std; long long z,ans; void ok(long long x)
{
long long endd = sqrt(x/);
for(long long i = ;i <= endd;i++)
{
long long a = i*i,b = x-a;
long long t = sqrt(b);
if(t*t == b && __gcd(a,b) == && a != b) ans++;
}
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> z;
ans = ;
long long endd = sqrt(*z);
for(long long i = ;i <= endd;i++)
{
if(*z%i) continue;
ok(i);
ok(*z/i);
}
cout << ans* << endl;
}
return ;
}
*1244.待补。
1245.数位dp+kmp。
#include<bits/stdc++.h>
using namespace std; int a[],b[],x[],cnt1,cnt2;
long long n,m,dp[][]; long long dfs(int now1,int now2,int limit)
{
if(now2 == ) return ;
if(now1 == ) return ;
if(!limit && dp[now1][now2] != -) return dp[now1][now2];
int endd = limit?a[now1]:;
long long ans = ;
for(int i = endd;i >= ;i--)
{
int t = now2;
while(b[t] != i && t != cnt2) t = x[t];
if(b[t] == i) t--;
ans += dfs(now1-,t,limit && i == endd);
}
if(!limit) dp[now1][now2] = ans;
return ans;
} int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> n >> m;
cnt1 = ;
cnt2 = ;
while(n)
{
a[++cnt1] = n%;
n /= ;
}
while(m)
{
b[++cnt2] = m%;
m /= ;
}
int i = cnt2,j = cnt2+;
x[cnt2] = cnt2+;
while(i >= )
{
if(j == cnt2+ || b[i] == b[j]) x[--i] = --j;
else j = x[j];
}
x[cnt2] = cnt2;
memset(dp,-,sizeof(dp));
cout << dfs(cnt1,cnt2,)- << endl;
}
return ;
}
*1247.待补。
*1248.待补。
*1250.待补。
1254.高精度。
#include<bits/stdc++.h>
using namespace std; string s1,s2,s3,s4; int compare(string str1,string str2)
{
if(str1.length() > str2.length()) return ;
else if(str1.length() < str2.length()) return -;
else return str1.compare(str2);
} string sub(string str1,string str2)
{
string str;
int flag = ;
if(compare(str1,str2) < )
{
flag = ;
swap(str1,str2);
}
int tmp = str1.length()-str2.length(),cf = ;
for(int i = str2.length()-;i >= ;i--)
{
if(str1[tmp+i] < str2[i]+cf)
{
str = char(str1[tmp+i]-str2[i]-cf+''+)+str;
cf = ;
}
else
{
str = char(str1[tmp+i]-str2[i]-cf+'')+str;
cf = ;
}
}
for(int i = tmp-;i >= ;i--)
{
if(str1[i]-cf >= '')
{
str = char(str1[i]-cf)+str;
cf = ;
}
else
{
str = char(str1[i]-cf+)+str;
cf = ;
}
}
str.erase(,str.find_first_not_of(''));
if(str.empty()) str = "";
if(flag) str = "-"+str;
return str;
} int main()
{
cin >> s1 >> s2 >> s3 >> s4;
string t = sub(s2,s1),y = sub(s4,s3);
int x = compare(t,y);
if(x > ) cout << "Ting" << endl;
else if(x < ) cout << "Yu" << endl;
else cout << "Excellent" << endl;
return ;
}
1255.进制转化。
#include<bits/stdc++.h>
using namespace std; string s; int main()
{
while(getline(cin,s))
{
stringstream ss(s);
char c;
int x,y,z;
ss >> c >> x >> c >> y >> c >> z;
cout << "#" << hex << uppercase << setw() << setfill('') << x << setw() << setfill('') << y << setw() << setfill('') << z << endl;
}
return ;
}
1256.处理每一位。
#include<bits/stdc++.h>
using namespace std; int n;
string yi[] = {"","I","X","C","M"};
string wu[] = {"","V","L","D"}; int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
string s = "";
int now = ;
while(n)
{
int t = n%;
n /= ;
if(t <= )
{
while(t--) s = yi[now]+s;
}
else if(t <= )
{
s = wu[now]+s;
if(t == ) s = yi[now]+s;
}
else if(t <= )
{
t -= ;
while(t--) s = yi[now]+s;
s = wu[now]+s;
}
else s = yi[now]+yi[now+]+s;
now++;
}
cout << s << endl;
}
return ;
}
1257.两个绝对值的最大值可转化为非绝对值的形式,维护两个最小值。
#include<bits/stdc++.h>
using namespace std; int n;
struct xx
{
int x,y;
friend bool operator<(xx a,xx b)
{
return a.x < b.x;
}
}a[]; int main()
{
ios::sync_with_stdio();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i <= n;i++) scanf("%d%d",&a[i].x,&a[i].y);
sort(a+,a++n);
long long ans = ,minn1 = 1e18,minn2 = 1e18;
for(int i = ;i <= n;i++)
{
ans = max(ans,max(a[i].x+a[i].y-minn1,a[i].x-a[i].y-minn2));
minn1 = min(minn1,(long long)a[i].x+a[i].y);
minn2 = min(minn2,(long long)a[i].x-a[i].y);
}
cout << ans << endl;
}
return ;
}
1259.n-1。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> n;
cout << n- << endl;
}
return ;
}
1260.加权平均值。
#include<bits/stdc++.h>
using namespace std; int n;
map<string,double> mp1;
map<string,int> mp2; int main()
{
ios::sync_with_stdio();
cin >> n;
for(int i = ;i <= n;i++)
{
string s;
double x;
int y;
cin >> s >> x >> y;
if(mp2.count(s))
{
mp2[s] = max(mp2[s],y);
}
else
{
mp1[s] = x;
mp2[s] = y;
}
}
double sum1 = ,sum2 = ,sum3;
for(auto it = mp1.begin();it != mp1.end();it++)
{
int t = mp2[it->first];
sum1 += t*it->second;
if(t < ) sum2 += ;
else if(t < ) sum2 += *it->second;
else if(t < ) sum2 += 1.5*it->second;
else if(t < ) sum2 += 1.7*it->second;
else if(t < ) sum2 += *it->second;
else if(t < ) sum2 += 2.3*it->second;
else if(t < ) sum2 += 2.7*it->second;
else if(t < ) sum2 += *it->second;
else if(t < ) sum2 += 3.3*it->second;
else if(t < ) sum2 += 3.7*it->second;
else sum2 += *it->second;
sum3 += it->second;
}
cout << fixed << setprecision() << sum1/sum3 << endl;
cout << fixed << setprecision() << sum2/sum3 << endl;
return ;
}
1261.带属性的并查集。
#include<bits/stdc++.h>
using namespace std; int n,m,a[],pre[]; int findd(int x)
{
if(x == pre[x]) return x;
int t = findd(pre[x]);
a[x] ^= a[pre[x]];
pre[x] = t;
return pre[x];
} void join(int x,int y)
{
int xx = findd(x),yy = findd(y);
if(xx != yy)
{
a[xx] = a[x]^a[y]^;
pre[xx] = yy;
}
} int main()
{
ios::sync_with_stdio();
while(~scanf("%d%d",&n,&m))
{
memset(a,,sizeof(a));
for(int i = ;i <= n;i++) pre[i] = i;
for(int i = ;i <= m;i++)
{
int t,x,y;
scanf("%d%d%d",&t,&x,&y);
if(t)
{
int xx = findd(x),yy = findd(y);
if(xx != yy) printf("Not sure yet.\n");
else if(a[x] == a[y]) printf("In the same category.\n");
else printf("In different category.\n");
}
else join(x,y);
}
}
return ;
}
1262.先求Q的前缀和后缀和。
#include<bits/stdc++.h>
using namespace std; string s;
long long l[] = {},r[] = {}; int main()
{
ios::sync_with_stdio();
cin >> s;
int n = s.length();
s = " "+s;
for(int i = ;i <= n;i++) l[i] = l[i-]+(s[i] == 'Q');
for(int i = n;i >= ;i--) r[i] = r[i+]+(s[i] == 'Q');
long long ans = ;
for(int i = ;i <= n;i++)
{
if(s[i] == 'A') ans += l[i-]*r[i+];
}
cout << ans << endl;
return ;
}
1263.用r来分隔g和b,注意r的个数限制,以及r只有两个时g和b的限制,以及n=1的特例。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> n;
int r = ,g = ,b = ;
for(int i = ;i <= ;i++)
{
string s;
cin >> s;
for(int j = ;j < n;j++)
{
if(s[j] == 'R') r++;
else if(s[j] == 'G') g++;
else b++;
}
}
if(n == && r != && (!g || !b) || n > && r <= n && (!g || !b || r == && g% == && b% == || r > )) cout << "YES" << endl;
else cout << "NO" << endl;
}
return ;
}
1264.二进制输出。
#include<bits/stdc++.h>
using namespace std; int a[];
long long n; int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
memset(a,,sizeof(a));
int now = ;
while(n)
{
a[now--] = n%;
n /= ;
}
for(int i = ;i <= ;i++) cout << char(a[i]?'R':'G');
cout << endl;
}
return ;
}
*1266.待补。
1269.bfs。
#include<bits/stdc++.h>
using namespace std; string s[];
bool vis[][][] = {};
int dis[][][];
int dx[] = {-,,,};
int dy[] = {,,-,};
struct xxx
{
int x,y,b;
xxx(){};
xxx(int xx,int yy,int bb = ):x(xx),y(yy),b(bb){};
}; int main()
{
ios::sync_with_stdio();
memset(dis,0x3f,sizeof(dis));
for(int i = ;i <= ;i++)
{
cin >> s[i];
s[i] = " "+s[i];
}
queue<xxx> q;
int endx,endy;
for(int i = ;i <= ;i++)
{
for(int j = ;j <= ;j++)
{
if(s[i][j] == 'S')
{
q.push(xxx(i,j,));
vis[i][j][] = ;
dis[i][j][] = ;
}
else if(s[i][j] == 'E')
{
endx = i;
endy = j;
}
}
}
while(!q.empty())
{
int x = q.front().x,y = q.front().y,b = q.front().b;
q.pop();
if(x == endx && y == endy) break;
for(int i = ;i < ;i++)
{
int xx = x+dx[i],yy = y+dy[i],bb = b;
if(xx < || xx > || yy < || yy > ) continue;
if(s[xx][yy] == 'r' && (bb == || bb == )) continue;
if(s[xx][yy] != 'r' && !vis[xx][yy][bb])
{
if(bb == && s[xx][yy] == 'B') bb = ;
vis[xx][yy][bb] = ;
dis[xx][yy][bb] = min(dis[xx][yy][bb],dis[x][y][b]+);
q.push(xxx(xx,yy,bb));
}
else if(s[xx][yy] == 'r' && bb == && !vis[xx][yy][])
{
vis[xx][yy][] = ;
dis[xx][yy][] = min(dis[xx][yy][],dis[x][y][b]+);
q.push(xxx(xx,yy,));
}
}
}
int ans = min(min(dis[endx][endy][],dis[endx][endy][]),dis[endx][endy][]);
if(ans != 0x3f3f3f3f) cout << ans << endl;
else cout << - << endl;
return ;
}
1272.排序和去重。
#include<bits/stdc++.h>
using namespace std; int n,a[],b[],c[] = {}; int main()
{
ios::sync_with_stdio();
cin >> n;
for(int i = ;i <= n;i++) cin >> a[i];
for(int i = ;i <= n;i++) b[i] = a[i];
sort(b+,b++n);
cout << b[];
for(int i = ;i <= n;i++) cout << " " << b[i];
cout << endl;
int maxx = b[n-],minn = b[];
for(int i = ;i <= n;i++) b[i] = a[i];
vector<int> v;
map<int,int> mp;
for(int i = ;i <= n;i++)
{
if(mp.count(b[i])) continue;
v.push_back(b[i]);
mp[b[i]] = ;
}
cout << v[];
for(int i = ;i < v.size();i++) cout << " " << v[i];
cout << endl;
cout << maxx << " " << minn << endl;
for(int i = ;i <= n;i++) c[a[i]]++;
maxx = ;
for(int i = ;i <= ;i++) maxx = max(maxx,c[i]);
v.clear();
for(int i = ;i <= ;i++)
{
if(c[i] == maxx) v.push_back(i);
}
cout << v[];
for(int i = ;i < v.size();i++) cout << " " << v[i];
cout << endl;
return ;
}
1273.不断交换到对应下标位置。
#include<bits/stdc++.h>
using namespace std; int n,b[]; struct xx
{
int x,pos;
friend bool operator<(xx a,xx b)
{
return a.x < b.x;
}
}a[]; int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = ;i <= n;i++)
{
cin >> a[i].x;
a[i].pos = i;
}
sort(a+,a++n);
for(int i = ;i <= n;i++) b[a[i].pos] = i;
int ans = ;
for(int i = ;i <= n;i++)
{
while(b[i] != i)
{
ans++;
swap(b[i],b[b[i]]);
}
}
cout << ans << endl;
}
return ;
}
1274.推公式。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> n;
cout << (n-)*(n-)/ << endl;
}
return ;
}
1275.枚举左下,右上两个点。
#include<bits/stdc++.h>
using namespace std; int n,a[][]; int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
memset(a,,sizeof(a));
cin >> n;
for(int i = ;i <= n;i++)
{
int x,y;
cin >> x >> y;
a[x][y] = ;
}
int ans = ;
for(int i = ;i <= ;i++)
{
for(int j = ;j <= ;j++)
{
if(!a[i][j]) continue;
int endd = min(-i,-j);
for(int k = ;k <= endd;k++)
{
if(a[i+k][j] && a[i][j+k] && a[i+k][j+k]) ans++;
}
}
}
cout << ans << endl;
}
return ;
}
1276.维护个前缀和累计值。
#include<bits/stdc++.h>
using namespace std; int n,m,a[] = {}; int main()
{
ios::sync_with_stdio();
while(cin >> n >> m)
{
memset(a,,sizeof(a));
for(int i = ;i <= m;i++)
{
int x;
cin >> x;
a[]++;
a[x+]--;
}
for(int i = ;i <= n;i++) a[i] += a[i-];
for(int i = ;i <= n;i++) cout << a[i] << endl;
}
return ;
}
1277.判断旋转的9种情况是否可行。
#include<bits/stdc++.h>
using namespace std; int a[],b[];
int pan[][] = {{,,,},{,,,},{,,,},{,,,},{,,,},{,,,}};
int zhuan[][] = {{,,,,,,,},{,,,,,,,},{,,,,,,,}};
bool ok()
{
for(int i = ;i < ;i++)
{
if(!(b[pan[i][]] == b[pan[i][]] && b[pan[i][]] == b[pan[i][]] && b[pan[i][]] == b[pan[i][]])) return ;
}
return ;
} int main()
{
ios::sync_with_stdio();
while(cin >> a[])
{
int flag = ;
for(int i = ;i <= ;i++) cin >> a[i];
for(int i = ;i < ;i++)
{
for(int k = ;k <= ;k++)
{
for(int j = ;j <= ;j++) b[j] = a[j];
for(int j = ;j < ;j++) b[zhuan[i][j]] = a[zhuan[i][(j+*k)%]];
if(ok()) flag = ;
}
}
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
return ;
}
1278.n小于等于100时,必为91。
#include<bits/stdc++.h>
using namespace std; long long n; int main()
{
ios::sync_with_stdio();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
if(n > ) printf("%lld\n",n-);
else printf("91\n");
}
return ;
}
1281.f(n) = f(n-1)+f(n-2)+f(n-4),矩阵快速幂。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; long long n; struct matrix
{
long long m[][];
}; matrix one = { ,,,,
,,,,
,,,,
,,, }; matrix base = { ,,,,
,,,,
,,,,
,,, };
matrix mul(matrix a, matrix b)
{
matrix tmp;
for(int i = ; i < ;i++)
{
for(int j = ; j < ;j++)
{
tmp.m[i][j] = ;
for(int k = ; k < ;k++) tmp.m[i][j] = (tmp.m[i][j]+a.m[i][k]*b.m[k][j])%MOD;
}
}
return tmp;
} matrix maqower(matrix a,long long b)
{
matrix ans = one;
while(b)
{
if(b%) ans = mul(ans,a);
a = mul(a,a);
b /= ;
}
return ans;
} int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
if(n == ) cout << << endl;
else if(n == ) cout << << endl;
else if(n == ) cout << << endl;
else if(n == ) cout << << endl;
else
{
matrix ans = maqower(base,n-);
cout << (*ans.m[][]+*ans.m[][]+*ans.m[][]+ans.m[][])%MOD << endl;
}
}
return ;
}
1282.推公式。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
cin >> n;
cout << n*(n-)/ << endl;
}
return ;
}
1283.从中位数开始向上发,使这n/2+1个人最小值最大。
#include<bits/stdc++.h>
using namespace std; int n,m;
long long a[]; int main()
{
ios::sync_with_stdio();
while(cin >> n >> m)
{
for(int i = ;i <= n;i++) cin >> a[i];
sort(a+,a++n);
reverse(a+,a++n);
int k = (n+)/,now = k;
long long sum = ;
while(now > && (a[now-]-a[now])*(k-now+)+sum <= m)
{
sum += (a[now-]-a[now])*(k-now+);
now--;
}
m -= sum;
cout << a[now]+m/(k-now+) << endl;
}
return ;
}
1284.最大公约数。
#include<bits/stdc++.h>
using namespace std; int a,b; int main()
{
ios::sync_with_stdio();
while(cin >> a >> b) cout << __gcd(a,b) << endl;
return ;
}
1285.简单推一下公式,记忆化dp。
#include<bits/stdc++.h>
using namespace std; int n;
double dp[]; double dfs(int x)
{
if(dp[x] > -1e-) return dp[x];
dp[x] = ;
for(int i = ;i <= x;i++)
{
if(x%i == ) dp[x] += 1.0/x;
else dp[x] += (dfs(x%i)+)/x;
}
double t = 1.0*x/n;
dp[x] += (-t)/t;
return dp[x];
} int main()
{
ios::sync_with_stdio();
while(cin >> n)
{
for(int i = ;i <= n;i++) dp[i] = -;
double ans = ;
for(int i = ;i <= n;i++) ans += dfs(i)/n;
cout << fixed << setprecision() << ans << endl;
}
return ;
}
1286.dp或者规律。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n;
int endd = sqrt(n+0.1);
long long ans = ;
for(int i = ;i <= endd;i++)
{
while(n%i == )
{
ans += i-;
n /= i;
}
}
if(n > ) ans += n-;
cout << ans <<endl;
}
return ;
}
1284.二分,三人或以上相同=1-无人相同-两人相同,计算时注意精度问题。
#include<bits/stdc++.h>
using namespace std; int n,ans[] = {,,,,,,,,,,};
double c[][]; void get_C(int maxn)
{
c[][] = ;
for(int i = ;i <= maxn;i++)
{
c[i][] = ;
for(int j = ;j <= i;j++) c[i][j] = c[i-][j]+c[i-][j-];
}
} bool ok(int x)
{
if(x > *n) return ;
double t1 = ,t2 = ,tt = ;
if(x > n) t1 = ;
else
{
for(int i = n-x+;i <= n;i++)
{
t1 *= 1.0*i/n;
}
}
for(int k = ;k <= x/;k++)
{
tt *= 1.0*c[*k][];
tt /= k*(n-x+k);
t2 += tt*c[x][*k];
}
for(int i = n-x+;i <= n;i++) t2 *= 1.0*i/n;
double t = -t1-t2;
return t > 0.5+1e-;
} int main()
{
ios::sync_with_stdio();
get_C();
while(cin >> n)
{
if(n <= )
{
cout << ans[n] << endl;
continue;
}
int l = ,r = min(,n);
while(l < r)
{
int mid = (l+r)/;
if(ok(mid)) r = mid;
else l = mid+;
}
cout << l << endl;
}
return ;
}
1288.根据逆序数判断是否有解,dfs,康拓展开储存状态。
#include<bits/stdc++.h>
using namespace std; struct Node
{
int a[],num,Hash;
}start; int HASH[] = {,,,,,,,,};
int vis[],dis[] = {-,-,,},step[];
int a[][]; int get_Hash(Node x)
{
int ans = ;
for(int i = ;i < ;i++)
{
int k = ;
for(int j =;j < i;j++)
{
if(x.a[j] > x.a[i]) k++;
}
ans += HASH[i]*k;
}
return ans;
} int main()
{
ios::sync_with_stdio();
int T;
cin >> T;
while(T--)
{
memset(vis,,sizeof(vis));
for(int i = ;i < ;i++)
{
cin >> start.a[i];
if(start.a[i] == ) start.num = i;
}
int num = ;
for(int i = ;i < ;i++)
{
for(int j = i+;j <;j++)
{
if(start.a[i] && start.a[j] && start.a[i]>start.a[j]) num++;
}
}
if(num%)
{
cout << "cannot" << endl;
continue;
}
start.Hash = get_Hash(start);
step[start.Hash] = ;
vis[start.Hash] = ;
queue<Node> q;
q.push(start);
while(!q.empty())
{
Node temp = q.front();
q.pop();
if(temp.Hash == ) break;
int xx = temp.num/,yy = temp.num%;
for(int i = ;i < ;i++)
{
Node x = temp;
x.num += dis[i];
if(yy == && i == ) continue;
if(yy == && i == ) continue;
if(x.num < || x.num > ) continue;
swap(x.a[x.num],x.a[temp.num]);
x.Hash = get_Hash(x);
if(vis[x.Hash]) continue;
step[x.Hash] = step[temp.Hash]+;
q.push(x);
vis[x.Hash] = ;
}
}
cout << step[] << endl;
}
return ;
}
1289.最优解必定为递增的相邻或递减的相邻,对于每种情况,找大于等于当前的最小值或小于等于当前的最大值。
#include<bits/stdc++.h>
using namespace std; int n,a[],l[]; int main()
{
while(~scanf("%d",&n))
{
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
double ans = 1e9;
l[] = ;
for(int i = ;i <= n;i++)
{
l[i] = i-;
while(l[i] >= && a[i] > a[l[i]]) l[i] = l[l[i]];
if(l[i] != ) ans = min(ans,1.0*(a[l[i]]-a[i])/(i-l[i]));
}
for(int i = ;i <= n;i++) a[i] = -a[i];
l[] = ;
for(int i = ;i <= n;i++)
{
l[i] = i-;
while(l[i] >= && a[i] > a[l[i]]) l[i] = l[l[i]];
if(l[i] != ) ans = min(ans,1.0*(a[l[i]]-a[i])/(i-l[i]));
}
printf("%.2f\n",ans);
}
return ;
}
1290.dp或者规律。
#include<bits/stdc++.h>
using namespace std; int n; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int endd = sqrt(n+0.1);
long long ans = ;
for(int i = ;i <= endd;i++)
{
while(n%i == )
{
ans += i-;
n /= i;
}
}
if(n > ) ans += n-;
printf("%d\n",ans);
}
return ;
}
1291.转化一下。
#include<bits/stdc++.h>
using namespace std; map<string,string> mp; int main()
{
mp["zero"] = "ling";
mp["one"] = "yi";
mp["two"] = "er";
mp["three"] = "san";
mp["four"] = "si";
mp["five"] = "wu";
mp["six"] = "liu";
mp["seven"] = "qi";
mp["eight"] = "ba";
mp["nine"] = "jiu";
mp["ten"] = "shi";
int T;
cin >> T;
while(T--)
{
string s;
cin >> s;
cout << mp[s] << endl;
}
return ;
}
1292.记录一下出现过的字母。
#include<bits/stdc++.h>
using namespace std; int n;
string s; int main()
{
ios::sync_with_stdio();
while(cin >> s)
{
set<char> st;
for(int i = ;i < s.length();i++) st.insert(s[i]);
cin >> n;
int maxx = ;
while(n--)
{
cin >> s;
int ok = ;
for(int i = ;i < s.length();i++)
{
if(!st.count(s[i])) ok = ;
}
if(ok) maxx = max(maxx,(int)s.length());
}
cout << maxx << endl;
}
return ;
}
1293.二分答案。
#include<bits/stdc++.h>
using namespace std; int n,k;
long long a[]; bool ok(int x)
{
int cnt = ;
for(int i = ;i <= n;i++)
{
for(int j = i+;j <= n;j++)
{
if(min(a[i],a[j]) <= x) cnt++;
}
}
return cnt >= n*(n-)/-k+;
} int main()
{
ios::sync_with_stdio();
int t;
cin >> t;
while(t--)
{
cin >> n >> k;
for(int i = ;i <= n;i++) cin >> a[i];
int cnt = ;
sort(a+,a++n);
int l = ,r = n;
while(l < r)
{
int mid = (l+r)/;
if(ok(a[mid])) r = mid;
else l = mid+;
}
cout << a[l] << endl;
}
return ;
}
1294.求均值向下取整。
#include<bits/stdc++.h>
using namespace std; int n,k;
long long a[]; int main()
{
ios::sync_with_stdio();
int t;
cin >> t;
while(t--)
{
cin >> n;
int sum = ;
for(int i = ;i <= n;i++)
{
cin >> a[i];
sum += a[i];
}
cout << sum/n <<endl;
}
return ;
}
1295.dp[i][j]表示前i位起,余数为j的个数。
#include<bits/stdc++.h>
using namespace std; string s;
long long dp[][]; int main()
{
ios::sync_with_stdio();
while(cin >> s)
{
memset(dp,,sizeof(dp));
if(s[] == '') dp[][] = ;
else dp[][] = ;
for(int i = ;i < s.length();i++)
{
if(s[i] == '')
{
dp[i][] += dp[i-][]+;
dp[i][] += dp[i-][];
dp[i][] += dp[i-][];
}
else
{
dp[i][] += dp[i-][]+;
dp[i][] += dp[i-][];
dp[i][] += dp[i-][];
}
}
long long ans = ;
for(int i = ;i < s.length();i++) ans += dp[i][];
cout << ans << endl;
}
return ;
}
*1296.待补。
*1297.待补。
*1298.待补。
XDOJ的更多相关文章
- xdoj 1241--余神的rp机(区间dp)
xdoj 1241---余神的rp机 核
- xdoj新生现场赛1269——带有限制条件的bfs 寻找最短路径
bfss是解决最短路径的强大武器 (尝试dfs寻找最短路径 -(7*7)就会爆炸) 例题1 ccf 201604-4 游戏 问题描述 小明在玩一个电脑游戏,游戏在一个n×m的方格图上进行,小明控制 ...
- XDOJ 1046 - 高精度模板综合测试 - [高精度模板]
题目链接:http://acm.xidian.edu.cn/problem.php?id=1046 题目描述 请输出两个数的和,差,积,商,取余.注意不要有前导零. 输入 多组数据,每组数据是两个整数 ...
- [xdoj]1303jlz的刷题黑科技
先分析复杂度,给的数据是1e5的,那么我们至少需要一个nlogn的算法才可以.由于答案是一个数字,首先想到是二分法(一般答案是一个数字都可以通过二分法来完成) 下面是思路: 1.可以完成题目的条件是, ...
- [xdoj]1299&1300朱神的烦恼 朱神的序列
http://acm.xidian.edu.cn/problem.php?id=1299 1.第一道题简单的很,数据范围最多只有1e4,对于数组中的每一个元素进行两个for循环,i=0;i<n; ...
- [xdoj] 1310 DSKer的卡牌游戏
http://acm.xidian.edu.cn/problem.php?id=1310 1. 这道题可以类比括号匹配,YY和yy是两组可以匹配的信号,当然要注意逻辑是否正确,一开始进行括号匹配算法的 ...
- [xdoj] 1301&1302 数字计数 数字计数的复仇
1.首先需要掌握二进制数的一种特性,00=0,01=1,10=2,11=3.每一个二进制的值代表他前面的二进数的个数,比如11=3,他的前面就有三个二进制的数字,不过在本题中,题目数据是1-n,故把0 ...
- xdoj 1330---异或(找规律)
我是打表找的规律 233 样例什么作用都没有 只会迷惑作用... 1330: 天才琪露诺的完美算数教室 时间限制: 1 Sec 内存限制: 128 MB Special Judge提交: 37 ...
- xdoj 1067组合数学+动态规划 (一个题断断续续想了半年 233)
题目分析 : (8 4) 可以由(7 4),(6,4),( 4,4) 基础上转化 意味着一个新加入的元素可以按照它加入的方式分类,从而实现动态规划 核心:加入方式 新加入的元素构成转换环的元素个数(n ...
- xdoj 1237 (贪心+逆向思维)
提示: 当有的元素分裂的同时,其他元素也可以+1 分析: 逆向思维,把当前数列变成一个0: 相应得操作相反: 每个元素减1 相同得两个元素可以合并 设数列中最大的数是max,则一共需要减max次才可 ...
随机推荐
- 投票:OAuth2.0 技术选型你会怎么选
1. 前言 在使用 OAuth2.0 中 Authorization Server (授权服务器)是一个回避不了的设施,在大多数情况下我们调用的是一些知名的.可靠的.可信任的第三方平台,比如 QQ.微 ...
- CCPC-Wannafly Winter Camp Day1 (Div2 ABCFJ) 待补...
Day1 Div2 场外链接 按题目顺序~ A 机器人 传送门 题意:有两条平行直线A.B,每条直线上有n个点,编号为1~n.在同一直线上,从a站点到b站点耗时为两点间的距离.存在m个特殊站点,只有在 ...
- 【记】VirtualBox安装CentOS6
推荐随笔 VirtualBox中安装CentOS-6.6虚拟机 问题1: 在选择虚拟硬盘大小时,最好不要用默认的8G 我的分区 /boot 200M swap 1024M /home 4096M / ...
- await Task.Yield()和await Task.CompletedTask有什么不同
有时候我们在代码中要执行一些非常耗时的操作,我们不希望这些操作阻塞调用线程(主线程)的执行,因为调用线程(主线程)可能还有更重要的工作要做,我们希望将这些非常耗时的操作由另外一个线程去执行,这个时候就 ...
- 关于django中的get_or_create方法的坑
最近在项目中发现了这样的一个坑,那就是我们的需求是不能添加一个相同的对象到数据库中,就通过某些字段的值组合成唯一值到数据库中去查找数据,如果没有找到对象,那就创建一条新的数据库记录,而刚好django ...
- hdu4841
今天天气确实很好! 接下来是圆桌问题,顺便做个vector容器的笔记方便以后复习.嘿嘿 Problem Description圆桌上围坐着2n个人.其中n个人是好人,另外n个人是坏人.如果从第一个人开 ...
- 【java面试】算法篇之堆排序
一.堆的概念 堆是一棵顺序存储的完全二叉树.完全二叉树中所有非终端节点的值均不大于(或不小于)其左.右孩子节点的值. 其中每个节点的值小于等于其左.右孩子的值,这样的堆称为小根堆: 其中每个节点的值大 ...
- 大叔 EF 来分析 EntityFrameworks.Data.Core 2
Extensions 1DbCommand拦截器扩展DbCommandInterceptorExtensions 2Class for IQuerable extensions methods Inc ...
- jade 的 考古
Jade是一款高性能简洁易懂的模板引擎(加上这两个字我想起了发动机,为什么不直接叫发动机呢), Jade是Haml的Javascript实现, 在服务端(NodeJS)及客户端均有支持. haml 是 ...
- Windos下的一些命令集合
由于在CMD模式下(也就是命令行)有较多的有用的命令.以下是自己平时所记录下来的以帮助平时的任务. 1. 显示计算机的操作系统 wmic os get osarchitecture /value