今日SGU 5.3

SGU 107

题意:输入一个N,表示N位数字里面有多少个的平方数的结尾9位是987654321

收获:打表,你发现相同位数的数相乘结果的最后几位,就和那两个相乘的数最后几位相乘一样,比如3416*8516 = 29090656,它的最后两位就和16*16=256的最后两位一样 为56,那么你发现987654321位9位,而且你预处理出的那8个答案就是9位,你就看d=n-9为多少位,那么就是9*8*(d位10)相乘,一个for就行了

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;++i)
typedef long long ll;

int main(){
    //for(ll i =2;i<=1e9;++i) if(i*i%mod==x) cout<<i<<" ";
    int n;
    scanf("%d",&n);
    if(n<9) return puts("0"),0;
    if(n==9) return printf("8"),0;
    int d = n - 9;
    printf("72");
    rep(i,1,d) printf("0");
    return 0;
}
View Code

 SGU 104

题意:给你n多花,i<j的话,i花只能放在j花前面的花瓶,每个花和对应花瓶有不同的魅力值,问你怎么放花使得魅力值最大

收获:一开始想错了,看到了数据很小,而且很小匹配的问题,没有注意到花只能按顺序放,然后就写了最小费用流(边值弄成魅力值的相反数),然后一直re在2

最后看了题解,看到dp,就很快想出了转移方程,然后初始化这些的又搞了很久,真菜~~,dp挺好理解的,直接看代码吧

#include<bits/stdc++.h>
#define de(x) cout<<#x<<"="<<x<<endl;
#define dd(x) cout<<#x<<"="<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define repd(i,a,b) for(int i=a;i>=(b);--i)
#define repp(i,a,b,t) for(int i=a;i<(b);i+=t)
#define ll long long
#define mt(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define pdd pair<double,double>
#define pdi pair<double,int>
#define mp(u,v) make_pair(u,v)
#define sz(a) (int)a.size()
#define ull unsigned long long
#define ll long long
#define pb push_back
#define PI acos(-1.0)
#define qc std::ios::sync_with_stdio(false)
#define db double
#define all(a) a.begin(),a.end()
const int mod = 1e9+7;
const int maxn = 1e2+5;
const double eps = 1e-6;
using namespace std;
bool eq(const db &a, const db &b) { return fabs(a - b) < eps; }
bool ls(const db &a, const db &b) { return a + eps < b; }
bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); }
ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); };
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll read(){
    ll 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;
}
//inv[1]=1;
//for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
int c[maxn][maxn],dp[maxn][maxn]={0};
int path[maxn][maxn];
vector<int> ans;
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    rep(i,0,n+1)rep(j,0,m+1) dp[i][j]=-inf;
    rep(i,1,n+1) rep(j,1,m+1) scanf("%d",&c[i][j]);
    dp[0][0]=0;
    rep(i,1,n+1)rep(j,i,m+1)rep(k,i-1,j){
        if(dp[i][j]<dp[i-1][k]+c[i][j]){
            dp[i][j]=dp[i-1][k]+c[i][j];
            path[i][j] = k;
        }
    }
    int x,y;
    int mx = -inf;
    rep(i,n,m+1) if(mx<dp[n][i]){
        mx = dp[n][i];
        x=n;y=i;
    }
    printf("%d\n",mx);
    while(x!=1){
        ans.pb(y);
        y=path[x][y];x--;
    }
    ans.pb(y);
    reverse(all(ans));
    rep(i,0,sz(ans)) printf("%d%c",ans[i]," \n"[i+1==sz(ans)]);
    return 0;
}
View Code

 SGU 127

题意:做个电话簿,前两页目录,然后一页最多k个电话,电话首位不同的不能在同一页

收获:无

#include<bits/stdc++.h>
#define de(x) cout<<#x<<"="<<x<<endl;
#define dd(x) cout<<#x<<"="<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define repd(i,a,b) for(int i=a;i>=(b);--i)
#define repp(i,a,b,t) for(int i=a;i<(b);i+=t)
#define ll long long
#define mt(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define pdd pair<double,double>
#define pdi pair<double,int>
#define mp(u,v) make_pair(u,v)
#define sz(a) (int)a.size()
#define ull unsigned long long
#define ll long long
#define pb push_back
#define PI acos(-1.0)
#define qc std::ios::sync_with_stdio(false)
#define db double
#define all(a) a.begin(),a.end()
const int mod = 1e9+7;
const int maxn = 8025;
const double eps = 1e-6;
using namespace std;
bool eq(const db &a, const db &b) { return fabs(a - b) < eps; }
bool ls(const db &a, const db &b) { return a + eps < b; }
bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); }
ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); };
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll read(){
    ll 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;
}
//inv[1]=1;
//for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
int a[maxn],cnt[12]={0};
int main(){
    int k,n,ans=2;
    scanf("%d%d",&k,&n);
    rep(i,0,n) scanf("%d",a+i);
    sort(a,a+n);
    rep(i,0,n) cnt[a[i]/1000]++;
    rep(i,0,10) if(cnt[i]){
        ans += (cnt[i]+k-1)/k;
    }
    printf("%d\n",ans);
    return 0;
}
View Code

 

posted on 2018-05-03 19:26  chinacwj1  阅读(149)  评论(0编辑  收藏  举报

导航