Mashmokh and Tokens

Codeforces Round #240 (Div. 2) B;http://codeforces.com/problemset/problem/415/B

题意:老板一天发x张代币券,员工能用它来换大洋,用w张代币券可换[a*w/b](下取整)块大洋,代币券只能当天适用,求换最多大洋时最多能留多少代币券

比如a=3,b=7,x=4时,我最多能换3×4/7=1块大洋,但是我显然用3张代币券就能换1块大洋,所以多的1块就应该被保留。

题解:就是找一个w0,使得a*(w-w0)/b最接近a*w/b;推到过程很巧妙,最终的结果就是a*w%b/a,注意用long long.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int N=1e5+10;
 7 long long c[N];
 8 int n;
 9 long long a,b;
10 int main(){
11    while(~scanf("%d%I64d%I64d",&n,&a,&b)){
12        for(int i=1;i<=n;i++){
13         scanf("%I64d",&c[i]);
14        }
15       for(int i=1;i<=n;i++){
16         if(i==n)printf("%I64d
",(c[i]*a)%b/a);
17         else{
18             printf("%I64d ",(c[i]*a)%b/a);
19         }
20       }
21    }
22 }
View Code
原文地址:https://www.cnblogs.com/chujian123/p/3891126.html