zoukankan      html  css  js  c++  java
  • 历届试题 数字游戏 (规律)

      历届试题 数字游戏  
    时间限制:1.0s   内存限制:256.0MB
        
    问题描述
      栋栋正在和同学们玩一个数字游戏。

      游戏的规则是这样的:栋栋和同学们一共n个人围坐在一圈。栋栋首先说出数字1。接下来,坐在栋栋左手边的同学要说下一个数字2。再下面的一个同学要从上一个同学说的数字往下数两个数说出来,也就是说4。下一个同学要往下数三个数,说7。依次类推。

      为了使数字不至于太大,栋栋和同学们约定,当在心中数到 k-1 时,下一个数字从0开始数。例如,当k=13时,栋栋和同学们报出的前几个数依次为:
      1, 2, 4, 7, 11, 3, 9, 3, 11, 7。

      游戏进行了一会儿,栋栋想知道,到目前为止,他所有说出的数字的总和是多少。
    输入格式
      输入的第一行包含三个整数 n,k,T,其中 n 和 k 的意义如上面所述,T 表示到目前为止栋栋一共说出的数字个数。
    输出格式
      输出一行,包含一个整数,表示栋栋说出所有数的和。
    样例输入
    3 13 3
    样例输出
    17
    样例说明
      栋栋说出的数依次为1, 7, 9,和为17。
    数据规模和约定
      1 < n,k,T < 1,000,000;

    题解:规律题;陈陈每次说的数字为a0+n*(n+1)/2;注意考虑%k否则最后一组数据会wa;

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    #define SD(x,y) scanf("%lf%lf",&x,&y)
    #define P_ printf(" ")
    typedef long long LL;
    int main(){
    	int n,k,T;
    	scanf("%d%d%d",&n,&k,&T);
    	LL ans=0;
    	LL temp=1,t=n;
    	for(int i=0;i<T;i++){
    		ans+=temp;
    		if(t&1)temp=(1+(t+1)/2*t%k)%k;
    		else temp=(1+t/2*(t+1)%k)%k;
    		t=(t+n)%(2*k);
    		/*for(int j=0;j<n;j++){
    			t++;
    			if(t>=k)t%=k;
    			temp+=t;
    			if(temp>=k){
    				temp=temp-k;
    			}	
    		}*/
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    

      

  • 相关阅读:
    LeetCode——Generate Parentheses
    LeetCode——Best Time to Buy and Sell Stock IV
    LeetCode——Best Time to Buy and Sell Stock III
    LeetCode——Best Time to Buy and Sell Stock
    LeetCode——Find Minimum in Rotated Sorted Array
    Mahout实现基于用户的协同过滤算法
    使用Java对文件进行解压缩
    LeetCode——Convert Sorted Array to Binary Search Tree
    LeetCode——Missing Number
    LeetCode——Integer to Roman
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5122101.html
Copyright © 2011-2022 走看看