Link:

Codeforces #174 传送门

A:

求原根的个数,有一条性质是原根个数为$\phi(\phi(n))$,多了一个不会证的性质

如果要确定哪些是原根的话还是要枚举,不过对于每个数不用枚举$p$次了

由于$\delta_p(x) | \phi(x)$,只要对欧拉函数值的约数枚举即可

不过此题好像直接$O(p^2)$枚举就行了……

#include <bits/stdc++.h>

using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> P;
typedef double db;
int p,res; int main()
{
scanf("%d",&p);
for(int i=;i<p;i++)
{
bool f=;int t=;
for(int j=;j<=p-;j++)
{
t=(t*i)%p;
if(t==) f=;
}
if(t*i%p!=) f=;
res+=f;
}
printf("%d",res);
return ;
}

Problem A

B:

简单分类

#include <bits/stdc++.h>

using namespace std;
int n,A,I;char s[]; int main()
{
scanf("%d%s",&n,s+);
for(int i=;i<=n;i++)
if(s[i]=='A') A++;
else if(s[i]=='I') I++;
if(!I) printf("%d",A);
else if(I==) printf("");
else printf("");
return ;
}

Problem B

C:

明明写起来很简单的一道题目被我弄复杂了

考虑维护序列中原来的值和增量

由于只要求当前最后一个数的增量,因此可以每次仅在增加的最后一个数打上标记

这样在删除数时将当前末尾的增量向前推就能满足要求

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int MAXN=2e5+;
int n,det[MAXN],dat[MAXN],lst;double sum; int main()
{
scanf("%d",&n);
dat[]=;lst=;
while(n--)
{
int op,x,y;
scanf("%d",&op);
if(op==) scanf("%d%d",&x,&y),sum+=x*y,det[x]+=y;
else if(op==) scanf("%d",&x),sum+=x,dat[++lst]=x;
else sum-=dat[lst]+det[lst],det[lst-]+=det[lst],det[lst--]=;
printf("%.6lf\n",sum/lst);
}
return ;
}

Problem C

结果我这个zz用树状数组维护了这个东西,还将一个增量写成了赋值WA了好几次……

#include <bits/stdc++.h>

using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> P;
const int MAXN=2e5+;
int n;ll bit[MAXN<<],dat[MAXN<<],lst,sum; void Update(int pos,int x)
{while(pos<=n) bit[pos]+=x,pos+=pos&(-pos);}
ll Query(int pos)
{ll ret=;while(pos) ret+=bit[pos],pos-=pos&(-pos);return ret;} int main()
{
scanf("%d",&n);
lst=;sum=;
for(int i=;i<=n;i++)
{
int op,x,y;
scanf("%d",&op);
if(op==)
{
scanf("%d%d",&x,&y);
Update(,y);Update(x+,-y);
dat[]+=y;dat[x+]-=y;
sum+=x*y;
}
else if(op==)
{
scanf("%d",&x);
Update(lst+,x);Update(lst+,-x);
dat[++lst]+=x;dat[lst+]=-x;
sum+=x;
}
else
{
sum-=Query(lst);
Update(lst+,-dat[lst+]);
Update(lst,dat[lst+]);
dat[lst]+=dat[lst+];dat[lst+]=;lst--;
}
printf("%.6lf\n",1.0*sum/lst);
}
return ;
}

Problem C

D:

可以发现真正的状态数只有$2*n$个,对于每一位只有加/减两种状态

发现$y$的值就是走过的总距离,这样用$dp[n][2]$记忆化搜索在当前状态走到边界有多远

每个状态在入栈时打上标记,如果再次走到形成环就是循环了

这样对于每个$i$就是对应于$dp[i+1][0]$时的解

注意将边界$dp[1][0/1]$设为-1

#include <bits/stdc++.h>

using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> P;
const int MAXN=2e5+;
int n,dat[MAXN],vis[MAXN][];ll dp[MAXN][]; ll dfs(int x,int d)
{
if(x<=||x>n) return ;
if(vis[x][d]==) return -;
if(vis[x][d]==) return dp[x][d];
vis[x][d]=;
ll dist=d?dfs(x+dat[x],):dfs(x-dat[x],);
vis[x][d]=;
if(dist==-) return dp[x][d]=-;
else return dp[x][d]=dist+dat[x];
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&dat[i]);
vis[][]=vis[][]=;
for(int i=;i<=n;i++)
{
if(!vis[i][]) dfs(i,);
printf("%I64d\n",dp[i][]==-?-:i+dp[i][]-);
}
return ;
}

Problem D

E:

由于每个点只有一个后继,因此最终图只有环和链

将环的情况排除后就只剩链了

对于一条链,一个可行解为$a_1*w_1+a_2*w_2...+a_n*w_n(a_1>a_2>...>a_n)$

但由于后面的限制条件不能直接背包,考虑将式子变换后消除限制:

设$d_i=a_i-a_{i-1},suf_i=w_i+w_{i+1}...+w_n$,

这样式子变为$d_1*suf_1+d_2*suf_2...+d_n*suf_n$

发现就是一个完全背包,且可以将各个链合并在一起考虑

但注意,这个背包要求$d_1,d_2...d_{n-1}>0$,否则无法保证条件成立

也就是原式每个物品的个数要达到${n-1,n-2...2,1,0}$的下限!

