J TLE code: 啊啊啊啊啊while(scanf("%d",!=EOF))啊啊啊啊 加上AC了...........................***
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll pre[10005];
ll qmod(ll a,ll b,ll c){
ll ans=1;
a=a%c;
while(b){
if(b&1) ans=a*ans%c;
a=a*a%c;
b>>=1;
}
return ans;
}
struct node{
ll x,y,v;
}N[100005];
ll find(ll x){
return x==pre[x]?x:find(pre[x]);
// if(x==pre[x]) return x ;
// return pre[x]= find(pre[x]);
}
void join(ll x,ll y){
ll fx=find(x),fy=find(y);
if(fx!=fy) pre[fx]=fy;
}
bool cmp(node a,node b){
return a.v<b.v;
}
int main(){
ll n,m;
while(scanf("%lld%lld",&n,&m)!=EOF){
for(ll i=0;i<=n;++i){
pre[i]=i;
}
for(ll i=0;i<m;++i){
scanf("%lld%lld%lld",&N[i].x,&N[i].y,&N[i].v);
}
sort(N,N+m,cmp);
ll ans=0,cnt=0;
for(int i=0;i<m;++i){
if(find(N[i].x)!=find(N[i].y)){
join(find(N[i].x),find(N[i].y));
ans+=N[i].v;
cnt++;
}
}
if(cnt==n-1) {
cout<<qmod(ans,n,1000000007)<<endl;
}
else cout<<"-1"<<endl;
}
return 0;
}
F
题目描述
给出一个正整数将其质因数分解的结果按照标准格式打印出来
输入
一个正整数T。代表有T组数据(T <= 10)
每组数据有一个非负整数n ( 0 <=n<=1000000)。
输出
若n能进行因式分解则按照标准格式讲质因数分解的结果打印出来,否则输出n。每组数据需要
换行。
输入样例
5
0
1
2
4
10
输出样例
0
1
2=2^1
4=2^2
10=2^1*5^1
7
F AC code: 比赛的时候一直RE mdzz了把b数组开到main函数里面了。。。*tm
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p;
ll prime[100000];
ll is[1000005],b[1000005];
void pri(){
p=0;
for(ll i=0;i<=1000005;i++) is[i]=1;
is[0]=is[1]=0;
for(ll i=2;i<=1000005;i++) {
if(is[i]) {
prime[p++]=i;
for(ll j=2*i;j<=1000005;j+=i) is[j]=0;
}
}
}
int main(){
pri();
ll T,n,t;
cin>>T;
while(T--){
cin>>n;
if(n==0) cout<<'0'<<endl;
else if(n==1) cout<<'1'<<endl;
else{
t=n;
memset(b,0,sizeof(b));
for(ll i=0;i<p;i++){
while(t%prime[i]==0){
b[prime[i]]++;
t/=prime[i];
}
}
cout<<n<<'=';
ll cnt=0;
for(ll i=0;i<p;i++) {
if(b[prime[i]]){
cnt++;
if(cnt==1)
cout<<prime[i]<<'^'<<b[prime[i]];
else cout<<'*'<<prime[i]<<'^'<<b[prime[i]];
}
}
cout<<endl;
}
}
return 0;
}
//标程
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"--------------"<<endl
typedef long long ll;
const int MAXN = 1e6+10;
const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;
int t,n,num;
int a[MAXN],an[MAXN];
void get_Prime(int n){
clr(a);clr(an);
num = 0;
for(int i=2;i*i<=n;i++){
if(n%i == 0){
while(n%i == 0){
a[num] = i;
n /= i;
an[num] ++;
}
num ++;
}
}
if(n != 1){
a[num] = n; an[num++] ++;
}
}
int main(){
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
cin >> t;
while(t--){
scanf("%d",&n);
get_Prime(n);
if(n == 0) printf("0\n");
else if(n == 1) printf("1\n");
else {
printf("%d=",n);
for(int i=0;i<num-1;i++)
printf("%d^%d*",a[i],an[i]);
printf("%d^%d\n",a[num-1],an[num-1]);
}
}
return 0;
}
I AC code:
题目描述
XM从家出发去商店购物,在他家附近有两家商店。从他家到商店1有一条长为d1的道路,从
他家到商店2有一条长为d2的道路;还有一条长为d3的道路,直接把这两家商店连在一起。帮
助XM计算出他从家出发去两家商店购物并返回家中需要走的最小距离
XM总是从他家出发。两个商店他都要光顾,而且只能沿着现有的三条道路移动,然后返回他的
房子。他可以去同一家商店多次,也可以走同一条路多次。唯一的目标是使行程的总距离最小
化。
注意:XM的目标是从家出发光顾两个商店(两个商店都要光顾),并返回家。
注意:d1,d2,d3非直线距离,而是道路长度。道路可以是弯曲的,即数据保证家,商店1,商店2可
以连接在一起。
输入
第一行输入一个整数T(1≤T≤200),代表T组测试样例。
对于每组测试样例输入包含三个整数d1、d2、d3(1≤d1、d2、d3≤100000)-路径的长度
d1是连接XM家和第一家商店的道路的长度;
d2是连接XM家和第二家商店的道路的长度;
d3是连接两个商店的道路的长度。
输出
对于每组测试样例输出XM从家出发光顾两个商店并返回家中所需的最短行程。
输入样例
2
10 20 30
1 1 5
10
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,a,b,c;
cin>>t;
while(t--){
cin>>a>>b>>c;
int p[4];
p[0]=a+b+c;
p[1]=2*a+2*c;
p[2]=2*a+2*b;
p[3]=2*b+2*c;
sort(p,p+4);
cout<<p[0]<<endl;
}
return 0;
}
G 迷宫BFS模板 AC code:
#include<bits/stdc++.h>
using namespace std;
char ma[105][105];
bool vis[105][105];
int n,m;
struct node{
int a,b,step;
};
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool check(int a,int b){
return a>=0&&a<n&&b>=0&&b<m;
}
int bfs(int x,int y){
queue <node> Q;
node u,v;
int ans=-1;
u.a=x,u.b=y,u.step=0;
vis[x][y]=1;
Q.push(u);
while(!Q.empty()){
u=Q.front();
Q.pop();
if(ma[u.a][u.b]=='t') {
return u.step;
}
for(int i=0;i<4;i++){
int fx=u.a+dx[i];
int fy=u.b+dy[i];
if(check(fx,fy)&&vis[fx][fy]==0&&ma[fx][fy]!='#'){
v.a=fx,v.b=fy,v.step=u.step+1;vis[fx][fy]=1;
Q.push(v);
}
}
}
return -1;
}
int main(){
while(cin>>n>>m){
memset(vis,0,sizeof(vis));
memset(ma,0,sizeof(ma));
for(int i=0;i<n;i++) cin>>ma[i];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++) {
if(ma[i][j]=='s'){
cout<<bfs(i,j)<<endl;
break;
}
}
}
}
return 0;
}
/*
5 5
s]...
.].].
.....
.].].
...]t
*/
code:
唉 让学长看了woc输出格式多了个空格md又zz了。。。。。
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1e8+10;
double a[maxn];
int main(){
a[1]=1.0;
for(int i=2;i<=maxn;i++) a[i]=a[i-1]+1.0/i;
int t,m;
scanf("%d",&t);
int p=1;
while(t--){
scanf("%d",&m);
printf("Case %d: %.6f\n",p++,a[m]-0.0000005);//控制四舍五入
}
return 0;
}