思路
对于一个数的后k位,在进行计算时,对他造成影响的只有后k位。由于给出的数据很大,所以我们再进行计算时算一次对算出的结果进行取模。这样我们就可以保证它不爆 long long 了 还有在最后进行处理的时候要把每一个数都存在一个数组中,然后不够的补0
代码
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 100001 using namespace std; long long n,x,k,l,ans; long long a[N],b[N],c[14]; long long read() { long long x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } long long pow(long long x,long long y) { long long sum=1; while(y) { if(y&1) sum=(sum*x)%k; x=(x*x)%k; y>>=1; } return sum; } int main() { freopen("digits.in","r",stdin); freopen("digits.out","w",stdout); n=read(),k=read(); l=k;k=1; for(int i=1;i<=l;i++) k*=10; for(int i=1;i<=n;i++) a[i]=read()%k,b[i]=read(); x=read();x%=k; for(int i=1;i<=n;i++) ans=(ans+a[i]*pow(x,b[i])%k)%k; for(int i=l;i>=1;i--) c[i]=ans%10,ans/=10; for(int i=1;i<=l;i++) printf("%d\n",c[i]); return 0; }
思路:
这道题在考试的时候我用的暴力枚举做的30分的部分分
先看看这个代码??
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,x1,x2,x3,x4,x5,x6,ans; int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f; } int main() { freopen("equation.in","r",stdin); freopen("equation.out","w",stdout); n=read(); x1=read(),x2=read(),x3=read(),x4=read(),x5=read(),x6=read(); for(int a=1;a<=n;a++) for(int b=1;b<=n;b++) for(int c=1;c<=n;c++) for(int d=1;d<=n;d++) for(int e=1;e<=n;e++) for(int f=1;f<=n;f++) if(x1*a-x2*b+x3*c-x4*d+x5*e-x6*f==0) ans++; printf("%d",ans); return 0; }
此时大佬像渣渣投来鄙视的眼神!(大佬:靠,这道题那么水你竟然只会部分分?!)
(⊙o⊙)…
好吧,来,大佬讲讲这答题吧....
大佬:哼,我讲就我讲!(鄙视。。。。)
我们来看这个题给出的方程:a1.x1-a2.x2+a3.x3-a4.x4+a5.x5-a6.x6=0
可以移项为a1.x1+a3.x3+a5.x5=a2.x2+a4.x4+a6.x6
然后我们用三个for循环来把等号前面的结果给记录下来。
然后再用三个for循环来判断等号后面的数是否和前面的数相等,这样我们就可以ac了!
(大佬再次投来鄙视的眼神。。)
渣渣表示无奈。。。。。
好了,来看看满分代码吧。
代码:
#include<map> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,x1,x2,x3,x4,x5,x6,ans; map<int,int>ma; int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f; } int main() { freopen("equation.in","r",stdin); freopen("equation.out","w",stdout); n=read(); x1=read(),x2=read(),x3=read(),x4=read(),x5=read(),x6=read(); for(int a=1;a<=n;a++) for(int b=1;b<=n;b++) for(int c=1;c<=n;c++) ma[x1*a+x3*b+x5*c]++; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) for(int k=1;k<=n;k++) ans+=ma[x2*i+x4*j+x6*k]; printf("%d",ans); return 0; }
思路:
我们在看着道题的时候,他让求最长路径最短。我们先sort一遍那么最长的路径一定就是最后走的了(为什么啊??大佬:白痴,都sort了,你说呢? 哼,白眼)我们在跑一边最小生成树不就好了吗??先把小的边加进去,那么最后到达终点的时候不就一定是最大的了吗??(恩,好像是。ORZ)
代码:
#include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define maxn 333444 int if_z,tot,ans,n,m,k,x,y,z; int fa[maxn]; bool flag; char Cget; struct Edge { int l,r,w; }edge[maxn]; int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f; } bool cmp(Edge a,Edge b) { return a.w<b.w; } int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } int main() { //freopen("graph.in","r",stdin); //freopen("graph.out","w",stdout); n=read();m=read();k=read(); for(int i=1;i<=m;i++) {edge[i].l=read();edge[i].r=read();edge[i].w=read();} for(int i=1;i<=n;i++) fa[i]=i; sort(edge+1,edge+m+1,cmp); for(int i=1;i<=m;i++) { int fx=find(edge[i].l); int fy=find(edge[i].r); if(fx!=fy) { fa[fx]=fy;tot++; ans=edge[i].w; } if(find(1)==find(n)) { printf("%d",edge[i].w); flag=true;break; } if(tot==n-1)break; } if(!flag)printf("-1"); return 0; }