这样就先都取一个,并将其从$t$中减去再跑背包即可

#include <bits/stdc++.h>

using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int,int> P;
const int MAXN=1e5+,MOD=1e9+;
int n,q,t,x,y,a[MAXN],nxt[MAXN],d[MAXN],dp[MAXN],cnt; int main()
{
scanf("%d%d%d",&n,&q,&t);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=q;i++)
scanf("%d%d",&x,&y),nxt[x]=y,d[y]++;
for(int i=;i<=n&&t>=;i++)
{
if(d[i]) continue;
int sum=;//t<0一定要及时退出!
for(int j=i;j&&t>=;j=nxt[j])
{
cnt++;
sum+=a[j];a[j]=sum;
if(nxt[j]) t-=a[j];
}
}
if(cnt<n||t<) return puts(""),; dp[]=;
for(int i=;i<=n;i++)
for(int j=a[i];j<=t;j++)
(dp[j]+=dp[j-a[i]])%=MOD;
printf("%d",dp[t]);
return ;
}

Problem E

同时还有一个注意点:$t<0$时要及时退出否则会爆$int$

[Codeforces #174] Tutorial的更多相关文章

  1. [Codeforces #172] Tutorial

    Link: Codeforces #172 传送门 A: 一眼看上去分两类就可以了 1.每个矩形只有两条边相交,重合的形状为菱形 2.每个矩形四条边都有相交 对于情况1答案为$h*h/sin(a)$ ...

  2. [Codeforces #514] Tutorial

    Link: Codeforces #514 传送门 很简单的一场比赛打崩了也是菜得令人无话可说…… D: 一眼二分,发现对于固定的半径和点,能包含该点的圆的圆心一定在一个区间内,求出区间判断即可 此题 ...

  3. [Codeforces #210] Tutorial

    Link: Codeforces #210 传送门 A: 贪心,对每个值都取最大值,不会有其他解使答案变优 #include <bits/stdc++.h> using namespace ...

  4. [Codeforces #196] Tutorial

    Link: Codeforces #196 传送门 A: 枚举 #include <bits/stdc++.h> using namespace std; #define X first ...

  5. [Codeforces #190] Tutorial

    Link: Codeforces #190 传送门 A: 明显答案为$n+m-1$且能构造出来 #include <bits/stdc++.h> using namespace std; ...

  6. [Codeforces #211] Tutorial

    Link: Codeforces #211 传送门 一套非常简单的题目,但很多细节都是错了一次才能发现啊…… 还是不能养成OJ依赖症,交之前先多想想corner case!!! A: 模拟,要特判0啊 ...

  7. [Codeforces #192] Tutorial

    Link: Codeforces #192 传送门 前两天由于食物中毒现在还要每天挂一天的水 只好晚上回来随便找套题做做找找感觉了o(╯□╰)o A: 看到直接大力模拟了 但有一个更简便的方法,复杂度 ...

  8. [Codeforces #201] Tutorial

    Link: 传送门 代码量很少的一套思维题 A: 试一试发现最后状态一定是所有$min,max$间$gcd$的倍数 直接判断数量的奇偶性即可 #include <bits/stdc++.h> ...

  9. [Codeforces #188] Tutorial

    Link: Codeoforces #188 传送门 A: 先全转为正数,后面就全是指数级增长了 #include <bits/stdc++.h> using namespace std; ...

随机推荐

  1. UIAlertView---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址   UIAlertView   //转载请注明出处--本文永久链接:http://ww ...

  2. jQuery操作Table学习总结[转]

    <style type="text/css">       .hover       {                  }    </style>< ...

  3. Windows下基于python3使用word2vec训练中文维基百科语料(一)

    在进行自然语言处理之前,首先需要一个语料,这里选择维基百科中文语料,由于维基百科是 .xml.bz2文件,所以要将其转换成.txt文件,下面就是相关步骤: 步骤一:下载维基百科中文语料 https:/ ...

  4. Coursera在线学习---第九节(2).推荐系统

    一.基于内容的推荐系统(Content Based Recommendations) 所谓基于内容的推荐,就是知道待推荐产品的一些特征情况,将产品的这些特征作为特征变量构建模型来预测.比如,下面的电影 ...

  5. mysql not null default / default

    not null default 说明不能是NULL, 并设置默认值 default 设置默认值 , 但值也可能是NULL mysql> create table test (id int, n ...

  6. linux和ubuntu防火墙相关命令

    1.永久有效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2.即刻生效 开启: service iptables start 关闭: se ...

  7. 2015多校第6场 HDU 5358 First One 枚举,双指针

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5358 题意:如题. 解法:观察式子发现,由于log函数的存在,使得这个函数的值域<=34,然后我 ...

  8. wifi驱动移植

    目标板:Hi3518 内核版本:linux3.0.8   1.修改makefile #PLATFORM = PC //注释掉 PLATFORM = HI3518 //支持平台 ifeq ($(PLAT ...

  9. Median_of_Two_Sorted_Arrays(理论支持和算法总结)

    可以将这个题目推广到更naive的情况,找两个排序数组中的第K个最大值(第K个最小值). 1.直接 merge 两个数组,然后求中位数(第K个最大值或者第K个最小值),能过,不过复杂度是 O(n + ...

  10. awk处理之案例五:awk匹配字段2包含字段1的文本

    编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...