LA-3521 (数论)
2016-07-03 11:28
316 查看
题意:
输入正整数n和k,(1<=n,k<=1e9),计算k%1+k%2+k%3...+k%n;
思路:
按k/i分层,一层一层的计算和,每一层可以求出左右界,这一层中的每个数组成的是等差数列,差为k/i;然后等差数列求和,最后再加一块就是答案了;
AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
//const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e4+10;
const int maxn=1005;
const double eps=1e-10;
int main()
{
LL n,k;
while(cin>>n>>k)
{
LL ans=0;
if(n>k)ans+=(n-k)*k,n=k;
LL l,r,len,m;
while(n)
{
r=n;m=k/r;l=k/(m+1)+1;
len=(r-l);//区间长度-1;
ans+=(len+1)*(k%r)+(1+len)*len/2*m;//k%r为首项a0,m为差d,这个就是求等差数列的和;
n=l-1;
}
cout<<ans<<"\n";
}
return 0;
}
输入正整数n和k,(1<=n,k<=1e9),计算k%1+k%2+k%3...+k%n;
思路:
按k/i分层,一层一层的计算和,每一层可以求出左右界,这一层中的每个数组成的是等差数列,差为k/i;然后等差数列求和,最后再加一块就是答案了;
AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
//const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e4+10;
const int maxn=1005;
const double eps=1e-10;
int main()
{
LL n,k;
while(cin>>n>>k)
{
LL ans=0;
if(n>k)ans+=(n-k)*k,n=k;
LL l,r,len,m;
while(n)
{
r=n;m=k/r;l=k/(m+1)+1;
len=(r-l);//区间长度-1;
ans+=(len+1)*(k%r)+(1+len)*len/2*m;//k%r为首项a0,m为差d,这个就是求等差数列的和;
n=l-1;
}
cout<<ans<<"\n";
}
return 0;
}
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?